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