1 /* 2 * Copyright (c) 2016, 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 package org.graalvm.compiler.core.test; 24 25 import java.lang.ref.Reference; 26 import java.lang.ref.ReferenceQueue; 27 import java.lang.ref.WeakReference; 28 29 import org.junit.Test; 30 31 import org.graalvm.compiler.core.common.LocationIdentity; 32 import org.graalvm.compiler.graph.Node; 33 import org.graalvm.compiler.loop.LoopEx; 34 import org.graalvm.compiler.loop.LoopsData; 35 import org.graalvm.compiler.nodes.FieldLocationIdentity; 36 import org.graalvm.compiler.nodes.StructuredGraph; 37 import org.graalvm.compiler.nodes.memory.Access; 38 39 import jdk.vm.ci.meta.ResolvedJavaField; 40 41 public class ReferenceGetLoopTest extends GraalCompilerTest { 42 43 @Override 44 protected boolean checkMidTierGraph(StructuredGraph graph) { 45 final LoopsData loops = new LoopsData(graph); 46 boolean found = false; 47 for (LoopEx loop : loops.loops()) { 48 for (Node node : loop.inside().nodes()) { 49 if (node instanceof Access) { 50 Access access = (Access) node; 51 LocationIdentity location = access.getLocationIdentity(); 52 if (location instanceof FieldLocationIdentity) { 53 ResolvedJavaField field = ((FieldLocationIdentity) location).getField(); 54 if (field.getName().equals("referent") && field.getDeclaringClass().equals(getMetaAccess().lookupJavaType(Reference.class))) { 55 found = true; 56 } 57 } 58 } 59 } 60 } 61 if (!found) { 62 assertTrue(false, "Reference.referent not found in loop: " + getCanonicalGraphString(graph, true, false)); 63 } 64 return true; 65 } 66 67 public volatile Object referent; 68 public final FinalWeakReference<Object> ref; 69 public final ReferenceQueue<Object> refQueue; 70 71 /* 72 * Ensure that the Reference.get invoke is statically bindable. 73 */ 74 public static final class FinalWeakReference<T> extends WeakReference<T> { 75 public FinalWeakReference(T referent, ReferenceQueue<? super T> q) { 76 super(referent, q); 77 } 78 } 79 80 public ReferenceGetLoopTest() { 81 referent = new Object(); 82 refQueue = new ReferenceQueue<>(); 83 ref = new FinalWeakReference<>(referent, refQueue); 84 } 85 86 @Test 87 public void test() { 88 getCode(getMetaAccess().lookupJavaMethod(getMethod("testSnippet"))); 89 } 90 91 public void testSnippet() { 92 while (ref.get() != null) { 93 // burn! 94 } 95 } 96 }