|
1 /* |
|
2 * Copyright (c) 2018, 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.jdk9; |
|
24 |
|
25 import jdk.vm.ci.amd64.AMD64; |
|
26 import jdk.vm.ci.code.TargetDescription; |
|
27 import jdk.vm.ci.meta.ResolvedJavaMethod; |
|
28 import org.graalvm.compiler.api.test.Graal; |
|
29 import org.graalvm.compiler.replacements.test.MethodSubstitutionTest; |
|
30 import org.graalvm.compiler.runtime.RuntimeProvider; |
|
31 import org.graalvm.compiler.test.AddExports; |
|
32 import org.junit.Test; |
|
33 |
|
34 @AddExports("java.base/jdk.internal.misc") |
|
35 public class UnsafeReplacementsTest extends MethodSubstitutionTest { |
|
36 |
|
37 // See GR-9819. |
|
38 @SuppressWarnings("unused") ResolvedJavaMethod method = null; |
|
39 |
|
40 static class Container { |
|
41 public volatile boolean booleanField; |
|
42 public volatile byte byteField = 17; |
|
43 public volatile char charField = 1025; |
|
44 public volatile short shortField = 2232; |
|
45 public volatile int intField = 0xcafebabe; |
|
46 public volatile long longField = 0xdedababafafaL; |
|
47 public volatile float floatField = 0.125f; |
|
48 public volatile double doubleField = 0.125; |
|
49 public volatile Object objectField = dummyValue; |
|
50 } |
|
51 |
|
52 static jdk.internal.misc.Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); |
|
53 static Container dummyValue = new Container(); |
|
54 static Container newDummyValue = new Container(); |
|
55 static long booleanOffset; |
|
56 static long byteOffset; |
|
57 static long charOffset; |
|
58 static long shortOffset; |
|
59 static long intOffset; |
|
60 static long longOffset; |
|
61 static long floatOffset; |
|
62 static long doubleOffset; |
|
63 static long objectOffset; |
|
64 |
|
65 static { |
|
66 try { |
|
67 booleanOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("booleanField")); |
|
68 byteOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("byteField")); |
|
69 charOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("charField")); |
|
70 shortOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("shortField")); |
|
71 intOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("intField")); |
|
72 longOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("longField")); |
|
73 floatOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("floatField")); |
|
74 doubleOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("doubleField")); |
|
75 objectOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("objectField")); |
|
76 } catch (NoSuchFieldException e) { |
|
77 throw new RuntimeException(e); |
|
78 } |
|
79 } |
|
80 |
|
81 public static boolean unsafeCompareAndSetBoolean() { |
|
82 Container container = new Container(); |
|
83 return unsafe.compareAndSetBoolean(container, booleanOffset, false, true); |
|
84 } |
|
85 |
|
86 public static boolean unsafeCompareAndSetByte() { |
|
87 Container container = new Container(); |
|
88 return unsafe.compareAndSetByte(container, byteOffset, (byte) 17, (byte) 121); |
|
89 } |
|
90 |
|
91 public static boolean unsafeCompareAndSetChar() { |
|
92 Container container = new Container(); |
|
93 return unsafe.compareAndSetChar(container, charOffset, (char) 1025, (char) 1777); |
|
94 } |
|
95 |
|
96 public static boolean unsafeCompareAndSetShort() { |
|
97 Container container = new Container(); |
|
98 return unsafe.compareAndSetShort(container, shortOffset, (short) 2232, (short) 12111); |
|
99 } |
|
100 |
|
101 public static boolean unsafeCompareAndSetInt() { |
|
102 Container container = new Container(); |
|
103 return unsafe.compareAndSetInt(container, intOffset, 0xcafebabe, 0xbabefafa); |
|
104 } |
|
105 |
|
106 public static boolean unsafeCompareAndSetLong() { |
|
107 Container container = new Container(); |
|
108 return unsafe.compareAndSetLong(container, longOffset, 0xdedababafafaL, 0xfafacecafafadedaL); |
|
109 } |
|
110 |
|
111 public static boolean unsafeCompareAndSetFloat() { |
|
112 Container container = new Container(); |
|
113 return unsafe.compareAndSetFloat(container, floatOffset, 0.125f, 0.25f); |
|
114 } |
|
115 |
|
116 public static boolean unsafeCompareAndSetDouble() { |
|
117 Container container = new Container(); |
|
118 return unsafe.compareAndSetDouble(container, doubleOffset, 0.125, 0.25); |
|
119 } |
|
120 |
|
121 public static boolean unsafeCompareAndSetObject() { |
|
122 Container container = new Container(); |
|
123 return unsafe.compareAndSetObject(container, objectOffset, dummyValue, newDummyValue); |
|
124 } |
|
125 |
|
126 public static boolean unsafeCompareAndExchangeBoolean() { |
|
127 Container container = new Container(); |
|
128 return unsafe.compareAndExchangeBoolean(container, booleanOffset, false, true); |
|
129 } |
|
130 |
|
131 public static byte unsafeCompareAndExchangeByte() { |
|
132 Container container = new Container(); |
|
133 return unsafe.compareAndExchangeByte(container, byteOffset, (byte) 17, (byte) 31); |
|
134 } |
|
135 |
|
136 public static char unsafeCompareAndExchangeChar() { |
|
137 Container container = new Container(); |
|
138 return unsafe.compareAndExchangeChar(container, charOffset, (char) 1025, (char) 4502); |
|
139 } |
|
140 |
|
141 public static short unsafeCompareAndExchangeShort() { |
|
142 Container container = new Container(); |
|
143 return unsafe.compareAndExchangeShort(container, shortOffset, (short) 2232, (short) 8121); |
|
144 } |
|
145 |
|
146 public static int unsafeCompareAndExchangeInt() { |
|
147 Container container = new Container(); |
|
148 return unsafe.compareAndExchangeInt(container, intOffset, 0xcafebabe, 0xbabefafa); |
|
149 } |
|
150 |
|
151 public static long unsafeCompareAndExchangeLong() { |
|
152 Container container = new Container(); |
|
153 return unsafe.compareAndExchangeLong(container, longOffset, 0xdedababafafaL, 0xfafacecafafadedaL); |
|
154 } |
|
155 |
|
156 public static float unsafeCompareAndExchangeFloat() { |
|
157 Container container = new Container(); |
|
158 return unsafe.compareAndExchangeFloat(container, floatOffset, 0.125f, 0.25f); |
|
159 } |
|
160 |
|
161 public static double unsafeCompareAndExchangeDouble() { |
|
162 Container container = new Container(); |
|
163 return unsafe.compareAndExchangeDouble(container, doubleOffset, 0.125, 0.25); |
|
164 } |
|
165 |
|
166 public static Object unsafeCompareAndExchangeObject() { |
|
167 Container container = new Container(); |
|
168 return unsafe.compareAndExchangeObject(container, objectOffset, dummyValue, newDummyValue); |
|
169 } |
|
170 |
|
171 @Test |
|
172 public void testCompareAndSet() { |
|
173 TargetDescription target = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getTarget(); |
|
174 if (target.arch instanceof AMD64) { |
|
175 testGraph("unsafeCompareAndSetBoolean"); |
|
176 testGraph("unsafeCompareAndSetByte"); |
|
177 testGraph("unsafeCompareAndSetChar"); |
|
178 testGraph("unsafeCompareAndSetShort"); |
|
179 testGraph("unsafeCompareAndSetInt"); |
|
180 testGraph("unsafeCompareAndSetLong"); |
|
181 testGraph("unsafeCompareAndSetFloat"); |
|
182 testGraph("unsafeCompareAndSetDouble"); |
|
183 testGraph("unsafeCompareAndSetObject"); |
|
184 testGraph("unsafeCompareAndExchangeBoolean"); |
|
185 testGraph("unsafeCompareAndExchangeByte"); |
|
186 testGraph("unsafeCompareAndExchangeChar"); |
|
187 testGraph("unsafeCompareAndExchangeShort"); |
|
188 testGraph("unsafeCompareAndExchangeInt"); |
|
189 testGraph("unsafeCompareAndExchangeLong"); |
|
190 testGraph("unsafeCompareAndExchangeFloat"); |
|
191 testGraph("unsafeCompareAndExchangeDouble"); |
|
192 testGraph("unsafeCompareAndExchangeObject"); |
|
193 } |
|
194 test("unsafeCompareAndSetBoolean"); |
|
195 test("unsafeCompareAndSetByte"); |
|
196 test("unsafeCompareAndSetChar"); |
|
197 test("unsafeCompareAndSetShort"); |
|
198 test("unsafeCompareAndSetInt"); |
|
199 test("unsafeCompareAndSetLong"); |
|
200 test("unsafeCompareAndSetFloat"); |
|
201 test("unsafeCompareAndSetDouble"); |
|
202 test("unsafeCompareAndSetObject"); |
|
203 test("unsafeCompareAndExchangeBoolean"); |
|
204 test("unsafeCompareAndExchangeByte"); |
|
205 test("unsafeCompareAndExchangeChar"); |
|
206 test("unsafeCompareAndExchangeShort"); |
|
207 test("unsafeCompareAndExchangeInt"); |
|
208 test("unsafeCompareAndExchangeLong"); |
|
209 test("unsafeCompareAndExchangeFloat"); |
|
210 test("unsafeCompareAndExchangeDouble"); |
|
211 test("unsafeCompareAndExchangeObject"); |
|
212 } |
|
213 |
|
214 public static int unsafeGetAndAddByte() { |
|
215 Container container = new Container(); |
|
216 return unsafe.getAndAddByte(container, byteOffset, (byte) 2); |
|
217 } |
|
218 |
|
219 public static int unsafeGetAndAddChar() { |
|
220 Container container = new Container(); |
|
221 return unsafe.getAndAddChar(container, charOffset, (char) 250); |
|
222 } |
|
223 |
|
224 public static int unsafeGetAndAddShort() { |
|
225 Container container = new Container(); |
|
226 return unsafe.getAndAddShort(container, shortOffset, (short) 1250); |
|
227 } |
|
228 |
|
229 public static int unsafeGetAndAddInt() { |
|
230 Container container = new Container(); |
|
231 return unsafe.getAndAddInt(container, intOffset, 104501); |
|
232 } |
|
233 |
|
234 public static long unsafeGetAndAddLong() { |
|
235 Container container = new Container(); |
|
236 return unsafe.getAndAddLong(container, longOffset, 0x123456abcdL); |
|
237 } |
|
238 |
|
239 @Test |
|
240 public void testGetAndAdd() { |
|
241 TargetDescription target = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getTarget(); |
|
242 if (target.arch instanceof AMD64) { |
|
243 testGraph("unsafeGetAndAddByte"); |
|
244 testGraph("unsafeGetAndAddChar"); |
|
245 testGraph("unsafeGetAndAddShort"); |
|
246 testGraph("unsafeGetAndAddInt"); |
|
247 testGraph("unsafeGetAndAddLong"); |
|
248 } |
|
249 test("unsafeGetAndAddByte"); |
|
250 test("unsafeGetAndAddChar"); |
|
251 test("unsafeGetAndAddShort"); |
|
252 test("unsafeGetAndAddInt"); |
|
253 test("unsafeGetAndAddLong"); |
|
254 } |
|
255 |
|
256 public static boolean unsafeGetAndSetBoolean() { |
|
257 Container container = new Container(); |
|
258 return unsafe.getAndSetBoolean(container, booleanOffset, true); |
|
259 } |
|
260 |
|
261 public static byte unsafeGetAndSetByte() { |
|
262 Container container = new Container(); |
|
263 return unsafe.getAndSetByte(container, byteOffset, (byte) 129); |
|
264 } |
|
265 |
|
266 public static char unsafeGetAndSetChar() { |
|
267 Container container = new Container(); |
|
268 return unsafe.getAndSetChar(container, charOffset, (char) 21111); |
|
269 } |
|
270 |
|
271 public static short unsafeGetAndSetShort() { |
|
272 Container container = new Container(); |
|
273 return unsafe.getAndSetShort(container, shortOffset, (short) 21111); |
|
274 } |
|
275 |
|
276 public static int unsafeGetAndSetInt() { |
|
277 Container container = new Container(); |
|
278 return unsafe.getAndSetInt(container, intOffset, 0x1234af); |
|
279 } |
|
280 |
|
281 public static long unsafeGetAndSetLong() { |
|
282 Container container = new Container(); |
|
283 return unsafe.getAndSetLong(container, longOffset, 0x12345678abL); |
|
284 } |
|
285 |
|
286 public static Object unsafeGetAndSetObject() { |
|
287 Container container = new Container(); |
|
288 container.objectField = null; |
|
289 Container other = new Container(); |
|
290 return unsafe.getAndSetObject(container, objectOffset, other); |
|
291 } |
|
292 |
|
293 @Test |
|
294 public void testGetAndSet() { |
|
295 TargetDescription target = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getTarget(); |
|
296 if (target.arch instanceof AMD64) { |
|
297 testGraph("unsafeGetAndSetBoolean"); |
|
298 testGraph("unsafeGetAndSetByte"); |
|
299 testGraph("unsafeGetAndSetChar"); |
|
300 testGraph("unsafeGetAndSetShort"); |
|
301 testGraph("unsafeGetAndSetInt"); |
|
302 testGraph("unsafeGetAndSetLong"); |
|
303 testGraph("unsafeGetAndSetObject"); |
|
304 } |
|
305 test("unsafeGetAndSetBoolean"); |
|
306 test("unsafeGetAndSetByte"); |
|
307 test("unsafeGetAndSetChar"); |
|
308 test("unsafeGetAndSetShort"); |
|
309 test("unsafeGetAndSetInt"); |
|
310 test("unsafeGetAndSetLong"); |
|
311 test("unsafeGetAndSetObject"); |
|
312 } |
|
313 } |