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