# HG changeset patch # User Roland Schatz # Date 1438352622 -7200 # Node ID ee4ceec1a0dba7c2fca69dda2bba03232966b669 # Parent 65eab59081061a85aabc9da7185484e45c86626a Unit tests for Word.objectTo(Un)trackedPointer. diff -r 65eab5908106 -r ee4ceec1a0db graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotReferenceMapBuilder.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotReferenceMapBuilder.java Fri Jul 31 15:07:27 2015 +0200 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotReferenceMapBuilder.java Fri Jul 31 16:23:42 2015 +0200 @@ -85,7 +85,7 @@ LIRKind kind = obj.getLIRKind(); int bytes = bytesPerElement(kind); if (kind.isUnknownReference()) { - throw JVMCIError.unimplemented("derived references not yet implemented"); + throw JVMCIError.shouldNotReachHere("unknown reference alive across safepoint"); } else { Location base = null; if (kind.isDerivedReference()) { diff -r 65eab5908106 -r ee4ceec1a0db graal/com.oracle.graal.replacements.test/src/com/oracle/graal/replacements/test/PointerTrackingTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.replacements.test/src/com/oracle/graal/replacements/test/PointerTrackingTest.java Fri Jul 31 16:23:42 2015 +0200 @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.replacements.test; + +import jdk.internal.jvmci.common.*; +import jdk.internal.jvmci.meta.*; + +import org.junit.*; + +import com.oracle.graal.api.directives.*; +import com.oracle.graal.compiler.test.*; +import com.oracle.graal.debug.*; +import com.oracle.graal.graphbuilderconf.GraphBuilderConfiguration.Plugins; +import com.oracle.graal.graphbuilderconf.*; +import com.oracle.graal.graphbuilderconf.InvocationPlugins.Registration; +import com.oracle.graal.nodes.*; +import com.oracle.graal.replacements.*; +import com.oracle.graal.word.*; + +public class PointerTrackingTest extends GraalCompilerTest implements Snippets { + + @Test + public void testTracking() { + Result result = executeActual(getResolvedJavaMethod("trackingSnippet"), null, new Object()); + assertEquals(new Result("OK", null), result); + } + + public static String trackingSnippet(Object obj) { + long trackedBeforeGC = getTrackedPointer(obj); + long untrackedBeforeGC = getUntrackedPointer(obj); + + int i = 0; + while (untrackedBeforeGC == getTrackedPointer(obj)) { + System.gc(); + if (i++ > 100) { + return "Timeout! Object didn't move after 100 GCs."; + } + } + + long trackedAfterGC = getTrackedPointer(obj); + long untrackedAfterGC = getUntrackedPointer(obj); + + if (untrackedBeforeGC == untrackedAfterGC) { + /* + * The untracked pointer isn't supposed to be updated, so it should be different before + * and after GC. + */ + return "untrackedBeforeGC == untrackedAfterGC"; + } + if (trackedBeforeGC != trackedAfterGC) { + /* + * The trackedBeforeGC variable should be updated to the new location by the GC, so it + * should be equal to trackedAfterGC. + */ + return "trackedBeforeGC != trackedAfterGC"; + } + + return "OK"; + } + + @Test(expected = JVMCIError.class) + public void testVerification() { + try (DebugConfigScope scope = Debug.disableIntercept()) { + compile(getResolvedJavaMethod("verificationSnippet"), null); + } + } + + public static long verificationSnippet(Object obj) { + long value = getTrackedPointer(obj) * 7 + 3; + + /* + * Ensure usage before and after GC to force the value to be alive across the safepoint. + * This should lead to a compiler error, since value can not be tracked in the reference + * map. + */ + GraalDirectives.blackhole(value); + System.gc(); + return value; + } + + static long getTrackedPointer(@SuppressWarnings("unused") Object obj) { + throw JVMCIError.shouldNotReachHere("should be intrinsified"); + } + + static long getUntrackedPointer(@SuppressWarnings("unused") Object obj) { + throw JVMCIError.shouldNotReachHere("should be intrinsified"); + } + + static long getTrackedPointerIntrinsic(Object obj) { + return Word.objectToTrackedPointer(obj).rawValue(); + } + + static long getUntrackedPointerIntrinsic(Object obj) { + return Word.objectToUntrackedPointer(obj).rawValue(); + } + + private void register(Registration r, String fnName) { + ResolvedJavaMethod intrinsic = getResolvedJavaMethod(fnName + "Intrinsic"); + r.register1(fnName, Object.class, new InvocationPlugin() { + public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) { + b.intrinsify(targetMethod, intrinsic, new ValueNode[]{arg}); + return true; + } + }); + } + + @Override + protected Plugins getDefaultGraphBuilderPlugins() { + Plugins plugins = super.getDefaultGraphBuilderPlugins(); + Registration r = new Registration(plugins.getInvocationPlugins(), PointerTrackingTest.class); + + register(r, "getTrackedPointer"); + register(r, "getUntrackedPointer"); + + return plugins; + } +}