46344
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
46344
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
46344
|
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;
|
55509
|
31 |
import org.graalvm.compiler.nodes.spi.CoreProviders;
|
46344
|
32 |
import org.graalvm.compiler.options.OptionValues;
|
|
33 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
34 |
import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
|
|
35 |
import org.junit.Test;
|
|
36 |
|
47798
|
37 |
import jdk.vm.ci.code.InstalledCode;
|
|
38 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
39 |
|
46344
|
40 |
public class UnsafeVirtualizationTest extends GraalCompilerTest {
|
|
41 |
|
47798
|
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 {
|
46344
|
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 |
|
47798
|
72 |
public static int unsafeSnippet1(double i1) {
|
46344
|
73 |
A a = new A();
|
47798
|
74 |
UNSAFE.putDouble(a, AF1Offset, i1);
|
46344
|
75 |
return UNSAFE.getInt(a, AF1Offset) + UNSAFE.getInt(a, AF2Offset);
|
|
76 |
}
|
|
77 |
|
47798
|
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) {
|
46344
|
93 |
A a = new A();
|
47798
|
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);
|
46344
|
111 |
}
|
|
112 |
|
|
113 |
@Test
|
|
114 |
public void testUnsafePEA01() {
|
47798
|
115 |
testPartialEscapeReadElimination("unsafeSnippet1", false, 1.0);
|
|
116 |
testPartialEscapeReadElimination("unsafeSnippet1", true, 1.0);
|
46344
|
117 |
}
|
|
118 |
|
|
119 |
@Test
|
|
120 |
public void testUnsafePEA02() {
|
47798
|
121 |
testPartialEscapeReadElimination("unsafeSnippet2a", false, 1);
|
|
122 |
testPartialEscapeReadElimination("unsafeSnippet2a", true, 1);
|
|
123 |
|
|
124 |
testPartialEscapeReadElimination("unsafeSnippet2b", false, 1);
|
|
125 |
testPartialEscapeReadElimination("unsafeSnippet2b", true, 1);
|
46344
|
126 |
}
|
|
127 |
|
47798
|
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);
|
46344
|
148 |
OptionValues options = graph.getOptions();
|
55509
|
149 |
CoreProviders context = getDefaultHighTierContext();
|
46344
|
150 |
CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
|
|
151 |
if (canonicalizeBefore) {
|
|
152 |
canonicalizer.apply(graph, context);
|
|
153 |
}
|
47798
|
154 |
Result r = executeExpected(method, null, args);
|
46344
|
155 |
new PartialEscapePhase(true, true, canonicalizer, null, options).apply(graph, context);
|
47798
|
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 |
}
|
46344
|
163 |
}
|
|
164 |
}
|