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.junit.Assert; 28 import org.junit.Test; 29 30 import org.graalvm.compiler.api.directives.GraalDirectives; 31 import org.graalvm.compiler.core.test.GraalCompilerTest; 32 import org.graalvm.compiler.nodes.StructuredGraph; 33 import org.graalvm.compiler.nodes.ValueNode; 34 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins; 35 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; 36 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin; 37 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration; 38 import org.graalvm.compiler.replacements.Snippets; 39 import org.graalvm.compiler.word.Word; 40 import org.graalvm.compiler.word.nodes.WordCastNode; 41 42 import jdk.vm.ci.meta.ResolvedJavaMethod; 43 44 /** 45 * Tests for derived oops in reference maps. 46 */ 47 public class DerivedOopTest extends GraalCompilerTest implements Snippets { 48 49 private static class Pointers { 50 public long basePointer; 51 public long internalPointer; 52 53 public long delta() { 54 return internalPointer - basePointer; 55 } 56 57 @Override 58 public boolean equals(Object obj) { 59 if (!(obj instanceof Pointers)) { 60 return false; 61 } 62 63 Pointers other = (Pointers) obj; 64 return this.delta() == other.delta(); 65 } 66 67 @Override 68 public int hashCode() { 69 return (int) delta(); 70 } 71 } 72 73 private static class Result { 74 public Pointers beforeGC; 75 public Pointers afterGC; 76 77 Result() { 78 beforeGC = new Pointers(); 79 afterGC = new Pointers(); 80 } 81 82 @Override 83 public int hashCode() { 84 final int prime = 31; 85 int result = 1; 86 result = prime * result + ((afterGC == null) ? 0 : afterGC.hashCode()); 87 result = prime * result + ((beforeGC == null) ? 0 : beforeGC.hashCode()); 88 return result; 89 } 90 91 @Override 92 public boolean equals(Object obj) { 93 if (!(obj instanceof Result)) { 94 return false; 95 } 96 Result other = (Result) obj; 97 return Objects.equals(this.beforeGC, other.beforeGC) && Objects.equals(this.afterGC, other.afterGC); 98 } 99 } 100 101 @Test 102 public void testFieldOffset() { 103 // Run a couple times to encourage objects to move 104 for (int i = 0; i < 4; i++) { 105 Result r = new Result(); 106 test("fieldOffsetSnippet", r, 16L); 107 108 Assert.assertEquals(r.beforeGC.delta(), r.afterGC.delta()); 109 } 110 } 111 112 static long getRawPointer(Object obj) { 113 // fake implementation for interpreter 114 return obj.hashCode(); 115 } 116 117 static long getRawPointerIntrinsic(Object obj) { 118 return Word.objectToTrackedPointer(obj).rawValue(); 119 } 120 121 public static Result fieldOffsetSnippet(Result obj, long offset) { 122 long internalPointer = getRawPointer(obj) + offset; 123 124 // make sure the internal pointer is computed before the safepoint 125 GraalDirectives.blackhole(internalPointer); 126 127 obj.beforeGC.basePointer = getRawPointer(obj); 128 obj.beforeGC.internalPointer = internalPointer; 129 130 System.gc(); 131 132 obj.afterGC.basePointer = getRawPointer(obj); 133 obj.afterGC.internalPointer = internalPointer; 134 135 return obj; 136 } 137 138 @Override 139 protected Plugins getDefaultGraphBuilderPlugins() { 140 Plugins plugins = super.getDefaultGraphBuilderPlugins(); 141 Registration r = new Registration(plugins.getInvocationPlugins(), DerivedOopTest.class); 142 143 ResolvedJavaMethod intrinsic = getResolvedJavaMethod("getRawPointerIntrinsic"); 144 r.register1("getRawPointer", Object.class, new InvocationPlugin() { 145 @Override 146 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) { 147 return b.intrinsify(getReplacements().getReplacementBytecodeProvider(), targetMethod, intrinsic, receiver, new ValueNode[]{arg}); 148 } 149 }); 150 151 return plugins; 152 } 153 154 @Override 155 protected boolean checkHighTierGraph(StructuredGraph graph) { 156 assert graph.getNodes().filter(WordCastNode.class).count() > 0 : "DerivedOopTest.toLong should be intrinsified"; 157 return super.checkHighTierGraph(graph); 158 } 159 }