1 /*
   2  * Copyright (c) 2013, 2014, 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 
  24 /*
  25  * @test
  26  * @bug 8010927
  27  * @summary Kitchensink crashed with SIGSEGV, Problematic frame: v ~StubRoutines::checkcast_arraycopy
  28  * @library /../../test/lib /testlibrary
  29  * @build Test8010927
  30  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  31  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  32  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+IgnoreUnrecognizedVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. -Xmx64m -XX:NewSize=20971520 -XX:MaxNewSize=32m -XX:-UseTLAB -XX:-UseAdaptiveSizePolicy Test8010927
  33  */
  34 
  35 import sun.hotspot.WhiteBox;
  36 import java.lang.reflect.Field;
  37 import sun.misc.Unsafe;
  38 
  39 /**
  40  * The test creates uncommitted space between oldgen and young gen
  41  * by specifying MaxNewSize bigger than NewSize.
  42  * NewSize = 20971520 = (512*4K) * 10 for 4k pages
  43  * Then it tries to execute arraycopy() with elements type check
  44  * to the array at the end of survive space near unused space.
  45  */
  46 
  47 public class Test8010927 {
  48 
  49   private static final Unsafe U;
  50 
  51   static {
  52     try {
  53       Field unsafe = Unsafe.class.getDeclaredField("theUnsafe");
  54       unsafe.setAccessible(true);
  55       U = (Unsafe) unsafe.get(null);
  56     } catch (Exception e) {
  57       throw new Error(e);
  58     }
  59   }
  60 
  61   public static Object[] o;
  62 
  63   public static final boolean debug = Boolean.getBoolean("debug");
  64 
  65   // 2 different obect arrays but same element types
  66   static Test8010927[] masterA;
  67   static Object[] masterB;
  68   static final Test8010927 elem = new Test8010927();
  69   static final WhiteBox wb = WhiteBox.getWhiteBox();
  70 
  71   static final int obj_header_size = U.ARRAY_OBJECT_BASE_OFFSET;
  72   static final int heap_oop_size = wb.getHeapOopSize();
  73   static final int card_size = 512;
  74   static final int one_card = (card_size - obj_header_size)/heap_oop_size;
  75 
  76   static final int surv_size = 2112 * 1024;
  77 
  78   // The size is big to not fit into survive space.
  79   static final Object[] cache = new Object[(surv_size / card_size)];
  80 
  81   public static void main(String[] args) {
  82     masterA = new Test8010927[one_card];
  83     masterB = new Object[one_card];
  84     for (int i = 0; i < one_card; ++i) {
  85       masterA[i] = elem;
  86       masterB[i] = elem;
  87     }
  88 
  89     // Move cache[] to the old gen.
  90     long low_limit = wb.getObjectAddress(cache);
  91     System.gc();
  92     // Move 'cache' to oldgen.
  93     long upper_limit = wb.getObjectAddress(cache);
  94     if ((low_limit - upper_limit) > 0) { // substaction works with unsigned values
  95       // OldGen is placed before youngger for ParallelOldGC.
  96       upper_limit = low_limit + 21000000l; // +20971520
  97     }
  98     // Each A[one_card] size is 512 bytes,
  99     // it will take about 40000 allocations to trigger GC.
 100     // cache[] has 8192 elements so GC should happen
 101     // each 5th iteration.
 102     for(long l = 0; l < 20; l++) {
 103       fill_heap();
 104       if (debug) {
 105         System.out.println("test oop_disjoint_arraycopy");
 106       }
 107       testA_arraycopy();
 108       if (debug) {
 109         System.out.println("test checkcast_arraycopy");
 110       }
 111       testB_arraycopy();
 112       // Execute arraycopy to the topmost array in young gen
 113       if (debug) {
 114         int top_index = get_top_address(low_limit, upper_limit);
 115         if (top_index >= 0) {
 116           long addr = wb.getObjectAddress(cache[top_index]);
 117           System.out.println("top_addr: 0x" + Long.toHexString(addr) + ", 0x" + Long.toHexString(addr + 512));
 118         }
 119       }
 120     }
 121   }
 122   static void fill_heap() {
 123     for (int i = 0; i < cache.length; ++i) {
 124       o = new Test8010927[one_card];
 125       System.arraycopy(masterA, 0, o, 0, masterA.length);
 126       cache[i] = o;
 127     }
 128     for (long j = 0; j < 256; ++j) {
 129       o = new Long[10000]; // to trigger GC
 130     }
 131   }
 132   static void testA_arraycopy() {
 133     for (int i = 0; i < cache.length; ++i) {
 134       System.arraycopy(masterA, 0, cache[i], 0, masterA.length);
 135     }
 136   }
 137   static void testB_arraycopy() {
 138     for (int i = 0; i < cache.length; ++i) {
 139       System.arraycopy(masterB, 0, cache[i], 0, masterB.length);
 140     }
 141   }
 142   static int get_top_address(long min, long max) {
 143     int index = -1;
 144     long addr = min;
 145     for (int i = 0; i < cache.length; ++i) {
 146       long test = wb.getObjectAddress(cache[i]);
 147       if (((test - addr) > 0) && ((max - test) > 0)) { // substaction works with unsigned values
 148         addr = test;
 149         index = i;
 150       }
 151     }
 152     return index;
 153   }
 154 }