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 org.graalvm.compiler.api.directives.GraalDirectives;
  26 import org.graalvm.compiler.bytecode.BytecodeProvider;
  27 import org.graalvm.compiler.debug.Debug;
  28 import org.graalvm.compiler.debug.DebugConfigScope;
  29 import org.graalvm.compiler.debug.GraalError;
  30 import org.graalvm.compiler.nodes.ValueNode;
  31 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  32 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  33 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  34 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
  35 import org.graalvm.compiler.replacements.Snippets;
  36 import org.graalvm.compiler.word.Word;
  37 import org.junit.Test;
  38 
  39 import jdk.vm.ci.meta.ResolvedJavaMethod;
  40 
  41 public class PointerTrackingTest extends ReplacementsTest implements Snippets {
  42 
  43     @Test
  44     public void testTracking() {
  45         Result result = executeActual(getResolvedJavaMethod("trackingSnippet"), null, new Object());
  46         assertEquals(new Result("OK", null), result);
  47     }
  48 
  49     public static String trackingSnippet(Object obj) {
  50         long trackedBeforeGC = getTrackedPointer(obj);
  51         long untrackedBeforeGC = getUntrackedPointer(obj);
  52 
  53         int i = 0;
  54         while (untrackedBeforeGC == getTrackedPointer(obj)) {
  55             System.gc();
  56             if (i++ > 100) {
  57                 return "Timeout! Object didn't move after 100 GCs.";
  58             }
  59         }
  60 
  61         long trackedAfterGC = getTrackedPointer(obj);
  62         long untrackedAfterGC = getUntrackedPointer(obj);
  63 
  64         if (untrackedBeforeGC == untrackedAfterGC) {
  65             /*
  66              * The untracked pointer isn't supposed to be updated, so it should be different before
  67              * and after GC.
  68              */
  69             return "untrackedBeforeGC == untrackedAfterGC";
  70         }
  71         if (trackedBeforeGC != trackedAfterGC) {
  72             /*
  73              * The trackedBeforeGC variable should be updated to the new location by the GC, so it
  74              * should be equal to trackedAfterGC.
  75              */
  76             return "trackedBeforeGC != trackedAfterGC";
  77         }
  78 
  79         return "OK";
  80     }
  81 
  82     @Test(expected = GraalError.class)
  83     @SuppressWarnings("try")
  84     public void testVerification() {
  85         try (DebugConfigScope scope = Debug.disableIntercept()) {
  86             compile(getResolvedJavaMethod("verificationSnippet"), null);
  87         }
  88     }
  89 
  90     public static long verificationSnippet(Object obj) {
  91         long value = getTrackedPointer(obj) * 7 + 3;
  92 
  93         /*
  94          * Ensure usage before and after GC to force the value to be alive across the safepoint.
  95          * This should lead to a compiler error, since value can not be tracked in the reference
  96          * map.
  97          */
  98         GraalDirectives.blackhole(value);
  99         System.gc();
 100         return value;
 101     }
 102 
 103     static long getTrackedPointer(@SuppressWarnings("unused") Object obj) {
 104         throw GraalError.shouldNotReachHere("should be intrinsified");
 105     }
 106 
 107     static long getUntrackedPointer(@SuppressWarnings("unused") Object obj) {
 108         throw GraalError.shouldNotReachHere("should be intrinsified");
 109     }
 110 
 111     static long getTrackedPointerIntrinsic(Object obj) {
 112         return Word.objectToTrackedPointer(obj).rawValue();
 113     }
 114 
 115     static long getUntrackedPointerIntrinsic(Object obj) {
 116         return Word.objectToUntrackedPointer(obj).rawValue();
 117     }
 118 
 119     private void register(Registration r, String fnName) {
 120         ResolvedJavaMethod intrinsic = getResolvedJavaMethod(fnName + "Intrinsic");
 121         BytecodeProvider bytecodeProvider = getSystemClassLoaderBytecodeProvider();
 122         r.register1(fnName, Object.class, new InvocationPlugin() {
 123             @Override
 124             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) {
 125                 return b.intrinsify(bytecodeProvider, targetMethod, intrinsic, receiver, new ValueNode[]{arg});
 126             }
 127         });
 128     }
 129 
 130     @Override
 131     protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
 132         Registration r = new Registration(invocationPlugins, PointerTrackingTest.class);
 133 
 134         register(r, "getTrackedPointer");
 135         register(r, "getUntrackedPointer");
 136     }
 137 }