1 /*
   2  * Copyright (c) 2015, 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.replacements.test;
  24 
  25 import java.util.Objects;
  26 
  27 import org.graalvm.compiler.api.directives.GraalDirectives;
  28 import org.graalvm.compiler.debug.DebugContext;
  29 import org.graalvm.compiler.debug.DebugContext.Scope;
  30 import org.graalvm.compiler.debug.GraalError;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.nodes.ValueNode;
  33 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  34 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  35 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  36 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
  37 import org.graalvm.compiler.replacements.Snippets;
  38 import org.graalvm.compiler.replacements.classfile.ClassfileBytecodeProvider;
  39 import org.graalvm.compiler.word.Word;
  40 import org.graalvm.compiler.word.WordCastNode;
  41 import org.junit.Assert;
  42 import org.junit.Rule;
  43 import org.junit.Test;
  44 import org.junit.rules.ExpectedException;
  45 
  46 import jdk.vm.ci.meta.ResolvedJavaMethod;
  47 
  48 /**
  49  * Tests for derived oops in reference maps.
  50  */
  51 public class DerivedOopTest extends ReplacementsTest implements Snippets {
  52 
  53     private static class Pointers {
  54         public long basePointer;
  55         public long internalPointer;
  56 
  57         public long delta() {
  58             return internalPointer - basePointer;
  59         }
  60 
  61         @Override
  62         public boolean equals(Object obj) {
  63             if (!(obj instanceof Pointers)) {
  64                 return false;
  65             }
  66 
  67             Pointers other = (Pointers) obj;
  68             return this.delta() == other.delta();
  69         }
  70 
  71         @Override
  72         public int hashCode() {
  73             return (int) delta();
  74         }
  75     }
  76 
  77     private static class Result {
  78         public Pointers beforeGC;
  79         public Pointers afterGC;
  80 
  81         Result() {
  82             beforeGC = new Pointers();
  83             afterGC = new Pointers();
  84         }
  85 
  86         @Override
  87         public int hashCode() {
  88             final int prime = 31;
  89             int result = 1;
  90             result = prime * result + ((afterGC == null) ? 0 : afterGC.hashCode());
  91             result = prime * result + ((beforeGC == null) ? 0 : beforeGC.hashCode());
  92             return result;
  93         }
  94 
  95         @Override
  96         public boolean equals(Object obj) {
  97             if (!(obj instanceof Result)) {
  98                 return false;
  99             }
 100             Result other = (Result) obj;
 101             return Objects.equals(this.beforeGC, other.beforeGC) && Objects.equals(this.afterGC, other.afterGC);
 102         }
 103     }
 104 
 105     @Test
 106     public void testFieldOffset() {
 107         // Run a couple times to encourage objects to move
 108         for (int i = 0; i < 4; i++) {
 109             Result r = new Result();
 110             test("fieldOffsetSnippet", r, 16L);
 111 
 112             Assert.assertEquals(r.beforeGC.delta(), r.afterGC.delta());
 113         }
 114     }
 115 
 116     static long getRawPointer(Object obj) {
 117         // fake implementation for interpreter
 118         return obj.hashCode();
 119     }
 120 
 121     static long getRawPointerIntrinsic(Object obj) {
 122         return Word.objectToTrackedPointer(obj).rawValue();
 123     }
 124 
 125     public static Result fieldOffsetSnippet(Result obj, long offset) {
 126         long internalPointer = getRawPointer(obj) + offset;
 127 
 128         // make sure the internal pointer is computed before the safepoint
 129         GraalDirectives.blackhole(internalPointer);
 130 
 131         obj.beforeGC.basePointer = getRawPointer(obj);
 132         obj.beforeGC.internalPointer = internalPointer;
 133 
 134         System.gc();
 135 
 136         obj.afterGC.basePointer = getRawPointer(obj);
 137         obj.afterGC.internalPointer = internalPointer;
 138 
 139         return obj;
 140     }
 141 
 142     @Rule public final ExpectedException thrown = ExpectedException.none();
 143     private static final String UNKNOWN_REFERENCE_AT_SAFEPOINT_MSG = "should not reach here: unknown reference alive across safepoint";
 144 
 145     @Test
 146     @SuppressWarnings("try")
 147     public void testFieldOffsetMergeNonLiveBasePointer() {
 148         thrown.expect(GraalError.class);
 149         thrown.expectMessage(UNKNOWN_REFERENCE_AT_SAFEPOINT_MSG);
 150         DebugContext debug = getDebugContext();
 151         try (Scope s = debug.disable()) {
 152             // Run a couple times to encourage objects to move
 153             for (int i = 0; i < 4; i++) {
 154                 Result r = new Result();
 155                 test("fieldOffsetMergeSnippet01", r, 8L, 16L);
 156                 Assert.assertEquals(r.beforeGC.delta(), r.afterGC.delta());
 157             }
 158         }
 159     }
 160 
 161     @Test
 162     public void testFieldOffsetMergeNonLiveBasePointerNotAccrossSafepoint() {
 163         // Run a couple times to encourage objects to move
 164         for (int i = 0; i < 4; i++) {
 165             Result r = new Result();
 166             test("fieldOffsetMergeSnippet02", r, 8L, 16L);
 167         }
 168     }
 169 
 170     @Test
 171     @SuppressWarnings("try")
 172     public void testFieldOffsetMergeLiveBasePointer() {
 173         thrown.expect(GraalError.class);
 174         thrown.expectMessage(UNKNOWN_REFERENCE_AT_SAFEPOINT_MSG);
 175         DebugContext debug = getDebugContext();
 176         try (Scope s = debug.disable()) {
 177             // Run a couple times to encourage objects to move
 178             for (int i = 0; i < 4; i++) {
 179                 Result r = new Result();
 180                 test("fieldOffsetMergeSnippet03", r, new Result(), new Result(), 8L, 16L);
 181                 Assert.assertEquals(r.beforeGC.delta(), r.afterGC.delta());
 182             }
 183         }
 184     }
 185 
 186     public static boolean SideEffectB;
 187     public static long SideEffect1 = 16;
 188     public static long SideEffect2 = 16;
 189     public static Object o1 = new Result();
 190     public static Object o2 = o1;
 191 
 192     public static Result fieldOffsetMergeSnippet01(Result objResult, long offsetA, long offsetB) {
 193         long internalPointer;
 194         if (SideEffectB) {
 195             internalPointer = getRawPointer(o1) + offsetA;
 196             SideEffect1 = internalPointer;
 197         } else {
 198             internalPointer = getRawPointer(o2) + offsetB;
 199             SideEffect2 = internalPointer;
 200         }
 201         GraalDirectives.controlFlowAnchor();
 202         // make sure the internal pointer is computed before the safepoint
 203         GraalDirectives.blackhole(internalPointer);
 204         objResult.beforeGC.basePointer = getRawPointer(objResult);
 205         objResult.beforeGC.internalPointer = internalPointer;
 206         System.gc();
 207         objResult.afterGC.basePointer = getRawPointer(objResult);
 208         objResult.afterGC.internalPointer = internalPointer;
 209         return objResult;
 210     }
 211 
 212     public static Result fieldOffsetMergeSnippet02(Result objResult, long offsetA, long offsetB) {
 213         long internalPointer;
 214         if (SideEffectB) {
 215             internalPointer = getRawPointer(o1) + offsetA;
 216             SideEffect1 = internalPointer;
 217         } else {
 218             internalPointer = getRawPointer(o2) + offsetB;
 219             SideEffect2 = internalPointer;
 220         }
 221         GraalDirectives.controlFlowAnchor();
 222         // make sure the internal pointer is computed before the safepoint
 223         GraalDirectives.blackhole(internalPointer);
 224         objResult.beforeGC.basePointer = getRawPointer(objResult);
 225         objResult.beforeGC.internalPointer = internalPointer;
 226         objResult.afterGC.basePointer = getRawPointer(objResult);
 227         objResult.afterGC.internalPointer = internalPointer;
 228         return objResult;
 229     }
 230 
 231     public static Result fieldOffsetMergeSnippet03(Result objResult, Result a, Result b, long offsetA, long offsetB) {
 232         long internalPointer;
 233         if (SideEffectB) {
 234             internalPointer = getRawPointer(a) + offsetA;
 235             SideEffect1 = internalPointer;
 236         } else {
 237             internalPointer = getRawPointer(b) + offsetB;
 238             SideEffect2 = internalPointer;
 239         }
 240         GraalDirectives.controlFlowAnchor();
 241         // make sure the internal pointer is computed before the safepoint
 242         GraalDirectives.blackhole(internalPointer);
 243         objResult.beforeGC.basePointer = getRawPointer(objResult);
 244         objResult.beforeGC.internalPointer = internalPointer;
 245         System.gc();
 246         objResult.afterGC.basePointer = getRawPointer(objResult);
 247         objResult.afterGC.internalPointer = internalPointer;
 248         return objResult;
 249     }
 250 
 251     @Override
 252     protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
 253         Registration r = new Registration(invocationPlugins, DerivedOopTest.class);
 254         ClassfileBytecodeProvider bytecodeProvider = getSystemClassLoaderBytecodeProvider();
 255 
 256         ResolvedJavaMethod intrinsic = getResolvedJavaMethod("getRawPointerIntrinsic");
 257         r.register1("getRawPointer", Object.class, new InvocationPlugin() {
 258             @Override
 259             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) {
 260                 return b.intrinsify(bytecodeProvider, targetMethod, intrinsic, receiver, new ValueNode[]{arg});
 261             }
 262         });
 263         super.registerInvocationPlugins(invocationPlugins);
 264     }
 265 
 266     @Override
 267     protected boolean checkHighTierGraph(StructuredGraph graph) {
 268         assert graph.getNodes().filter(WordCastNode.class).count() > 0 : "DerivedOopTest.toLong should be intrinsified";
 269         return super.checkHighTierGraph(graph);
 270     }
 271 }