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