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  * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement TestArrayCopyOverflowArguments
  32  *
  33  */
  34 
  35 public class TestArrayCopyOverflowArguments {
  36 
  37     // Without volatile the overflowing computation was moved up and then
  38     // spilled to the stack. The 32-bit spill store caused proper rounding.
  39     static volatile int mod = Integer.MAX_VALUE;
  40 
  41     public static int[] m1(Object src) {
  42         if (src == null) return null;
  43         int[] dest = new int[10];
  44         try {
  45             // PPC C calling conventions require that ints are properly expanded
  46             // to longs when passed to a function.
  47             int pos   =  8 + mod + mod; // = 0x1_0000_0006.
  48             int start =  2 + mod + mod; // = 0x1_0000_0000.
  49             int len   = 12 + mod + mod; // = 0x1_0000_0010.
  50             // This is supposed to call SharedRuntime::slow_arraycopy_C().
  51             System.arraycopy(src, pos, dest, 0, 10);
  52         } catch (ArrayStoreException npe) {
  53         }
  54         return dest;
  55     }
  56 
  57     static public void main(String[] args) throws Exception {
  58         int[] src = new int[20];
  59 
  60         for (int i  = 0; i < 20; ++i) {
  61             src[i] = i * (i-1);
  62         }
  63 
  64         for (int i = 0; i < 20000; i++) {
  65             m1(src);
  66         }
  67     }
  68 }
  69