1 /*
  2  * Copyright (c) 2020, 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 /*
 26  * @test
 27  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAdaptVarHandles
 28  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
 29  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAdaptVarHandles
 30  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
 31  */
 32 
 33 import jdk.incubator.foreign.MemoryHandles;
 34 import jdk.incubator.foreign.MemoryLayout;
 35 import jdk.incubator.foreign.MemoryLayouts;
 36 import jdk.incubator.foreign.MemorySegment;
 37 import jdk.incubator.foreign.ResourceScope;
 38 import jdk.incubator.foreign.ValueLayout;
 39 import org.testng.annotations.*;
 40 import static org.testng.Assert.*;
 41 
 42 import java.lang.invoke.MethodHandle;
 43 import java.lang.invoke.MethodHandles;
 44 import java.lang.invoke.MethodType;
 45 import java.lang.invoke.VarHandle;
 46 import java.util.List;
 47 
 48 public class TestAdaptVarHandles {
 49 
 50     static MethodHandle S2I;
 51     static MethodHandle I2S;
 52     static MethodHandle CTX_I2S;
 53     static MethodHandle O2I;
 54     static MethodHandle I2O;
 55     static MethodHandle S2L;
 56     static MethodHandle S2L_EX;
 57     static MethodHandle S2I_EX;
 58     static MethodHandle I2S_EX;
 59     static MethodHandle BASE_ADDR;
 60     static MethodHandle SUM_OFFSETS;
 61     static MethodHandle VOID_FILTER;
 62 
 63     static {
 64         try {
 65             S2I = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToInt", MethodType.methodType(int.class, String.class));
 66             I2S = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "intToString", MethodType.methodType(String.class, int.class));
 67             CTX_I2S = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "ctxIntToString",
 68                     MethodType.methodType(String.class, String.class, String.class, int.class));
 69             O2I = MethodHandles.explicitCastArguments(S2I, MethodType.methodType(int.class, Object.class));
 70             I2O = MethodHandles.explicitCastArguments(I2S, MethodType.methodType(Object.class, int.class));
 71             S2L = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToLong", MethodType.methodType(long.class, String.class));
 72             S2L_EX = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToLongException", MethodType.methodType(long.class, String.class));
 73             BASE_ADDR = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "baseAddress", MethodType.methodType(MemorySegment.class, MemorySegment.class));
 74             SUM_OFFSETS = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "sumOffsets", MethodType.methodType(long.class, long.class, long.class));
 75             VOID_FILTER = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "void_filter", MethodType.methodType(void.class, String.class));
 76 
 77             MethodHandle s2i_ex = MethodHandles.throwException(int.class, Throwable.class);
 78             s2i_ex = MethodHandles.insertArguments(s2i_ex, 0, new Throwable());
 79             S2I_EX = MethodHandles.dropArguments(s2i_ex, 0, String.class);
 80 
 81             MethodHandle i2s_ex = MethodHandles.throwException(String.class, Throwable.class);
 82             i2s_ex = MethodHandles.insertArguments(i2s_ex, 0, new Throwable());
 83             I2S_EX = MethodHandles.dropArguments(i2s_ex, 0, int.class);
 84         } catch (Throwable ex) {
 85             throw new ExceptionInInitializerError();
 86         }
 87     }
 88 
 89     static final VarHandle intHandleIndexed = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT)
 90             .varHandle(int.class, MemoryLayout.PathElement.sequenceElement());
 91 
 92     static final VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
 93 
 94     static final VarHandle floatHandle = MemoryLayouts.JAVA_FLOAT.varHandle(float.class);
 95 
 96     @Test
 97     public void testFilterValue() throws Throwable {
 98         ValueLayout layout = MemoryLayouts.JAVA_INT;
 99         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
100         VarHandle intHandle = layout.varHandle(int.class);
101         VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, S2I, I2S);
102         i2SHandle.set(segment, "1");
103         String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
104         assertEquals(oldValue, "1");
105         String value = (String)i2SHandle.get(segment);
106         assertEquals(value, "43");
107         boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
108         assertTrue(swapped);
109         oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
110         assertEquals(oldValue, "12");
111         value = (String)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
112         assertEquals(value, "42");
113     }
114 
115     @Test
116     public void testFilterValueComposite() throws Throwable {
117         ValueLayout layout = MemoryLayouts.JAVA_INT;
118         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
119         VarHandle intHandle = layout.varHandle(int.class);
120         MethodHandle CTX_S2I = MethodHandles.dropArguments(S2I, 0, String.class, String.class);
121         VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, CTX_S2I, CTX_I2S);
122         i2SHandle = MemoryHandles.insertCoordinates(i2SHandle, 1, "a", "b");
123         i2SHandle.set(segment, "1");
124         String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
125         assertEquals(oldValue, "ab1");
126         String value = (String)i2SHandle.get(segment);
127         assertEquals(value, "ab43");
128         boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
129         assertTrue(swapped);
130         oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
131         assertEquals(oldValue, "ab12");
132         value = (String)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
133         assertEquals(value, "ab42");
134     }
135 
136     @Test
137     public void testFilterValueLoose() throws Throwable {
138         ValueLayout layout = MemoryLayouts.JAVA_INT;
139         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
140         VarHandle intHandle = layout.varHandle(int.class);
141         VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, O2I, I2O);
142         i2SHandle.set(segment, "1");
143         String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
144         assertEquals(oldValue, "1");
145         String value = (String)i2SHandle.get(segment);
146         assertEquals(value, "43");
147         boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
148         assertTrue(swapped);
149         oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
150         assertEquals(oldValue, "12");
151         value = (String)(Object)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
152         assertEquals(value, "42");
153     }
154 
155     @Test(expectedExceptions = IllegalArgumentException.class)
156     public void testBadFilterCarrier() {
157         MemoryHandles.filterValue(floatHandle, S2I, I2S);
158     }
159 
160     @Test(expectedExceptions = IllegalArgumentException.class)
161     public void testBadFilterUnboxArity() {
162         VarHandle floatHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
163         MemoryHandles.filterValue(floatHandle, S2I.bindTo(""), I2S);
164     }
165 
166     @Test(expectedExceptions = IllegalArgumentException.class)
167     public void testBadFilterBoxArity() {
168         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
169         MemoryHandles.filterValue(intHandle, S2I, I2S.bindTo(42));
170     }
171 
172     @Test(expectedExceptions = IllegalArgumentException.class)
173     public void testBadFilterBoxPrefixCoordinates() {
174         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
175         MemoryHandles.filterValue(intHandle,
176                 MethodHandles.dropArguments(S2I, 1, int.class),
177                 MethodHandles.dropArguments(I2S, 1, long.class));
178     }
179 
180     @Test(expectedExceptions = IllegalArgumentException.class)
181     public void testBadFilterBoxException() {
182         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
183         MemoryHandles.filterValue(intHandle, I2S, S2L_EX);
184     }
185 
186     @Test(expectedExceptions = IllegalArgumentException.class)
187     public void testBadFilterUnboxException() {
188         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
189         MemoryHandles.filterValue(intHandle, S2L_EX, I2S);
190     }
191 
192     @Test(expectedExceptions = IllegalArgumentException.class)
193     public void testBadFilterBoxHandleException() {
194         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
195         MemoryHandles.filterValue(intHandle, S2I, I2S_EX);
196     }
197 
198     @Test(expectedExceptions = IllegalArgumentException.class)
199     public void testBadFilterUnboxHandleException() {
200         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
201         MemoryHandles.filterValue(intHandle, S2I_EX, I2S);
202     }
203 
204     @Test
205     public void testFilterCoordinates() throws Throwable {
206         ValueLayout layout = MemoryLayouts.JAVA_INT;
207         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
208         VarHandle intHandle_longIndex = MemoryHandles.filterCoordinates(intHandleIndexed, 0, BASE_ADDR, S2L);
209         intHandle_longIndex.set(segment, "0", 1);
210         int oldValue = (int)intHandle_longIndex.getAndAdd(segment, "0", 42);
211         assertEquals(oldValue, 1);
212         int value = (int)intHandle_longIndex.get(segment, "0");
213         assertEquals(value, 43);
214         boolean swapped = (boolean)intHandle_longIndex.compareAndSet(segment, "0", 43, 12);
215         assertTrue(swapped);
216         oldValue = (int)intHandle_longIndex.compareAndExchange(segment, "0", 12, 42);
217         assertEquals(oldValue, 12);
218         value = (int)intHandle_longIndex.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, "0");
219         assertEquals(value, 42);
220     }
221 
222     @Test(expectedExceptions = IllegalArgumentException.class)
223     public void testBadFilterCoordinatesNegativePos() {
224         MemoryHandles.filterCoordinates(intHandle, -1, SUM_OFFSETS);
225     }
226 
227     @Test(expectedExceptions = IllegalArgumentException.class)
228     public void testBadFilterCoordinatesPosTooBig() {
229         MemoryHandles.filterCoordinates(intHandle, 1, SUM_OFFSETS);
230     }
231 
232     @Test(expectedExceptions = IllegalArgumentException.class)
233     public void testBadFilterCoordinatesWrongFilterType() {
234         MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2I);
235     }
236 
237     @Test(expectedExceptions = IllegalArgumentException.class)
238     public void testBadFilterCoordinatesWrongFilterException() {
239         MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2L_EX);
240     }
241 
242     @Test(expectedExceptions = IllegalArgumentException.class)
243     public void testBadFilterCoordinatesTooManyFilters() {
244         MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2L, S2L);
245     }
246 
247     @Test
248     public void testInsertCoordinates() throws Throwable {
249         ValueLayout layout = MemoryLayouts.JAVA_INT;
250         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
251         VarHandle intHandle_longIndex = MemoryHandles.insertCoordinates(intHandleIndexed, 0, segment, 0L);
252         intHandle_longIndex.set(1);
253         int oldValue = (int)intHandle_longIndex.getAndAdd(42);
254         assertEquals(oldValue, 1);
255         int value = (int)intHandle_longIndex.get();
256         assertEquals(value, 43);
257         boolean swapped = (boolean)intHandle_longIndex.compareAndSet(43, 12);
258         assertTrue(swapped);
259         oldValue = (int)intHandle_longIndex.compareAndExchange(12, 42);
260         assertEquals(oldValue, 12);
261         value = (int)intHandle_longIndex.toMethodHandle(VarHandle.AccessMode.GET).invokeExact();
262         assertEquals(value, 42);
263     }
264 
265     @Test(expectedExceptions = IllegalArgumentException.class)
266     public void testBadInsertCoordinatesNegativePos() {
267         MemoryHandles.insertCoordinates(intHandle, -1, 42);
268     }
269 
270     @Test(expectedExceptions = IllegalArgumentException.class)
271     public void testBadInsertCoordinatesPosTooBig() {
272         MemoryHandles.insertCoordinates(intHandle, 1, 42);
273     }
274 
275     @Test(expectedExceptions = ClassCastException.class)
276     public void testBadInsertCoordinatesWrongCoordinateType() {
277         MemoryHandles.insertCoordinates(intHandleIndexed, 1, "Hello");
278     }
279 
280     @Test(expectedExceptions = IllegalArgumentException.class)
281     public void testBadInsertCoordinatesTooManyValues() {
282         MemoryHandles.insertCoordinates(intHandleIndexed, 1, 0L, 0L);
283     }
284 
285     @Test
286     public void testPermuteCoordinates() throws Throwable {
287         ValueLayout layout = MemoryLayouts.JAVA_INT;
288         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
289         VarHandle intHandle_swap = MemoryHandles.permuteCoordinates(intHandleIndexed,
290                 List.of(long.class, MemorySegment.class), 1, 0);
291         intHandle_swap.set(0L, segment, 1);
292         int oldValue = (int)intHandle_swap.getAndAdd(0L, segment, 42);
293         assertEquals(oldValue, 1);
294         int value = (int)intHandle_swap.get(0L, segment);
295         assertEquals(value, 43);
296         boolean swapped = (boolean)intHandle_swap.compareAndSet(0L, segment, 43, 12);
297         assertTrue(swapped);
298         oldValue = (int)intHandle_swap.compareAndExchange(0L, segment, 12, 42);
299         assertEquals(oldValue, 12);
300         value = (int)intHandle_swap.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(0L, segment);
301         assertEquals(value, 42);
302     }
303 
304     @Test(expectedExceptions = IllegalArgumentException.class)
305     public void testBadPermuteCoordinatesTooManyCoordinates() {
306         MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), new int[2]);
307     }
308 
309     @Test(expectedExceptions = IllegalArgumentException.class)
310     public void testBadPermuteCoordinatesTooFewCoordinates() {
311         MemoryHandles.permuteCoordinates(intHandle, List.of());
312     }
313 
314     @Test(expectedExceptions = IllegalArgumentException.class)
315     public void testBadPermuteCoordinatesIndexTooBig() {
316         MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), 3);
317     }
318 
319     @Test(expectedExceptions = IllegalArgumentException.class)
320     public void testBadPermuteCoordinatesIndexTooSmall() {
321         MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), -1);
322     }
323 
324     @Test
325     public void testCollectCoordinates() throws Throwable {
326         ValueLayout layout = MemoryLayouts.JAVA_INT;
327         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
328         VarHandle intHandle_sum = MemoryHandles.collectCoordinates(intHandleIndexed, 1, SUM_OFFSETS);
329         intHandle_sum.set(segment, -2L, 2L, 1);
330         int oldValue = (int)intHandle_sum.getAndAdd(segment, -2L, 2L, 42);
331         assertEquals(oldValue, 1);
332         int value = (int)intHandle_sum.get(segment, -2L, 2L);
333         assertEquals(value, 43);
334         boolean swapped = (boolean)intHandle_sum.compareAndSet(segment, -2L, 2L, 43, 12);
335         assertTrue(swapped);
336         oldValue = (int)intHandle_sum.compareAndExchange(segment, -2L, 2L, 12, 42);
337         assertEquals(oldValue, 12);
338         value = (int)intHandle_sum.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, -2L, 2L);
339         assertEquals(value, 42);
340     }
341 
342     @Test(expectedExceptions = IllegalArgumentException.class)
343     public void testBadCollectCoordinatesNegativePos() {
344         MemoryHandles.collectCoordinates(intHandle, -1, SUM_OFFSETS);
345     }
346 
347     @Test(expectedExceptions = IllegalArgumentException.class)
348     public void testBadCollectCoordinatesPosTooBig() {
349         MemoryHandles.collectCoordinates(intHandle, 1, SUM_OFFSETS);
350     }
351 
352     @Test(expectedExceptions = IllegalArgumentException.class)
353     public void testBadCollectCoordinatesWrongFilterType() {
354         MemoryHandles.collectCoordinates(intHandle, 0, SUM_OFFSETS);
355     }
356 
357     @Test(expectedExceptions = IllegalArgumentException.class)
358     public void testBadCollectCoordinatesWrongVoidFilterType() {
359         MemoryHandles.collectCoordinates(intHandle, 0, VOID_FILTER);
360     }
361 
362     @Test(expectedExceptions = IllegalArgumentException.class)
363     public void testBadCollectCoordinatesWrongFilterException() {
364         MemoryHandles.collectCoordinates(intHandle, 0, S2L_EX);
365     }
366 
367     @Test
368     public void testDropCoordinates() throws Throwable {
369         ValueLayout layout = MemoryLayouts.JAVA_INT;
370         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
371         VarHandle intHandle_dummy = MemoryHandles.dropCoordinates(intHandleIndexed, 1, float.class, String.class);
372         intHandle_dummy.set(segment, 1f, "hello", 0L, 1);
373         int oldValue = (int)intHandle_dummy.getAndAdd(segment, 1f, "hello", 0L, 42);
374         assertEquals(oldValue, 1);
375         int value = (int)intHandle_dummy.get(segment, 1f, "hello", 0L);
376         assertEquals(value, 43);
377         boolean swapped = (boolean)intHandle_dummy.compareAndSet(segment, 1f, "hello", 0L, 43, 12);
378         assertTrue(swapped);
379         oldValue = (int)intHandle_dummy.compareAndExchange(segment, 1f, "hello", 0L, 12, 42);
380         assertEquals(oldValue, 12);
381         value = (int)intHandle_dummy.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, 1f, "hello", 0L);
382         assertEquals(value, 42);
383     }
384 
385     @Test(expectedExceptions = IllegalArgumentException.class)
386     public void testBadDropCoordinatesNegativePos() {
387         MemoryHandles.dropCoordinates(intHandle, -1);
388     }
389 
390     @Test(expectedExceptions = IllegalArgumentException.class)
391     public void testBadDropCoordinatesPosTooBig() {
392         MemoryHandles.dropCoordinates(intHandle, 2);
393     }
394 
395     //helper methods
396 
397     static int stringToInt(String s) {
398         return Integer.valueOf(s);
399     }
400 
401     static String intToString(int i) {
402         return String.valueOf(i);
403     }
404 
405     static long stringToLong(String s) {
406         return Long.valueOf(s);
407     }
408 
409     static long stringToLongException(String s) throws Throwable {
410         return Long.valueOf(s);
411     }
412 
413     static MemorySegment baseAddress(MemorySegment segment) {
414         return segment;
415     }
416 
417     static long sumOffsets(long l1, long l2) {
418         return l1 + l2;
419     }
420 
421     static void void_filter(String s) { }
422 
423     static String ctxIntToString(String a, String b, int i) {
424         return a + b + String.valueOf(i);
425     }
426 }