8235250: Create (test) abstraction for allocating objects that is immune to removal by optimizations

Reviewed-by: eosterlund, tschatzl
diff --git a/test/hotspot/jtreg/gc/arguments/TestTargetSurvivorRatioFlag.java b/test/hotspot/jtreg/gc/arguments/TestTargetSurvivorRatioFlag.java
index fa0b7fb..53d698a 100644
--- a/test/hotspot/jtreg/gc/arguments/TestTargetSurvivorRatioFlag.java
+++ b/test/hotspot/jtreg/gc/arguments/TestTargetSurvivorRatioFlag.java
@@ -23,8 +23,6 @@
 
 package gc.arguments;
 
-import static java.lang.ref.Reference.reachabilityFence;
-
 /*
  * @test TestTargetSurvivorRatioFlag
  * @key gc
@@ -50,9 +48,9 @@
 import java.util.regex.Pattern;
 import jdk.internal.misc.Unsafe;
 import jdk.test.lib.process.OutputAnalyzer;
-import jdk.test.lib.process.ProcessTools;
 import jdk.test.lib.Utils;
 import sun.hotspot.WhiteBox;
+import static gc.testlibrary.Allocation.blackHole;
 
 /* In order to test that TargetSurvivorRatio affects survivor space occupancy
  * we setup fixed MaxTenuringThreshold and then verifying that if size of allocated
@@ -299,7 +297,7 @@
 
             // force minor GC
             while (youngGCBean.getCollectionCount() <= initialGcId + MAX_TENURING_THRESHOLD * 2) {
-                reachabilityFence(new byte[ARRAY_LENGTH]);
+                blackHole(new byte[ARRAY_LENGTH]);
             }
 
             allocator.release();
diff --git a/test/hotspot/jtreg/gc/cslocker/TestCSLocker.java b/test/hotspot/jtreg/gc/cslocker/TestCSLocker.java
index 0dc57aa..4686ed8 100644
--- a/test/hotspot/jtreg/gc/cslocker/TestCSLocker.java
+++ b/test/hotspot/jtreg/gc/cslocker/TestCSLocker.java
@@ -23,12 +23,13 @@
 
 package gc.cslocker;
 
-import static java.lang.ref.Reference.reachabilityFence;
+import static gc.testlibrary.Allocation.blackHole;
 
 /*
  * @test TestCSLocker
  * @key gc
  * @bug 6186200
+ * @library /
  * @summary This short test check RFE 6186200 changes. One thread locked
  * @summary completely in JNI CS, while other is trying to allocate memory
  * @summary provoking GC. OOM means FAIL, deadlock means PASS.
@@ -75,7 +76,7 @@
 
         while (isRunning) {
             try {
-                reachabilityFence(new int[size]);
+                blackHole(new int[size]);
                 sleep(sleepTime);
             } catch (InterruptedException e) {
                 isRunning = false;
diff --git a/test/hotspot/jtreg/gc/g1/TestHumongousAllocNearlyFullRegion.java b/test/hotspot/jtreg/gc/g1/TestHumongousAllocNearlyFullRegion.java
index 87eb727e..aae56bc 100644
--- a/test/hotspot/jtreg/gc/g1/TestHumongousAllocNearlyFullRegion.java
+++ b/test/hotspot/jtreg/gc/g1/TestHumongousAllocNearlyFullRegion.java
@@ -23,7 +23,7 @@
 
 package gc.g1;
 
-import static java.lang.ref.Reference.reachabilityFence;
+import static gc.testlibrary.Allocation.blackHole;
 
 /*
  * @test TestHumongousAllocNearlyFullRegion
@@ -33,6 +33,7 @@
  * @requires vm.gc.G1
  * @modules java.base/jdk.internal.misc
  * @library /test/lib
+ * @library /
  * @run driver gc.g1.TestHumongousAllocNearlyFullRegion
  */
 
@@ -64,7 +65,7 @@
             for (int i = 0; i < heapSize; i++) {
                 // 131069 is the number of longs it takes to fill a heapRegion except
                 // for 8 bytes on 64 bit.
-                reachabilityFence(new long[131069]);
+                blackHole(new long[131069]);
             }
         }
     }
