Path: blob/master/test/jdk/java/foreign/TestIntrinsics.java
41145 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @requires os.arch=="amd64" | os.arch=="x86_64" | os.arch=="aarch64"26* @run testng/othervm27* -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=true28* -Djdk.internal.foreign.ProgrammableInvoker.USE_INTRINSICS=true29* --enable-native-access=ALL-UNNAMED30* -Xbatch31* TestIntrinsics32*/3334import jdk.incubator.foreign.CLinker;35import jdk.incubator.foreign.FunctionDescriptor;3637import java.lang.invoke.MethodHandle;38import java.lang.invoke.MethodType;39import java.util.ArrayList;40import java.util.List;4142import jdk.incubator.foreign.MemoryAddress;43import jdk.incubator.foreign.MemoryLayout;44import jdk.incubator.foreign.SymbolLookup;45import org.testng.annotations.*;4647import static java.lang.invoke.MethodType.methodType;48import static jdk.incubator.foreign.CLinker.*;49import static jdk.incubator.foreign.FunctionDescriptor.TRIVIAL_ATTRIBUTE_NAME;50import static org.testng.Assert.assertEquals;5152public class TestIntrinsics {5354static final CLinker abi = CLinker.getInstance();55static {56System.loadLibrary("Intrinsics");57}5859static final SymbolLookup LOOKUP = SymbolLookup.loaderLookup();6061private interface RunnableX {62void run() throws Throwable;63}6465@Test(dataProvider = "tests")66public void testIntrinsics(RunnableX test) throws Throwable {67for (int i = 0; i < 20_000; i++) {68test.run();69}70}7172@DataProvider73public Object[][] tests() {74List<RunnableX> testsList = new ArrayList<>();7576interface AddTest {77void add(MethodHandle target, Object expectedResult, Object... args);78}7980AddTest tests = (mh, expectedResult, args) -> testsList.add(() -> {81Object actual = mh.invokeWithArguments(args);82assertEquals(actual, expectedResult);83});8485interface AddIdentity {86void add(String name, Class<?> carrier, MemoryLayout layout, Object arg);87}8889AddIdentity addIdentity = (name, carrier, layout, arg) -> {90MemoryAddress ma = LOOKUP.lookup(name).get();91MethodType mt = methodType(carrier, carrier);92FunctionDescriptor fd = FunctionDescriptor.of(layout, layout);9394tests.add(abi.downcallHandle(ma, mt, fd), arg, arg);95tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), arg, arg);96tests.add(abi.downcallHandle(mt, fd), arg, ma, arg);97};9899{ // empty100MemoryAddress ma = LOOKUP.lookup("empty").get();101MethodType mt = methodType(void.class);102FunctionDescriptor fd = FunctionDescriptor.ofVoid();103tests.add(abi.downcallHandle(ma, mt, fd), null);104tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), null);105}106107addIdentity.add("identity_char", byte.class, C_CHAR, (byte) 10);108addIdentity.add("identity_short", short.class, C_SHORT, (short) 10);109addIdentity.add("identity_int", int.class, C_INT, 10);110addIdentity.add("identity_long", long.class, C_LONG_LONG, 10L);111addIdentity.add("identity_float", float.class, C_FLOAT, 10F);112addIdentity.add("identity_double", double.class, C_DOUBLE, 10D);113114{ // identity_va115MemoryAddress ma = LOOKUP.lookup("identity_va").get();116MethodType mt = methodType(int.class, int.class, double.class, int.class, float.class, long.class);117FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, asVarArg(C_DOUBLE),118asVarArg(C_INT), asVarArg(C_FLOAT), asVarArg(C_LONG_LONG));119tests.add(abi.downcallHandle(ma, mt, fd), 1, 1, 10D, 2, 3F, 4L);120tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), 1, 1, 10D, 2, 3F, 4L);121}122123{ // high_arity124MethodType baseMT = methodType(void.class, int.class, double.class, long.class, float.class, byte.class,125short.class, char.class);126FunctionDescriptor baseFD = FunctionDescriptor.ofVoid(C_INT, C_DOUBLE, C_LONG_LONG, C_FLOAT, C_CHAR,127C_SHORT, C_SHORT);128Object[] args = {1, 10D, 2L, 3F, (byte) 0, (short) 13, 'a'};129for (int i = 0; i < args.length; i++) {130MemoryAddress ma = LOOKUP.lookup("invoke_high_arity" + i).get();131MethodType mt = baseMT.changeReturnType(baseMT.parameterType(i));132FunctionDescriptor fd = baseFD.withReturnLayout(baseFD.argumentLayouts().get(i));133Object expected = args[i];134tests.add(abi.downcallHandle(ma, mt, fd), expected, args);135tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), expected, args);136}137}138139return testsList.stream().map(rx -> new Object[]{ rx }).toArray(Object[][]::new);140}141}142143144