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