1 /*
  2  *  Copyright (c) 2020, 2021, 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  * @test
 26  * @requires os.arch=="amd64" | os.arch=="x86_64" | os.arch=="aarch64"
 27  * @run testng/othervm
 28  *   -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=true
 29  *   -Djdk.internal.foreign.ProgrammableInvoker.USE_INTRINSICS=true
 30  *   --enable-native-access=ALL-UNNAMED
 31  *   -Xbatch
 32  *   TestIntrinsics
 33  */
 34 
 35 import jdk.incubator.foreign.CLinker;
 36 import jdk.incubator.foreign.FunctionDescriptor;
 37 
 38 import java.lang.invoke.MethodHandle;
 39 import java.lang.invoke.MethodType;
 40 import java.util.ArrayList;
 41 import java.util.List;
 42 
 43 import jdk.incubator.foreign.MemoryAddress;
 44 import jdk.incubator.foreign.MemoryLayout;
 45 import jdk.incubator.foreign.NativeSymbol;
 46 import jdk.incubator.foreign.SymbolLookup;
 47 import org.testng.annotations.*;
 48 
 49 import static java.lang.invoke.MethodType.methodType;
 50 import static jdk.incubator.foreign.ValueLayout.JAVA_CHAR;
 51 import static org.testng.Assert.assertEquals;
 52 
 53 public class TestIntrinsics extends NativeTestHelper {
 54 
 55     static final CLinker abi = CLinker.systemCLinker();
 56     static {
 57         System.loadLibrary("Intrinsics");
 58     }
 59 
 60     static final SymbolLookup LOOKUP = SymbolLookup.loaderLookup();
 61 
 62     private interface RunnableX {
 63         void run() throws Throwable;
 64     }
 65 
 66     @Test(dataProvider = "tests")
 67     public void testIntrinsics(RunnableX test) throws Throwable {
 68         for (int i = 0; i < 20_000; i++) {
 69             test.run();
 70         }
 71     }
 72 
 73     @DataProvider
 74     public Object[][] tests() {
 75         List<RunnableX> testsList = new ArrayList<>();
 76 
 77         interface AddTest {
 78             void add(MethodHandle target, Object expectedResult, Object... args);
 79         }
 80 
 81         AddTest tests = (mh, expectedResult, args) -> testsList.add(() -> {
 82             Object actual = mh.invokeWithArguments(args);
 83             assertEquals(actual, expectedResult);
 84         });
 85 
 86         interface AddIdentity {
 87             void add(String name, Class<?> carrier, MemoryLayout layout, Object arg);
 88         }
 89 
 90         AddIdentity addIdentity = (name, carrier, layout, arg) -> {
 91             NativeSymbol ma = LOOKUP.lookup(name).get();
 92             MethodType mt = methodType(carrier, carrier);
 93             FunctionDescriptor fd = FunctionDescriptor.of(layout, layout);
 94 
 95             tests.add(abi.downcallHandle(ma, fd), arg, arg);
 96             tests.add(abi.downcallHandle(fd), arg, ma, arg);
 97         };
 98 
 99         { // empty
100             NativeSymbol ma = LOOKUP.lookup("empty").get();
101             MethodType mt = methodType(void.class);
102             FunctionDescriptor fd = FunctionDescriptor.ofVoid();
103             tests.add(abi.downcallHandle(ma, fd), null);
104         }
105 
106         addIdentity.add("identity_bool",   boolean.class, C_BOOL,   true);
107         addIdentity.add("identity_char",   byte.class,    C_CHAR,   (byte) 10);
108         addIdentity.add("identity_short",  short.class,   C_SHORT, (short) 10);
109         addIdentity.add("identity_int",    int.class,     C_INT,           10);
110         addIdentity.add("identity_long",   long.class,    C_LONG_LONG,     10L);
111         addIdentity.add("identity_float",  float.class,   C_FLOAT,         10F);
112         addIdentity.add("identity_double", double.class,  C_DOUBLE,        10D);
113 
114         { // identity_va
115             NativeSymbol ma = LOOKUP.lookup("identity_va").get();
116             MethodType mt = methodType(int.class, int.class, double.class, int.class, float.class, long.class);
117             FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT).asVariadic(C_DOUBLE, C_INT, C_FLOAT, C_LONG_LONG);
118             tests.add(abi.downcallHandle(ma, fd), 1, 1, 10D, 2, 3F, 4L);
119         }
120 
121         { // high_arity
122             MethodType baseMT = methodType(void.class, int.class, double.class, long.class, float.class, byte.class,
123                     short.class, char.class);
124             FunctionDescriptor baseFD = FunctionDescriptor.ofVoid(C_INT, C_DOUBLE, C_LONG_LONG, C_FLOAT, C_CHAR,
125                     C_SHORT, JAVA_CHAR);
126             Object[] args = {1, 10D, 2L, 3F, (byte) 0, (short) 13, 'a'};
127             for (int i = 0; i < args.length; i++) {
128                 NativeSymbol ma = LOOKUP.lookup("invoke_high_arity" + i).get();
129                 MethodType mt = baseMT.changeReturnType(baseMT.parameterType(i));
130                 FunctionDescriptor fd = baseFD.withReturnLayout(baseFD.argumentLayouts().get(i));
131                 Object expected = args[i];
132                 tests.add(abi.downcallHandle(ma, fd), expected, args);
133             }
134         }
135 
136         return testsList.stream().map(rx -> new Object[]{ rx }).toArray(Object[][]::new);
137     }
138 }