1/*
2 * Copyright (c) 2015 SAP SE. 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/*
25 * @test
26 * @summary Test that overflowed integers passed to arraycopy don't do any harm. This might
27 *          be the case on platforms where C-code expects that ints passed to a call
28 *          are properly sign extended to 64 bit (e.g., PPC64, s390x). This can fail
29 *          if slow_arraycopy_C() is commpiled by the C compiler without any imlicit
30 *          casts (as spill stores to the stack that are done with 4-byte instruction).
31 *
32 * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
33 *                   compiler.arraycopy.TestArrayCopyOverflowArguments
34 */
35
36package compiler.arraycopy;
37
38public class TestArrayCopyOverflowArguments {
39
40    // Without volatile the overflowing computation was moved up and then
41    // spilled to the stack. The 32-bit spill store caused proper rounding.
42    static volatile int mod = Integer.MAX_VALUE;
43
44    public static int[] m1(Object src) {
45        if (src == null) return null;
46        int[] dest = new int[10];
47        try {
48            // PPC C calling conventions require that ints are properly expanded
49            // to longs when passed to a function.
50            int pos   =  8 + mod + mod; // = 0x1_0000_0006.
51            int start =  2 + mod + mod; // = 0x1_0000_0000.
52            int len   = 12 + mod + mod; // = 0x1_0000_0010.
53            // This is supposed to call SharedRuntime::slow_arraycopy_C().
54            System.arraycopy(src, pos, dest, 0, 10);
55        } catch (ArrayStoreException npe) {
56        }
57        return dest;
58    }
59
60    static public void main(String[] args) throws Exception {
61        int[] src = new int[20];
62
63        for (int i  = 0; i < 20; ++i) {
64            src[i] = i * (i-1);
65        }
66
67        for (int i = 0; i < 20000; i++) {
68            m1(src);
69        }
70    }
71}
72
73