test/hotspot/jtreg/compiler/exceptions/SumTest.java
author stefank
Tue, 28 Nov 2017 21:43:45 +0100
changeset 48157 7c4d43c26352
parent 47216 71c04702a3d5
permissions -rw-r--r--
8192061: Clean up allocation.inline.hpp includes Reviewed-by: eosterlund, coleenp
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
 *
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 28045
diff changeset
    29
 * @run main/othervm -Xbatch compiler.exceptions.SumTest
28045
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    30
 */
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 28045
diff changeset
    31
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 28045
diff changeset
    32
package compiler.exceptions;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 28045
diff changeset
    33
28045
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    34
public class SumTest {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    35
    private static class Sum {
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
        double[] sums;
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
        /**
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    40
         * Construct empty Sum
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
        public Sum() {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    43
            sums = new double[0];
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    44
        }
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
        /**
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    47
         * Return the sum of all numbers added to this Sum
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    48
         *
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    49
         * @return the sum
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    50
         */
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    51
        final public double getSum() {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    52
            double sum = 0;
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    53
            for (final double s : sums) {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    54
                sum += s;
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
            return sum;
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    58
        }
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
        /**
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    61
         * Add a new number to this Sum
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    62
         *
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    63
         * @param a number to be added.
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    64
         */
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    65
        final public void add(double a) {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    66
            try {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    67
                sums[sums.length] = -1; // Cause IndexOutOfBoundsException
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    68
            } catch (final IndexOutOfBoundsException e) {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    69
                final double[] oldSums = sums;
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    70
                sums = new double[oldSums.length + 1]; // Extend sums
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    71
                System.arraycopy(oldSums, 0, sums, 0, oldSums.length);
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    72
                sums[oldSums.length] = a; // Append a
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
        }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    75
    }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    76
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    77
    public static void main(String[] args) throws Exception {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    78
        final Sum sum = new Sum();
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    79
        for (int i = 1; i <= 10000; ++i) {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    80
            sum.add(1);
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    81
            double ii = sum.getSum();
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    82
            if (i != ii) {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    83
                throw new Exception("Failure: computed = " + ii + ", expected = " + i);
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
    }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    87
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    88
}
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    89