1 /* 2 * Copyright (c) 2013, 2019, Oracle and/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 24 25 package org.graalvm.compiler.hotspot.test; 26 27 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.referentOffset; 28 29 import java.lang.ref.WeakReference; 30 31 import org.graalvm.compiler.api.test.Graal; 32 import org.graalvm.compiler.debug.DebugContext; 33 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 34 import org.graalvm.compiler.hotspot.HotSpotBackend; 35 import org.graalvm.compiler.nodes.StructuredGraph; 36 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 37 import org.graalvm.compiler.nodes.gc.G1PostWriteBarrier; 38 import org.graalvm.compiler.nodes.gc.G1PreWriteBarrier; 39 import org.graalvm.compiler.nodes.gc.G1ReferentFieldReadBarrier; 40 import org.graalvm.compiler.nodes.gc.SerialWriteBarrier; 41 import org.graalvm.compiler.nodes.memory.HeapAccess.BarrierType; 42 import org.graalvm.compiler.nodes.memory.ReadNode; 43 import org.graalvm.compiler.nodes.memory.WriteNode; 44 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode; 45 import org.graalvm.compiler.nodes.spi.LoweringTool; 46 import org.graalvm.compiler.phases.OptimisticOptimizations; 47 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 48 import org.graalvm.compiler.phases.common.GuardLoweringPhase; 49 import org.graalvm.compiler.phases.common.LoweringPhase; 50 import org.graalvm.compiler.phases.common.WriteBarrierAdditionPhase; 51 import org.graalvm.compiler.phases.common.inlining.InliningPhase; 52 import org.graalvm.compiler.phases.common.inlining.policy.InlineEverythingPolicy; 53 import org.graalvm.compiler.phases.tiers.HighTierContext; 54 import org.graalvm.compiler.phases.tiers.MidTierContext; 55 import org.graalvm.compiler.runtime.RuntimeProvider; 56 import org.junit.Assert; 57 import org.junit.Test; 58 59 import jdk.vm.ci.hotspot.HotSpotInstalledCode; 60 import jdk.vm.ci.meta.JavaConstant; 61 import jdk.vm.ci.meta.ResolvedJavaMethod; 62 import sun.misc.Unsafe; 63 64 /** 65 * The following unit tests assert the presence of write barriers for both Serial and G1 GCs. 66 * Normally, the tests check for compile time inserted barriers. However, there are the cases of 67 * unsafe loads of the java.lang.ref.Reference.referent field where runtime checks have to be 68 * performed also. For those cases, the unit tests check the presence of the compile-time inserted 69 * barriers. Concerning the runtime checks, the results of variable inputs (object types and 70 * offsets) passed as input parameters can be checked against printed output from the G1 write 71 * barrier snippets. The runtime checks have been validated offline. 72 */ 73 public class WriteBarrierAdditionTest extends HotSpotGraalCompilerTest { 74 75 private final GraalHotSpotVMConfig config = runtime().getVMConfig(); 76 77 public static class Container { 78 79 public Container a; 80 public Container b; 81 } 82 83 /** 84 * Expected 2 barriers for the Serial GC and 4 for G1 (2 pre + 2 post). 85 */ 86 @Test 87 public void test1() throws Exception { 88 testHelper("test1Snippet", (config.useG1GC) ? 4 : 2); 89 } 90 91 public static void test1Snippet() { 92 Container main = new Container(); 93 Container temp1 = new Container(); 94 Container temp2 = new Container(); 95 main.a = temp1; 96 main.b = temp2; 97 } 98 99 /** 100 * Expected 4 barriers for the Serial GC and 8 for G1 (4 pre + 4 post). 101 */ 102 @Test 103 public void test2() throws Exception { 104 testHelper("test2Snippet", config.useG1GC ? 8 : 4); 105 } 106 107 public static void test2Snippet(boolean test) { 108 Container main = new Container(); 109 Container temp1 = new Container(); 110 Container temp2 = new Container(); 111 for (int i = 0; i < 10; i++) { 112 if (test) { 113 main.a = temp1; 114 main.b = temp2; 115 } else { 116 main.a = temp2; 117 main.b = temp1; 118 } 119 } 120 } 121 122 /** 123 * Expected 4 barriers for the Serial GC and 8 for G1 (4 pre + 4 post). 124 */ 125 @Test 126 public void test3() throws Exception { 127 testHelper("test3Snippet", config.useG1GC ? 8 : 4); 128 } 129 130 public static void test3Snippet() { 131 Container[] main = new Container[10]; 132 Container temp1 = new Container(); 133 Container temp2 = new Container(); 134 for (int i = 0; i < 10; i++) { 135 main[i].a = main[i].b = temp1; 136 } 137 138 for (int i = 0; i < 10; i++) { 139 main[i].a = main[i].b = temp2; 140 } 141 } 142 143 /** 144 * Expected 2 barriers for the Serial GC and 5 for G1 (3 pre + 2 post) The (2 or 4) barriers are 145 * emitted while initializing the fields of the WeakReference instance. The extra pre barrier of 146 * G1 concerns the read of the referent field. 147 */ 148 @Test 149 public void test4() throws Exception { 150 testHelper("test4Snippet", config.useG1GC ? 5 : 2); 151 } 152 153 public static Object test4Snippet() { 154 WeakReference<Object> weakRef = new WeakReference<>(new Object()); 155 return weakRef.get(); 156 } 157 158 static WeakReference<Object> wr = new WeakReference<>(new Object()); 159 static Container con = new Container(); 160 161 /** 162 * Expected 0 barrier for the Serial GC and 1 for G1. In this test, we load the correct offset 163 * of the WeakReference object so naturally we assert the presence of the pre barrier. 164 */ 165 @Test 166 public void test5() throws Exception { 167 testHelper("test5Snippet", config.useG1GC ? 1 : 0); 168 } 169 170 private static final boolean useCompressedOops = ((HotSpotBackend) Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend()).getRuntime().getVMConfig().useCompressedOops; 171 172 public Object test5Snippet() { 173 return UNSAFE.getObject(wr, useCompressedOops ? 12L : 16L); 174 } 175 176 /** 177 * The following test concerns the runtime checks of the unsafe loads. In this test, we unsafely 178 * load the java.lang.ref.Reference.referent field so the pre barier has to be executed. 179 */ 180 @Test 181 public void test6() throws Exception { 182 test2("testUnsafeLoad", UNSAFE, wr, Long.valueOf(referentOffset(getMetaAccess())), null); 183 } 184 185 /** 186 * The following test concerns the runtime checks of the unsafe loads. In this test, we unsafely 187 * load a matching offset of a wrong object so the pre barier must not be executed. 188 */ 189 @Test 190 public void test7() throws Exception { 191 test2("testUnsafeLoad", UNSAFE, con, Long.valueOf(referentOffset(getMetaAccess())), null); 192 } 193 194 /** 195 * The following test concerns the runtime checks of the unsafe loads. In this test, we unsafely 196 * load a non-matching offset field of the java.lang.ref.Reference object so the pre barier must 197 * not be executed. 198 */ 199 @Test 200 public void test8() throws Exception { 201 test2("testUnsafeLoad", UNSAFE, wr, Long.valueOf(config.useCompressedOops ? 20 : 32), null); 202 } 203 204 /** 205 * The following test concerns the runtime checks of the unsafe loads. In this test, we unsafely 206 * load a matching offset+disp field of the java.lang.ref.Reference object so the pre barier 207 * must be executed. 208 */ 209 @Test 210 public void test10() throws Exception { 211 test2("testUnsafeLoad", UNSAFE, wr, Long.valueOf(config.useCompressedOops ? 6 : 8), Integer.valueOf(config.useCompressedOops ? 6 : 8)); 212 } 213 214 /** 215 * The following test concerns the runtime checks of the unsafe loads. In this test, we unsafely 216 * load a non-matching offset+disp field of the java.lang.ref.Reference object so the pre barier 217 * must not be executed. 218 */ 219 @Test 220 public void test9() throws Exception { 221 test2("testUnsafeLoad", UNSAFE, wr, Long.valueOf(config.useCompressedOops ? 10 : 16), Integer.valueOf(config.useCompressedOops ? 10 : 16)); 222 } 223 224 static Object[] src = new Object[1]; 225 static Object[] dst = new Object[1]; 226 227 static { 228 for (int i = 0; i < src.length; i++) { 229 src[i] = new Object(); 230 } 231 for (int i = 0; i < dst.length; i++) { 232 dst[i] = new Object(); 233 } 234 } 235 236 public static void testArrayCopy(Object a, Object b, Object c) throws Exception { 237 System.arraycopy(a, 0, b, 0, (int) c); 238 } 239 240 @Test 241 public void test11() throws Exception { 242 test2("testArrayCopy", src, dst, dst.length); 243 } 244 245 public static Object testUnsafeLoad(Unsafe theUnsafe, Object a, Object b, Object c) throws Exception { 246 final int offset = (c == null ? 0 : ((Integer) c).intValue()); 247 final long displacement = (b == null ? 0 : ((Long) b).longValue()); 248 return theUnsafe.getObject(a, offset + displacement); 249 } 250 251 private HotSpotInstalledCode getInstalledCode(String name, boolean withUnsafePrefix) throws Exception { 252 final ResolvedJavaMethod javaMethod = withUnsafePrefix ? getResolvedJavaMethod(WriteBarrierAdditionTest.class, name, Unsafe.class, Object.class, Object.class, Object.class) 253 : getResolvedJavaMethod(WriteBarrierAdditionTest.class, name, Object.class, Object.class, Object.class); 254 final HotSpotInstalledCode installedCode = (HotSpotInstalledCode) getCode(javaMethod); 255 return installedCode; 256 } 257 258 @SuppressWarnings("try") 259 private void testHelper(final String snippetName, final int expectedBarriers) throws Exception, SecurityException { 260 ResolvedJavaMethod snippet = getResolvedJavaMethod(snippetName); 261 DebugContext debug = getDebugContext(); 262 try (DebugContext.Scope s = debug.scope("WriteBarrierAdditionTest", snippet)) { 263 StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO, debug); 264 HighTierContext highContext = getDefaultHighTierContext(); 265 MidTierContext midContext = new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, graph.getProfilingInfo()); 266 new InliningPhase(new InlineEverythingPolicy(), new CanonicalizerPhase()).apply(graph, highContext); 267 new CanonicalizerPhase().apply(graph, highContext); 268 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, highContext); 269 new GuardLoweringPhase().apply(graph, midContext); 270 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, midContext); 271 new WriteBarrierAdditionPhase().apply(graph, midContext); 272 debug.dump(DebugContext.BASIC_LEVEL, graph, "After Write Barrier Addition"); 273 274 int barriers = 0; 275 if (config.useG1GC) { 276 barriers = graph.getNodes().filter(G1ReferentFieldReadBarrier.class).count() + graph.getNodes().filter(G1PreWriteBarrier.class).count() + 277 graph.getNodes().filter(G1PostWriteBarrier.class).count(); 278 } else { 279 barriers = graph.getNodes().filter(SerialWriteBarrier.class).count(); 280 } 281 if (expectedBarriers != barriers) { 282 Assert.assertEquals(getScheduledGraphString(graph), expectedBarriers, barriers); 283 } 284 for (WriteNode write : graph.getNodes().filter(WriteNode.class)) { 285 if (config.useG1GC) { 286 if (write.getBarrierType() != BarrierType.NONE) { 287 Assert.assertEquals(1, write.successors().count()); 288 Assert.assertTrue(write.next() instanceof G1PostWriteBarrier); 289 Assert.assertTrue(write.predecessor() instanceof G1PreWriteBarrier); 290 } 291 } else { 292 if (write.getBarrierType() != BarrierType.NONE) { 293 Assert.assertEquals(1, write.successors().count()); 294 Assert.assertTrue(write.next() instanceof SerialWriteBarrier); 295 } 296 } 297 } 298 299 for (ReadNode read : graph.getNodes().filter(ReadNode.class)) { 300 if (read.getBarrierType() != BarrierType.NONE) { 301 Assert.assertTrue(read.getAddress() instanceof OffsetAddressNode); 302 JavaConstant constDisp = ((OffsetAddressNode) read.getAddress()).getOffset().asJavaConstant(); 303 Assert.assertNotNull(constDisp); 304 Assert.assertEquals(referentOffset(getMetaAccess()), constDisp.asLong()); 305 Assert.assertEquals(BarrierType.WEAK_FIELD, read.getBarrierType()); 306 if (config.useG1GC) { 307 Assert.assertTrue(read.next() instanceof G1ReferentFieldReadBarrier); 308 } 309 } 310 } 311 } catch (Throwable e) { 312 throw debug.handle(e); 313 } 314 } 315 316 private void test2(final String snippet, Object... args) throws Exception { 317 HotSpotInstalledCode code = getInstalledCode(snippet, args[0] instanceof Unsafe); 318 code.executeVarargs(args); 319 } 320 }