1 /*
  2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 /* @test id=passive
 27  * @summary Test that garbage in the pinned region does not crash VM
 28  * @key randomness
 29  * @requires vm.gc.Shenandoah
 30  * @library /test/lib
 31  *
 32  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx128m
 33  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 34  *      -XX:+ShenandoahVerify -XX:+ShenandoahDegeneratedGC
 35  *      TestPinnedGarbage
 36  *
 37  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx128m
 38  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 39  *      -XX:+ShenandoahVerify -XX:-ShenandoahDegeneratedGC
 40  *      TestPinnedGarbage
 41  */
 42 
 43 /* @test id=aggressive
 44  * @summary Test that garbage in the pinned region does not crash VM
 45  * @key randomness
 46  * @requires vm.gc.Shenandoah
 47  * @library /test/lib
 48  *
 49  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx128m
 50  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 51  *      TestPinnedGarbage
 52  */
 53 
 54 /* @test id=verify
 55  * @summary Test that garbage in the pinned region does not crash VM
 56  * @key randomness
 57  * @requires vm.gc.Shenandoah
 58  * @library /test/lib
 59  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx128m
 60  *      -XX:+UseShenandoahGC
 61  *      -XX:+ShenandoahVerify
 62  *      TestPinnedGarbage
 63  */
 64 
 65 /* @test id=generational
 66  * @summary Test that garbage in the pinned region does not crash VM
 67  * @key randomness
 68  * @requires vm.gc.Shenandoah
 69  * @library /test/lib
 70  *
 71  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx128m
 72  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
 73  *      TestPinnedGarbage
 74  *
 75  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx128m
 76  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
 77  *      -XX:+ShenandoahVerify
 78  *      TestPinnedGarbage
 79  */
 80 
 81 import java.util.Arrays;
 82 import java.util.Random;
 83 import jdk.test.lib.Utils;
 84 
 85 public class TestPinnedGarbage {
 86     static {
 87         System.loadLibrary("TestPinnedGarbage");
 88     }
 89 
 90     private static final int NUM_RUNS      = 1_000;
 91     private static final int OBJS_COUNT    = 1 << 10;
 92     private static final int GARBAGE_COUNT = 1 << 18;
 93 
 94     private static native void pin(int[] a);
 95     private static native void unpin(int[] a);
 96 
 97     public static void main(String[] args) {
 98         Random rng = Utils.getRandomInstance();
 99         for (int i = 0; i < NUM_RUNS; i++) {
100             test(rng);
101         }
102     }
103 
104     private static void test(Random rng) {
105         Object[] objs = new Object[OBJS_COUNT];
106         for (int i = 0; i < OBJS_COUNT; i++) {
107             objs[i] = new MyClass();
108         }
109 
110         int[] cog = new int[10];
111         int cogIdx = rng.nextInt(OBJS_COUNT);
112         objs[cogIdx] = cog;
113         pin(cog);
114 
115         for (int i = 0; i < GARBAGE_COUNT; i++) {
116             int rIdx = rng.nextInt(OBJS_COUNT);
117             if (rIdx != cogIdx) {
118                 objs[rIdx] = new MyClass();
119             }
120         }
121 
122         unpin(cog);
123     }
124 
125     public static class MyClass {
126         public Object ref = new Object();
127     }
128 
129 }