diff --git a/test/hotspot/jtreg/gc/g1/humongousObjects/TestNoAllocationsInHRegions.java b/test/hotspot/jtreg/gc/g1/humongousObjects/TestNoAllocationsInHRegions.java
index a5b6c37..8a3bf91 100644
--- a/test/hotspot/jtreg/gc/g1/humongousObjects/TestNoAllocationsInHRegions.java
+++ b/test/hotspot/jtreg/gc/g1/humongousObjects/TestNoAllocationsInHRegions.java
@@ -26,7 +26,7 @@
 import jdk.test.lib.Utils;
 import sun.hotspot.WhiteBox;
 
-import static java.lang.ref.Reference.reachabilityFence;
+import static gc.testlibrary.Allocation.blackHole;
 
 import java.util.LinkedList;
 import java.util.List;
@@ -141,7 +141,7 @@
                     // Dead object allocation
                     () -> {
                         int size = RND.nextInt(DEAD_OBJECT_MAX_SIZE);
-                        reachabilityFence(new byte[size]);
+                        blackHole(new byte[size]);
                     },
 
                     // Check
diff --git a/test/hotspot/jtreg/gc/logging/TestUnifiedLoggingSwitchStress.java b/test/hotspot/jtreg/gc/logging/TestUnifiedLoggingSwitchStress.java
index 0ab231d..c47568b 100644
--- a/test/hotspot/jtreg/gc/logging/TestUnifiedLoggingSwitchStress.java
+++ b/test/hotspot/jtreg/gc/logging/TestUnifiedLoggingSwitchStress.java
@@ -32,7 +32,7 @@
 import javax.management.ObjectName;
 import javax.management.ReflectionException;
 
-import static java.lang.ref.Reference.reachabilityFence;
+import static gc.testlibrary.Allocation.blackHole;
 
 import java.lang.management.ManagementFactory;
 import java.util.LinkedList;
@@ -131,7 +131,7 @@
             // Dead object allocation
             () -> {
                 int size = RND.nextInt(DEAD_OBJECT_MAX_SIZE);
-                reachabilityFence(new byte[size]);
+                blackHole(new byte[size]);
             }
     };
 
diff --git a/test/hotspot/jtreg/gc/testlibrary/Allocation.java b/test/hotspot/jtreg/gc/testlibrary/Allocation.java
new file mode 100644
index 0000000..c66106d
--- /dev/null
+++ b/test/hotspot/jtreg/gc/testlibrary/Allocation.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package gc.testlibrary;
+
+public class Allocation {
+    public static volatile Object obj;
+
+    /**
+     * This code assigns the object to a "public static volatile" variable. The
+     * compiler does not seem capable of optimizing through this (yet). Any object
+     * allocated and sent to this method ought not to be optimized away.
+     *
+     * @param obj The allocation of this object will not be optimized away.
+     */
+
+    public static void blackHole(Object obj) {
+        Allocation.obj = obj;
+        Allocation.obj = null;
+    }
+}
diff --git a/test/hotspot/jtreg/gc/z/TestSmallHeap.java b/test/hotspot/jtreg/gc/z/TestSmallHeap.java
index 435d939..1707e20 100644
--- a/test/hotspot/jtreg/gc/z/TestSmallHeap.java
+++ b/test/hotspot/jtreg/gc/z/TestSmallHeap.java
@@ -27,6 +27,7 @@
  * @test TestSmallHeap
  * @requires vm.gc.Z & !vm.graal.enabled & vm.compMode != "Xcomp"
  * @summary Test ZGC with small heaps
+ * @library /
  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xlog:gc,gc+init,gc+heap -Xmx8M gc.z.TestSmallHeap
  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xlog:gc,gc+init,gc+heap -Xmx16M gc.z.TestSmallHeap
  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xlog:gc,gc+init,gc+heap -Xmx32M gc.z.TestSmallHeap
@@ -37,7 +38,7 @@
  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xlog:gc,gc+init,gc+heap -Xmx1024M gc.z.TestSmallHeap
  */
 
-import java.lang.ref.Reference;
+import static gc.testlibrary.Allocation.blackHole;
 
 public class TestSmallHeap {
     public static void main(String[] args) throws Exception {
@@ -45,10 +46,10 @@
         System.out.println("Max Capacity " + maxCapacity + " bytes");
 
         // Allocate byte arrays of increasing length, so that
-        // all allocaion paths (small/medium/large) are tested.
+        // all allocation paths (small/medium/large) are tested.
         for (int length = 16; length <= maxCapacity / 16; length *= 2) {
             System.out.println("Allocating " + length + " bytes");
-            Reference.reachabilityFence(new byte[length]);
+            blackHole(new byte[length]);
         }
 
         System.out.println("Success");