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.reflect.Field; 26 27 import org.graalvm.compiler.nodes.StructuredGraph; 28 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 29 import org.graalvm.compiler.options.OptionValues; 30 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 31 import org.graalvm.compiler.phases.tiers.PhaseContext; 32 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase; 33 import org.junit.Test; 34 35 import jdk.vm.ci.code.InstalledCode; 36 import jdk.vm.ci.meta.ResolvedJavaMethod; 37 38 public class UnsafeVirtualizationTest extends GraalCompilerTest { 39 40 public static class Base { 41 /* 42 * This padding ensure that the size of the Base class ends up as a multiple of 8, which 43 * makes the first field of the subclass 8-byte aligned. 44 */ 45 double padding; 46 } 47 48 public static class A extends Base { 49 int f1; 50 int f2; 51 } 52 53 private static final long AF1Offset; 54 private static final long AF2Offset; 55 static { 56 long o1 = -1; 57 long o2 = -1; 58 try { 59 Field f1 = A.class.getDeclaredField("f1"); 60 Field f2 = A.class.getDeclaredField("f2"); 61 o1 = UNSAFE.objectFieldOffset(f1); 62 o2 = UNSAFE.objectFieldOffset(f2); 63 } catch (NoSuchFieldException | SecurityException e) { 64 throw new AssertionError(e); 65 } 66 AF1Offset = o1; 67 AF2Offset = o2; 68 } 69 70 public static int unsafeSnippet1(double i1) { 71 A a = new A(); 72 UNSAFE.putDouble(a, AF1Offset, i1); 73 return UNSAFE.getInt(a, AF1Offset) + UNSAFE.getInt(a, AF2Offset); 74 } 75 76 public static long unsafeSnippet2a(int i1) { 77 A a = new A(); 78 UNSAFE.putDouble(a, AF1Offset, i1); 79 a.f1 = i1; 80 return UNSAFE.getLong(a, AF1Offset); 81 } 82 83 public static long unsafeSnippet2b(int i1) { 84 A a = new A(); 85 UNSAFE.putDouble(a, AF1Offset, i1); 86 a.f2 = i1; 87 return UNSAFE.getLong(a, AF1Offset); 88 } 89 90 public static long unsafeSnippet3a(int i1) { 91 A a = new A(); 92 UNSAFE.putDouble(a, AF1Offset, i1); 93 UNSAFE.putInt(a, AF1Offset, i1); 94 return UNSAFE.getLong(a, AF1Offset); 95 } 96 97 public static long unsafeSnippet3b(int i1) { 98 A a = new A(); 99 UNSAFE.putDouble(a, AF1Offset, i1); 100 UNSAFE.putInt(a, AF2Offset, i1); 101 return UNSAFE.getLong(a, AF1Offset); 102 } 103 104 public static int unsafeSnippet4(double i1) { 105 A a = new A(); 106 UNSAFE.putDouble(a, AF1Offset, i1); 107 UNSAFE.putDouble(a, AF1Offset, i1); 108 return UNSAFE.getInt(a, AF1Offset) + UNSAFE.getInt(a, AF2Offset); 109 } 110 111 @Test 112 public void testUnsafePEA01() { 113 testPartialEscapeReadElimination("unsafeSnippet1", false, 1.0); 114 testPartialEscapeReadElimination("unsafeSnippet1", true, 1.0); 115 } 116 117 @Test 118 public void testUnsafePEA02() { 119 testPartialEscapeReadElimination("unsafeSnippet2a", false, 1); 120 testPartialEscapeReadElimination("unsafeSnippet2a", true, 1); 121 122 testPartialEscapeReadElimination("unsafeSnippet2b", false, 1); 123 testPartialEscapeReadElimination("unsafeSnippet2b", true, 1); 124 } 125 126 @Test 127 public void testUnsafePEA03() { 128 testPartialEscapeReadElimination("unsafeSnippet3a", false, 1); 129 testPartialEscapeReadElimination("unsafeSnippet3a", true, 1); 130 131 testPartialEscapeReadElimination("unsafeSnippet3b", false, 1); 132 testPartialEscapeReadElimination("unsafeSnippet3b", true, 1); 133 } 134 135 @Test 136 public void testUnsafePEA04() { 137 testPartialEscapeReadElimination("unsafeSnippet4", false, 1.0); 138 testPartialEscapeReadElimination("unsafeSnippet4", true, 1.0); 139 } 140 141 public void testPartialEscapeReadElimination(String snippet, boolean canonicalizeBefore, Object... args) { 142 assert AF1Offset % 8 == 0 : "First of the two int-fields must be 8-byte aligned"; 143 144 ResolvedJavaMethod method = getResolvedJavaMethod(snippet); 145 StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO); 146 OptionValues options = graph.getOptions(); 147 PhaseContext context = getDefaultHighTierContext(); 148 CanonicalizerPhase canonicalizer = new CanonicalizerPhase(); 149 if (canonicalizeBefore) { 150 canonicalizer.apply(graph, context); 151 } 152 Result r = executeExpected(method, null, args); 153 new PartialEscapePhase(true, true, canonicalizer, null, options).apply(graph, context); 154 try { 155 InstalledCode code = getCode(method, graph); 156 Object result = code.executeVarargs(args); 157 assertEquals(r, new Result(result, null)); 158 } catch (Throwable e) { 159 assertFalse(true, e.toString()); 160 } 161 } 162 }