43972
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
|
43972
|
25 |
package org.graalvm.compiler.core.test.ea;
|
|
26 |
|
47798
|
27 |
import java.nio.ByteBuffer;
|
43972
|
28 |
|
47798
|
29 |
import org.graalvm.compiler.api.directives.GraalDirectives;
|
|
30 |
import org.graalvm.compiler.graph.Graph;
|
|
31 |
import org.graalvm.compiler.graph.Node;
|
|
32 |
import org.graalvm.compiler.nodes.NamedLocationIdentity;
|
|
33 |
import org.graalvm.compiler.nodes.PhiNode;
|
|
34 |
import org.graalvm.compiler.nodes.ValuePhiNode;
|
|
35 |
import org.graalvm.compiler.nodes.calc.UnpackEndianHalfNode;
|
|
36 |
import org.graalvm.compiler.nodes.extended.RawLoadNode;
|
|
37 |
import org.graalvm.compiler.nodes.extended.RawStoreNode;
|
|
38 |
import org.graalvm.compiler.nodes.extended.UnsafeAccessNode;
|
|
39 |
import org.graalvm.compiler.nodes.java.LoadFieldNode;
|
43972
|
40 |
import org.junit.Assert;
|
|
41 |
import org.junit.Test;
|
|
42 |
|
47798
|
43 |
import jdk.vm.ci.meta.JavaConstant;
|
|
44 |
import jdk.vm.ci.meta.JavaKind;
|
|
45 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
43972
|
46 |
|
|
47 |
public class UnsafeEATest extends EATestBase {
|
|
48 |
|
|
49 |
public static int zero = 0;
|
|
50 |
|
47798
|
51 |
@Override
|
|
52 |
protected void testEscapeAnalysis(String snippet, JavaConstant expectedConstantResult, boolean iterativeEscapeAnalysis) {
|
|
53 |
// Exercise both a graph containing UnsafeAccessNodes and one which has been possibly been
|
|
54 |
// canonicalized into AccessFieldNodes.
|
|
55 |
testingUnsafe = true;
|
|
56 |
super.testEscapeAnalysis(snippet, expectedConstantResult, iterativeEscapeAnalysis);
|
|
57 |
testingUnsafe = false;
|
|
58 |
super.testEscapeAnalysis(snippet, expectedConstantResult, iterativeEscapeAnalysis);
|
|
59 |
if (expectedConstantResult != null) {
|
|
60 |
// Check that a compiled version of this method returns the same value if we expect a
|
|
61 |
// constant result.
|
|
62 |
ResolvedJavaMethod method = getResolvedJavaMethod(snippet);
|
|
63 |
JavaKind[] javaKinds = method.getSignature().toParameterKinds(false);
|
|
64 |
Object[] args = new Object[javaKinds.length];
|
|
65 |
int i = 0;
|
|
66 |
for (JavaKind k : javaKinds) {
|
|
67 |
args[i++] = JavaConstant.defaultForKind(k).asBoxedPrimitive();
|
|
68 |
}
|
|
69 |
Result result = executeExpected(method, null, args);
|
|
70 |
assertTrue(result.returnValue.equals(expectedConstantResult.asBoxedPrimitive()));
|
|
71 |
}
|
|
72 |
}
|
|
73 |
|
|
74 |
@Override
|
|
75 |
protected void canonicalizeGraph() {
|
|
76 |
if (testingUnsafe) {
|
|
77 |
// For testing purposes we'd like to ensure that our raw unsafe operations stay as
|
|
78 |
// unsafe nodes, so force them to appear to have LocationIdentity.any to disable
|
|
79 |
// transformation into field access nodes.
|
|
80 |
for (Node node : graph.getNodes().filter(x -> x instanceof UnsafeAccessNode).snapshot()) {
|
|
81 |
if (node instanceof RawStoreNode) {
|
|
82 |
RawStoreNode store = (RawStoreNode) node;
|
|
83 |
RawStoreNode newStore = graph.add(new RawStoreNode(store.object(), store.offset(), store.value(), store.accessKind(), NamedLocationIdentity.any(),
|
|
84 |
store.needsBarrier(), store.stateAfter(), true));
|
|
85 |
graph.replaceFixedWithFixed(store, newStore);
|
|
86 |
} else if (node instanceof RawLoadNode) {
|
|
87 |
RawLoadNode load = (RawLoadNode) node;
|
|
88 |
RawLoadNode newLoad = graph.add(new RawLoadNode(load.object(), load.offset(), load.accessKind(), NamedLocationIdentity.any(),
|
|
89 |
true));
|
|
90 |
graph.replaceFixedWithFixed(load, newLoad);
|
|
91 |
}
|
|
92 |
}
|
|
93 |
}
|
|
94 |
super.canonicalizeGraph();
|
|
95 |
}
|
|
96 |
|
|
97 |
@Override
|
|
98 |
protected void postEACanonicalizeGraph() {
|
|
99 |
// Simplify any UnpackEndianHalfNode so we end up with constants.
|
|
100 |
Graph.Mark mark = graph.getMark();
|
|
101 |
for (UnpackEndianHalfNode node : graph.getNodes().filter(UnpackEndianHalfNode.class)) {
|
|
102 |
node.lower(getTarget().arch.getByteOrder());
|
|
103 |
}
|
58877
|
104 |
createCanonicalizerPhase().applyIncremental(graph, context, mark);
|
47798
|
105 |
}
|
|
106 |
|
|
107 |
private boolean testingUnsafe;
|
|
108 |
|
43972
|
109 |
@Test
|
|
110 |
public void testSimpleInt() {
|
|
111 |
testEscapeAnalysis("testSimpleIntSnippet", JavaConstant.forInt(101), false);
|
|
112 |
}
|
|
113 |
|
|
114 |
public static int testSimpleIntSnippet() {
|
|
115 |
TestClassInt x = new TestClassInt();
|
59095
|
116 |
UNSAFE.putInt(x, TestClassInt.fieldOffset1, 101);
|
|
117 |
return UNSAFE.getInt(x, TestClassInt.fieldOffset1);
|
43972
|
118 |
}
|
|
119 |
|
|
120 |
@Test
|
|
121 |
public void testMaterializedInt() {
|
|
122 |
test("testMaterializedIntSnippet");
|
|
123 |
}
|
|
124 |
|
|
125 |
public static TestClassInt testMaterializedIntSnippet() {
|
|
126 |
TestClassInt x = new TestClassInt();
|
59095
|
127 |
UNSAFE.putInt(x, TestClassInt.fieldOffset1, 101);
|
43972
|
128 |
return x;
|
|
129 |
}
|
|
130 |
|
|
131 |
@Test
|
|
132 |
public void testSimpleDouble() {
|
|
133 |
testEscapeAnalysis("testSimpleDoubleSnippet", JavaConstant.forDouble(10.1), false);
|
|
134 |
}
|
|
135 |
|
|
136 |
public static double testSimpleDoubleSnippet() {
|
|
137 |
TestClassInt x = new TestClassInt();
|
59095
|
138 |
UNSAFE.putDouble(x, TestClassInt.fieldOffset1, 10.1);
|
|
139 |
return UNSAFE.getDouble(x, TestClassInt.fieldOffset1);
|
43972
|
140 |
}
|
|
141 |
|
|
142 |
@Test
|
47798
|
143 |
public void testSimpleDoubleOverwriteWithInt() {
|
|
144 |
testEscapeAnalysis("testSimpleDoubleOverwriteWithIntSnippet", JavaConstant.forInt(10), false);
|
|
145 |
}
|
|
146 |
|
|
147 |
public static int testSimpleDoubleOverwriteWithIntSnippet() {
|
|
148 |
TestClassInt x = new TestClassInt();
|
59095
|
149 |
UNSAFE.putDouble(x, TestClassInt.fieldOffset1, 10.1);
|
|
150 |
UNSAFE.putInt(x, TestClassInt.fieldOffset1, 10);
|
|
151 |
return UNSAFE.getInt(x, TestClassInt.fieldOffset1);
|
47798
|
152 |
}
|
|
153 |
|
|
154 |
@Test
|
|
155 |
public void testSimpleDoubleOverwriteWithSecondInt() {
|
|
156 |
ByteBuffer bb = ByteBuffer.allocate(8).order(getTarget().arch.getByteOrder());
|
|
157 |
bb.putDouble(10.1);
|
|
158 |
int value = bb.getInt(4);
|
|
159 |
|
|
160 |
testEscapeAnalysis("testSimpleDoubleOverwriteWithSecondIntSnippet", JavaConstant.forInt(value), false);
|
|
161 |
}
|
|
162 |
|
|
163 |
public static int testSimpleDoubleOverwriteWithSecondIntSnippet() {
|
|
164 |
TestClassInt x = new TestClassInt();
|
59095
|
165 |
UNSAFE.putDouble(x, TestClassInt.fieldOffset1, 10.1);
|
|
166 |
UNSAFE.putInt(x, TestClassInt.fieldOffset1, 10);
|
|
167 |
return UNSAFE.getInt(x, TestClassInt.fieldOffset2);
|
47798
|
168 |
}
|
|
169 |
|
|
170 |
@Test
|
|
171 |
public void testSimpleDoubleOverwriteWithFirstInt() {
|
|
172 |
ByteBuffer bb = ByteBuffer.allocate(8).order(getTarget().arch.getByteOrder());
|
|
173 |
bb.putDouble(10.1);
|
|
174 |
int value = bb.getInt(0);
|
|
175 |
|
|
176 |
testEscapeAnalysis("testSimpleDoubleOverwriteWithFirstIntSnippet", JavaConstant.forInt(value), false);
|
|
177 |
}
|
|
178 |
|
|
179 |
public static int testSimpleDoubleOverwriteWithFirstIntSnippet() {
|
|
180 |
TestClassInt x = new TestClassInt();
|
59095
|
181 |
UNSAFE.putDouble(x, TestClassInt.fieldOffset1, 10.1);
|
|
182 |
UNSAFE.putInt(x, TestClassInt.fieldOffset2, 10);
|
|
183 |
return UNSAFE.getInt(x, TestClassInt.fieldOffset1);
|
47798
|
184 |
}
|
|
185 |
|
|
186 |
@Test
|
|
187 |
public void testSimpleLongOverwriteWithSecondInt() {
|
|
188 |
ByteBuffer bb = ByteBuffer.allocate(8).order(getTarget().arch.getByteOrder());
|
|
189 |
bb.putLong(0, 0x1122334455667788L);
|
|
190 |
int value = bb.getInt(4);
|
|
191 |
|
|
192 |
testEscapeAnalysis("testSimpleLongOverwriteWithSecondIntSnippet", JavaConstant.forInt(value), false);
|
|
193 |
}
|
|
194 |
|
|
195 |
public static int testSimpleLongOverwriteWithSecondIntSnippet() {
|
|
196 |
TestClassInt x = new TestClassInt();
|
59095
|
197 |
UNSAFE.putLong(x, TestClassInt.fieldOffset1, 0x1122334455667788L);
|
|
198 |
UNSAFE.putInt(x, TestClassInt.fieldOffset1, 10);
|
|
199 |
return UNSAFE.getInt(x, TestClassInt.fieldOffset2);
|
47798
|
200 |
}
|
|
201 |
|
|
202 |
@Test
|
|
203 |
public void testSimpleLongOverwriteWithFirstInt() {
|
|
204 |
ByteBuffer bb = ByteBuffer.allocate(8).order(getTarget().arch.getByteOrder());
|
|
205 |
bb.putLong(0, 0x1122334455667788L);
|
|
206 |
int value = bb.getInt(0);
|
|
207 |
|
|
208 |
testEscapeAnalysis("testSimpleLongOverwriteWithFirstIntSnippet", JavaConstant.forInt(value), false);
|
|
209 |
}
|
|
210 |
|
|
211 |
public static int testSimpleLongOverwriteWithFirstIntSnippet() {
|
|
212 |
TestClassInt x = new TestClassInt();
|
59095
|
213 |
UNSAFE.putLong(x, TestClassInt.fieldOffset1, 0x1122334455667788L);
|
|
214 |
UNSAFE.putInt(x, TestClassInt.fieldOffset2, 10);
|
|
215 |
return UNSAFE.getInt(x, TestClassInt.fieldOffset1);
|
47798
|
216 |
}
|
|
217 |
|
|
218 |
@Test
|
43972
|
219 |
public void testMergedDouble() {
|
|
220 |
testEscapeAnalysis("testMergedDoubleSnippet", null, false);
|
|
221 |
Assert.assertEquals(1, returnNodes.size());
|
|
222 |
Assert.assertTrue(returnNodes.get(0).result() instanceof ValuePhiNode);
|
|
223 |
PhiNode phi = (PhiNode) returnNodes.get(0).result();
|
|
224 |
Assert.assertTrue(phi.valueAt(0) instanceof LoadFieldNode);
|
|
225 |
Assert.assertTrue(phi.valueAt(1) instanceof LoadFieldNode);
|
|
226 |
}
|
|
227 |
|
|
228 |
public static double testMergedDoubleSnippet(boolean a) {
|
|
229 |
TestClassInt x;
|
|
230 |
if (a) {
|
|
231 |
x = new TestClassInt(0, 0);
|
59095
|
232 |
UNSAFE.putDouble(x, TestClassInt.fieldOffset1, doubleField);
|
43972
|
233 |
} else {
|
|
234 |
x = new TestClassInt();
|
59095
|
235 |
UNSAFE.putDouble(x, TestClassInt.fieldOffset1, doubleField2);
|
43972
|
236 |
}
|
59095
|
237 |
return UNSAFE.getDouble(x, TestClassInt.fieldOffset1);
|
43972
|
238 |
}
|
|
239 |
|
47798
|
240 |
static class ExtendedTestClassInt extends TestClassInt {
|
|
241 |
public long l;
|
|
242 |
}
|
|
243 |
|
|
244 |
@Test
|
|
245 |
public void testMergedVirtualObjects() {
|
|
246 |
testEscapeAnalysis("testMergedVirtualObjectsSnippet", null, false);
|
|
247 |
}
|
|
248 |
|
|
249 |
public static TestClassInt testMergedVirtualObjectsSnippet(int value) {
|
|
250 |
TestClassInt x;
|
|
251 |
if (value == 1) {
|
|
252 |
x = new TestClassInt();
|
59095
|
253 |
UNSAFE.putDouble(x, TestClassInt.fieldOffset1, 10);
|
47798
|
254 |
} else {
|
|
255 |
x = new TestClassInt();
|
59095
|
256 |
UNSAFE.putInt(x, TestClassInt.fieldOffset1, 0);
|
47798
|
257 |
}
|
59095
|
258 |
UNSAFE.putInt(x, TestClassInt.fieldOffset1, 0);
|
47798
|
259 |
if (value == 2) {
|
59095
|
260 |
UNSAFE.putInt(x, TestClassInt.fieldOffset2, 0);
|
47798
|
261 |
}
|
|
262 |
GraalDirectives.deoptimizeAndInvalidate();
|
|
263 |
return x;
|
|
264 |
}
|
|
265 |
|
43972
|
266 |
@Test
|
|
267 |
public void testMaterializedDouble() {
|
|
268 |
test("testMaterializedDoubleSnippet");
|
|
269 |
}
|
|
270 |
|
|
271 |
public static TestClassInt testMaterializedDoubleSnippet() {
|
|
272 |
TestClassInt x = new TestClassInt();
|
59095
|
273 |
UNSAFE.putDouble(x, TestClassInt.fieldOffset1, 10.1);
|
43972
|
274 |
return x;
|
|
275 |
}
|
|
276 |
|
|
277 |
@Test
|
|
278 |
public void testDeoptDoubleVar() {
|
|
279 |
test("testDeoptDoubleVarSnippet");
|
|
280 |
}
|
|
281 |
|
|
282 |
public static double doubleField = 10.1e99;
|
|
283 |
public static double doubleField2;
|
|
284 |
|
|
285 |
public static TestClassInt testDeoptDoubleVarSnippet() {
|
|
286 |
TestClassInt x = new TestClassInt();
|
59095
|
287 |
UNSAFE.putDouble(x, TestClassInt.fieldOffset1, doubleField);
|
43972
|
288 |
doubleField2 = 123;
|
|
289 |
try {
|
59095
|
290 |
doubleField = ((int) UNSAFE.getDouble(x, TestClassInt.fieldOffset1)) / zero;
|
43972
|
291 |
} catch (RuntimeException e) {
|
|
292 |
return x;
|
|
293 |
}
|
|
294 |
return x;
|
|
295 |
}
|
|
296 |
|
|
297 |
@Test
|
|
298 |
public void testDeoptDoubleConstant() {
|
|
299 |
test("testDeoptDoubleConstantSnippet");
|
|
300 |
}
|
|
301 |
|
|
302 |
public static TestClassInt testDeoptDoubleConstantSnippet() {
|
|
303 |
TestClassInt x = new TestClassInt();
|
59095
|
304 |
UNSAFE.putDouble(x, TestClassInt.fieldOffset1, 10.123);
|
43972
|
305 |
doubleField2 = 123;
|
|
306 |
try {
|
59095
|
307 |
doubleField = ((int) UNSAFE.getDouble(x, TestClassInt.fieldOffset1)) / zero;
|
43972
|
308 |
} catch (RuntimeException e) {
|
|
309 |
return x;
|
|
310 |
}
|
|
311 |
return x;
|
|
312 |
}
|
|
313 |
|
|
314 |
@Test
|
|
315 |
public void testDeoptLongVar() {
|
|
316 |
test("testDeoptLongVarSnippet");
|
|
317 |
}
|
|
318 |
|
|
319 |
public static long longField = 0x133443218aaaffffL;
|
|
320 |
public static long longField2;
|
|
321 |
|
|
322 |
public static TestClassInt testDeoptLongVarSnippet() {
|
|
323 |
TestClassInt x = new TestClassInt();
|
59095
|
324 |
UNSAFE.putLong(x, TestClassInt.fieldOffset1, longField);
|
43972
|
325 |
longField2 = 123;
|
|
326 |
try {
|
59095
|
327 |
longField = UNSAFE.getLong(x, TestClassInt.fieldOffset1) / zero;
|
43972
|
328 |
} catch (RuntimeException e) {
|
|
329 |
return x;
|
|
330 |
}
|
|
331 |
return x;
|
|
332 |
}
|
|
333 |
|
|
334 |
@Test
|
|
335 |
public void testDeoptLongConstant() {
|
|
336 |
test("testDeoptLongConstantSnippet");
|
|
337 |
}
|
|
338 |
|
|
339 |
public static TestClassInt testDeoptLongConstantSnippet() {
|
|
340 |
TestClassInt x = new TestClassInt();
|
59095
|
341 |
UNSAFE.putLong(x, TestClassInt.fieldOffset1, 0x2222222210123L);
|
43972
|
342 |
longField2 = 123;
|
|
343 |
try {
|
59095
|
344 |
longField = UNSAFE.getLong(x, TestClassInt.fieldOffset1) / zero;
|
43972
|
345 |
} catch (RuntimeException e) {
|
|
346 |
return x;
|
|
347 |
}
|
|
348 |
return x;
|
|
349 |
}
|
46762
|
350 |
|
43972
|
351 |
}
|