< prev index next >

test/hotspot/jtreg/compiler/gcbarriers/TestImplicitNullChecks.java

Print this page

  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 package compiler.gcbarriers;
 25 
 26 import compiler.lib.ir_framework.*;
 27 import java.lang.invoke.VarHandle;
 28 import java.lang.invoke.MethodHandles;
 29 import java.lang.ref.Reference;
 30 import java.lang.ref.ReferenceQueue;
 31 import java.lang.ref.SoftReference;
 32 import java.lang.ref.WeakReference;
 33 import jdk.test.lib.Asserts;

 34 
 35 /**
 36  * @test
 37  * @summary Test that implicit null checks are generated as expected for
 38             different GC memory accesses.
 39  * @library /test/lib /

 40  * @run driver compiler.gcbarriers.TestImplicitNullChecks
 41  */
 42 
 43 
 44 public class TestImplicitNullChecks {
 45 
 46     static class Outer {
 47         Object f;
 48     }
 49 
 50     static class OuterWithVolatileField {
 51         volatile Object f;
 52     }
 53 
 54     static final VarHandle fVarHandle;

 55     static {
 56         MethodHandles.Lookup l = MethodHandles.lookup();
 57         try {
 58             fVarHandle = l.findVarHandle(Outer.class, "f", Object.class);
 59         } catch (Exception e) {
 60             throw new Error(e);
 61         }
 62     }
 63 
 64     public static void main(String[] args) {
 65         TestFramework.runWithFlags("-XX:CompileCommand=inline,java.lang.ref.*::*",

 66                                    "-XX:-TieredCompilation");
 67     }
 68 
 69     @Test
 70     // On AIX, implicit null checks are limited because the zero page is
 71     // readable (but not writable). See os::zero_page_read_protected().
 72     @IR(applyIfPlatform = {"aix", "false"},
 73         applyIfOr = {"UseZGC", "true", "UseG1GC", "true"},
 74         counts = {IRNode.NULL_CHECK, "1"},
 75         phase = CompilePhase.FINAL_CODE)
 76     static Object testLoad(Outer o) {
 77         return o.f;
 78     }
 79 
 80     @Test
 81     // On aarch64, volatile loads always use indirect memory operands, which
 82     // leads to a pattern that cannot be exploited by the current C2 analysis.
 83     // On PPC64, volatile loads are preceded by membar_volatile instructions,
 84     // which also inhibits the current C2 analysis.
 85     @IR(applyIfPlatformAnd = {"aarch64", "false", "ppc", "false"},

137 
138     @Run(test = {"testStore"})
139     static void runStoreTests() {
140         {
141             Outer o = new Outer();
142             Object o1 = new Object();
143             testStore(o, o1);
144         }
145     }
146 
147     @Test
148     // G1 and ZGC compare-and-exchange operations cannot be currently used to
149     // implement implicit null checks, because they expand into multiple memory
150     // access instructions that are not necessarily located at the initial
151     // instruction start address. The same holds for testCompareAndSwap and
152     // testGetAndSet below.
153     @IR(applyIfOr = {"UseZGC", "true", "UseG1GC", "true"},
154         failOn = IRNode.NULL_CHECK,
155         phase = CompilePhase.FINAL_CODE)
156     static Object testCompareAndExchange(Outer o, Object oldVal, Object newVal) {
157         return fVarHandle.compareAndExchange(o, oldVal, newVal);
158     }
159 
160     @Test
161     @IR(applyIfOr = {"UseZGC", "true", "UseG1GC", "true"},
162         failOn = IRNode.NULL_CHECK,
163         phase = CompilePhase.FINAL_CODE)
164     static boolean testCompareAndSwap(Outer o, Object oldVal, Object newVal) {
165         return fVarHandle.compareAndSet(o, oldVal, newVal);
166     }
167 
168     @Test
169     @IR(applyIfOr = {"UseZGC", "true", "UseG1GC", "true"},
170         failOn = IRNode.NULL_CHECK,
171         phase = CompilePhase.FINAL_CODE)
172     static Object testGetAndSet(Outer o, Object newVal) {
173         return fVarHandle.getAndSet(o, newVal);
174     }
175 
176     @Run(test = {"testCompareAndExchange",
177                  "testCompareAndSwap",
178                  "testGetAndSet"})
179     static void runAtomicTests() {
180         {
181             Outer o = new Outer();
182             Object oldVal = new Object();
183             Object newVal = new Object();
184             testCompareAndExchange(o, oldVal, newVal);
185         }
186         {
187             Outer o = new Outer();
188             Object oldVal = new Object();
189             Object newVal = new Object();
190             testCompareAndSwap(o, oldVal, newVal);
191         }
192         {
193             Outer o = new Outer();

  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 package compiler.gcbarriers;
 25 
 26 import compiler.lib.ir_framework.*;




 27 import java.lang.ref.SoftReference;
 28 import java.lang.ref.WeakReference;
 29 import jdk.test.lib.Asserts;
 30 import jdk.internal.misc.Unsafe;
 31 
 32 /**
 33  * @test
 34  * @summary Test that implicit null checks are generated as expected for
 35             different GC memory accesses.
 36  * @library /test/lib /
 37  * @modules java.base/jdk.internal.misc
 38  * @run driver compiler.gcbarriers.TestImplicitNullChecks
 39  */
 40 
 41 
 42 public class TestImplicitNullChecks {
 43 
 44     static class Outer {
 45         Object f;
 46     }
 47 
 48     static class OuterWithVolatileField {
 49         volatile Object f;
 50     }
 51 
 52     static final Unsafe UNSAFE = Unsafe.getUnsafe();
 53     static final long F_OFFSET;
 54     static {

 55         try {
 56             F_OFFSET = UNSAFE.objectFieldOffset(Outer.class.getDeclaredField("f"));
 57         } catch (Exception e) {
 58             throw new Error(e);
 59         }
 60     }
 61 
 62     public static void main(String[] args) {
 63         TestFramework.runWithFlags("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED",
 64                                    "-XX:CompileCommand=inline,java.lang.ref.*::*",
 65                                    "-XX:-TieredCompilation");
 66     }
 67 
 68     @Test
 69     // On AIX, implicit null checks are limited because the zero page is
 70     // readable (but not writable). See os::zero_page_read_protected().
 71     @IR(applyIfPlatform = {"aix", "false"},
 72         applyIfOr = {"UseZGC", "true", "UseG1GC", "true"},
 73         counts = {IRNode.NULL_CHECK, "1"},
 74         phase = CompilePhase.FINAL_CODE)
 75     static Object testLoad(Outer o) {
 76         return o.f;
 77     }
 78 
 79     @Test
 80     // On aarch64, volatile loads always use indirect memory operands, which
 81     // leads to a pattern that cannot be exploited by the current C2 analysis.
 82     // On PPC64, volatile loads are preceded by membar_volatile instructions,
 83     // which also inhibits the current C2 analysis.
 84     @IR(applyIfPlatformAnd = {"aarch64", "false", "ppc", "false"},

136 
137     @Run(test = {"testStore"})
138     static void runStoreTests() {
139         {
140             Outer o = new Outer();
141             Object o1 = new Object();
142             testStore(o, o1);
143         }
144     }
145 
146     @Test
147     // G1 and ZGC compare-and-exchange operations cannot be currently used to
148     // implement implicit null checks, because they expand into multiple memory
149     // access instructions that are not necessarily located at the initial
150     // instruction start address. The same holds for testCompareAndSwap and
151     // testGetAndSet below.
152     @IR(applyIfOr = {"UseZGC", "true", "UseG1GC", "true"},
153         failOn = IRNode.NULL_CHECK,
154         phase = CompilePhase.FINAL_CODE)
155     static Object testCompareAndExchange(Outer o, Object oldVal, Object newVal) {
156         return UNSAFE.compareAndExchangeReference(o, F_OFFSET, oldVal, newVal);
157     }
158 
159     @Test
160     @IR(applyIfOr = {"UseZGC", "true", "UseG1GC", "true"},
161         failOn = IRNode.NULL_CHECK,
162         phase = CompilePhase.FINAL_CODE)
163     static boolean testCompareAndSwap(Outer o, Object oldVal, Object newVal) {
164         return UNSAFE.compareAndSetReference(o, F_OFFSET, oldVal, newVal);
165     }
166 
167     @Test
168     @IR(applyIfOr = {"UseZGC", "true", "UseG1GC", "true"},
169         failOn = IRNode.NULL_CHECK,
170         phase = CompilePhase.FINAL_CODE)
171     static Object testGetAndSet(Outer o, Object newVal) {
172         return UNSAFE.getAndSetReference(o, F_OFFSET, newVal);
173     }
174 
175     @Run(test = {"testCompareAndExchange",
176                  "testCompareAndSwap",
177                  "testGetAndSet"})
178     static void runAtomicTests() {
179         {
180             Outer o = new Outer();
181             Object oldVal = new Object();
182             Object newVal = new Object();
183             testCompareAndExchange(o, oldVal, newVal);
184         }
185         {
186             Outer o = new Outer();
187             Object oldVal = new Object();
188             Object newVal = new Object();
189             testCompareAndSwap(o, oldVal, newVal);
190         }
191         {
192             Outer o = new Outer();
< prev index next >