|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. 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 | +package compiler.c2.irTests.gc; |
| 24 | + |
| 25 | +import jdk.test.lib.Asserts; |
| 26 | +import compiler.lib.ir_framework.*; |
| 27 | +import jdk.test.whitebox.gc.GC; |
| 28 | + |
| 29 | +import java.lang.ref.*; |
| 30 | +import java.util.*; |
| 31 | + |
| 32 | +/* |
| 33 | + * @test |
| 34 | + * @bug 8256999 |
| 35 | + * @summary Test that Reference.refersTo intrinsics are properly handled |
| 36 | + * @library /test/lib / |
| 37 | + * @build jdk.test.whitebox.WhiteBox |
| 38 | + * @requires vm.compiler2.enabled |
| 39 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox |
| 40 | + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI compiler.c2.irTests.gc.ReferenceRefersToTests |
| 41 | + */ |
| 42 | +public class ReferenceRefersToTests { |
| 43 | + |
| 44 | + private static String[] args(String... add) { |
| 45 | + List<String> args = new ArrayList<>(); |
| 46 | + |
| 47 | + // Use PerMethodTrapLimit=0 to compile all branches in the intrinsics. |
| 48 | + args.add("-XX:PerMethodTrapLimit=0"); |
| 49 | + |
| 50 | + // Forcefully inline all methods to reach the intrinsic code. |
| 51 | + args.add("-XX:CompileCommand=inline,compiler.c2.irTests.gc.ReferenceRefersToTests::*"); |
| 52 | + args.add("-XX:CompileCommand=inline,java.lang.ref.Reference::*"); |
| 53 | + args.add("-XX:CompileCommand=inline,java.lang.ref.PhantomReference::*"); |
| 54 | + |
| 55 | + // Mix in test config code. |
| 56 | + args.addAll(Arrays.asList(add)); |
| 57 | + |
| 58 | + return args.toArray(new String[0]); |
| 59 | + } |
| 60 | + |
| 61 | + public static void main(String[] args) { |
| 62 | + TestFramework framework = new TestFramework(); |
| 63 | + |
| 64 | + int idx = 0; |
| 65 | + if (GC.isSelectedErgonomically() && GC.Serial.isSupported()) { |
| 66 | + // Serial does not have any barriers in refersTo. |
| 67 | + framework.addScenarios(new Scenario(idx++, args( |
| 68 | + "-XX:+UseSerialGC" |
| 69 | + ))); |
| 70 | + } |
| 71 | + if (GC.isSelectedErgonomically() && GC.Parallel.isSupported()) { |
| 72 | + // Parallel does not have any barriers in refersTo. |
| 73 | + framework.addScenarios(new Scenario(idx++, args( |
| 74 | + "-XX:+UseParallelGC" |
| 75 | + ))); |
| 76 | + } |
| 77 | + if (GC.isSelectedErgonomically() && GC.G1.isSupported()) { |
| 78 | + // G1 nominally needs keep-alive barriers for Reference loads, |
| 79 | + // but should not have them for refersTo. |
| 80 | + framework.addScenarios(new Scenario(idx++, args( |
| 81 | + "-XX:+UseG1GC" |
| 82 | + ))); |
| 83 | + } |
| 84 | + if (GC.isSelectedErgonomically() && GC.Shenandoah.isSupported()) { |
| 85 | + // Shenandoah nominally needs keep-alive barriers for Reference loads, |
| 86 | + // but should not have them for refersTo. We only care to check that |
| 87 | + // SATB barrier is not emitted. Shenandoah would also emit LRB barrier, |
| 88 | + // which would false-negative the test. |
| 89 | + framework.addScenarios(new Scenario(idx++, args( |
| 90 | + "-XX:+UnlockDiagnosticVMOptions", |
| 91 | + "-XX:ShenandoahGCMode=passive", |
| 92 | + "-XX:+ShenandoahSATBBarrier", |
| 93 | + "-XX:+UseShenandoahGC" |
| 94 | + ))); |
| 95 | + } |
| 96 | + if (GC.isSelectedErgonomically() && GC.Z.isSupported()) { |
| 97 | + // ZGC does not emit barriers in IR. |
| 98 | + framework.addScenarios(new Scenario(idx++, args( |
| 99 | + "-XX:+UseZGC" |
| 100 | + ))); |
| 101 | + } |
| 102 | + framework.start(); |
| 103 | + } |
| 104 | + |
| 105 | + static final Object REF = new Object(); |
| 106 | + |
| 107 | + static final SoftReference<Object> SR = new SoftReference<>(REF); |
| 108 | + static final WeakReference<Object> WR = new WeakReference<>(REF); |
| 109 | + static final PhantomReference<Object> PR = new PhantomReference<>(REF, null); |
| 110 | + |
| 111 | + // Verify that we are left with a single load of Reference.referent and no stores. |
| 112 | + // This serves as a signal that no GC barriers are emitted in IR. |
| 113 | + |
| 114 | + @Test |
| 115 | + @IR(counts = { IRNode.LOAD, "1" }) |
| 116 | + @IR(failOn = { IRNode.STORE }) |
| 117 | + public boolean soft_null() { |
| 118 | + return SR.refersTo(null); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + @IR(counts = { IRNode.LOAD, "1" }) |
| 123 | + @IR(failOn = { IRNode.STORE }) |
| 124 | + public boolean soft_ref() { |
| 125 | + return SR.refersTo(REF); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + @IR(counts = { IRNode.LOAD, "1" }) |
| 130 | + @IR(failOn = { IRNode.STORE }) |
| 131 | + public boolean weak_null() { |
| 132 | + return WR.refersTo(null); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + @IR(counts = { IRNode.LOAD, "1" }) |
| 137 | + @IR(failOn = { IRNode.STORE }) |
| 138 | + public boolean weak_ref() { |
| 139 | + return WR.refersTo(REF); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + @IR(counts = { IRNode.LOAD, "1" }) |
| 144 | + @IR(failOn = { IRNode.STORE }) |
| 145 | + public boolean phantom_null() { |
| 146 | + return PR.refersTo(null); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + @IR(counts = { IRNode.LOAD, "1" }) |
| 151 | + @IR(failOn = { IRNode.STORE }) |
| 152 | + public boolean phantom_ref() { |
| 153 | + return PR.refersTo(REF); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments