1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. 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 TestPinnedGarbage
  26  * @summary Test that garbage in the pinned region does not crash VM
  27  * @key randomness
  28  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  29  * @library /test/lib
  30  *
  31  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
  32  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  33  *      -XX:+ShenandoahVerify -XX:+ShenandoahDegeneratedGC
  34  *      TestPinnedGarbage
  35  *
  36  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
  37  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  38  *      -XX:+ShenandoahVerify -XX:-ShenandoahDegeneratedGC
  39  *      TestPinnedGarbage
  40  */
  41 
  42 /* @test TestPinnedGarbage
  43  * @summary Test that garbage in the pinned region does not crash VM
  44  * @key randomness
  45  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  46  * @library /test/lib
  47  *
  48  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
  49  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  50  *      TestPinnedGarbage
  51  *
  52  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
  53  *      -XX:+UseShenandoahGC
  54  *      -XX:+ShenandoahVerify
  55  *      TestPinnedGarbage
  56  */
  57 
  58 import java.util.Arrays;
  59 import java.util.Random;
  60 import jdk.test.lib.Utils;
  61 
  62 public class TestPinnedGarbage {
  63     static {
  64         System.loadLibrary("TestPinnedGarbage");
  65     }
  66 
  67     private static final int NUM_RUNS      = 1_000;
  68     private static final int OBJS_COUNT    = 1_000;
  69     private static final int GARBAGE_COUNT = 1_000_000;
  70 
  71     private static native void pin(int[] a);
  72     private static native void unpin(int[] a);
  73 
  74     public static void main(String[] args) {
  75         for (int i = 0; i < NUM_RUNS; i++) {
  76             test();
  77         }
  78     }
  79 
  80     private static void test() {
  81         Object[] objs = new Object[OBJS_COUNT];
  82         for (int i = 0; i < OBJS_COUNT; i++) {
  83             objs[i] = new MyClass();
  84         }
  85 
  86         int[] cog = new int[10];
  87         Random rng = Utils.getRandomInstance();
  88         int cogIdx = rng.nextInt(OBJS_COUNT);
  89         objs[cogIdx] = cog;
  90         pin(cog);
  91 
  92         for (int i = 0; i < GARBAGE_COUNT; i++) {
  93             int rIdx = rng.nextInt(OBJS_COUNT);
  94             if (rIdx != cogIdx) {
  95                 objs[rIdx] = new MyClass();
  96             }
  97         }
  98 
  99         unpin(cog);
 100     }
 101 
 102     public static class MyClass {
 103         public Object ref = new Object();
 104     }
 105 
 106 }