1 /* 2 * Copyright (c) 2022, 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 * @enablePreview 27 * @requires jdk.foreign.linker != "UNSUPPORTED" 28 * @modules java.base/jdk.internal.foreign 29 * @run testng TestLinker 30 * @run testng/othervm/policy=security.policy 31 * -Djava.security.manager=default TestLinker 32 */ 33 34 import jdk.internal.foreign.CABI; 35 import org.testng.annotations.DataProvider; 36 import org.testng.annotations.Test; 37 38 import java.lang.foreign.FunctionDescriptor; 39 import java.lang.foreign.Linker; 40 import java.lang.invoke.MethodHandle; 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.List; 44 45 import static java.lang.foreign.MemoryLayout.*; 46 import static java.lang.foreign.ValueLayout.JAVA_CHAR; 47 import static java.lang.foreign.ValueLayout.JAVA_SHORT; 48 import static org.testng.Assert.assertSame; 49 import static org.testng.Assert.assertNotSame; 50 51 public class TestLinker extends NativeTestHelper { 52 53 static final boolean IS_FALLBACK_LINKER = CABI.current() == CABI.FALLBACK; 54 55 record LinkRequest(FunctionDescriptor descriptor, Linker.Option... options) {} 56 57 @Test(dataProvider = "notSameCases") 58 public void testLinkerOptionsCache(LinkRequest l1, LinkRequest l2) { 59 Linker linker = Linker.nativeLinker(); 60 MethodHandle mh1 = linker.downcallHandle(l1.descriptor(), l1.options()); 61 MethodHandle mh2 = linker.downcallHandle(l2.descriptor(), l2.options()); 62 // assert that these are 2 distinct link request. No caching allowed 63 assertNotSame(mh1, mh2); 64 } 65 66 @DataProvider 67 public static Object[][] notSameCases() { 68 FunctionDescriptor fd_II_V = FunctionDescriptor.ofVoid(C_INT, C_INT); 69 return new Object[][]{ 70 {new LinkRequest(fd_II_V), new LinkRequest(fd_II_V, Linker.Option.firstVariadicArg(1))}, 71 {new LinkRequest(FunctionDescriptor.ofVoid(JAVA_SHORT)), new LinkRequest(FunctionDescriptor.ofVoid(JAVA_CHAR))}, 72 {new LinkRequest(FunctionDescriptor.ofVoid(JAVA_SHORT)), new LinkRequest(FunctionDescriptor.ofVoid(JAVA_CHAR))}, 73 }; 74 } 75 76 @Test(dataProvider = "namedDescriptors") 77 public void testNamedLinkerCache(FunctionDescriptor f1, FunctionDescriptor f2) { 78 Linker linker = Linker.nativeLinker(); 79 MethodHandle mh1 = linker.downcallHandle(f1); 80 MethodHandle mh2 = linker.downcallHandle(f2); 81 // assert that these are the same link request, even though layout names differ 82 assertSame(mh1, mh2); 83 } 84 85 @DataProvider 86 public static Object[][] namedDescriptors() { 87 List<Object[]> cases = new ArrayList<>(Arrays.asList(new Object[][]{ 88 { FunctionDescriptor.ofVoid(C_INT), 89 FunctionDescriptor.ofVoid(C_INT.withName("x")) }, 90 { FunctionDescriptor.ofVoid(structLayout(C_INT)), 91 FunctionDescriptor.ofVoid(structLayout(C_INT).withName("x")) }, 92 { FunctionDescriptor.ofVoid(structLayout(C_INT)), 93 FunctionDescriptor.ofVoid(structLayout(C_INT.withName("x"))) }, 94 { FunctionDescriptor.ofVoid(structLayout(C_INT, paddingLayout(4), C_LONG_LONG)), 95 FunctionDescriptor.ofVoid(structLayout(C_INT, paddingLayout(4), C_LONG_LONG.withName("x"))) }, 96 { FunctionDescriptor.ofVoid(structLayout(C_INT, paddingLayout(4), C_LONG_LONG)), 97 FunctionDescriptor.ofVoid(structLayout(C_INT, paddingLayout(4).withName("x"), C_LONG_LONG)) }, 98 { FunctionDescriptor.ofVoid(structLayout(sequenceLayout(1, C_INT))), 99 FunctionDescriptor.ofVoid(structLayout(sequenceLayout(1, C_INT).withName("x"))) }, 100 { FunctionDescriptor.ofVoid(structLayout(sequenceLayout(1, C_INT))), 101 FunctionDescriptor.ofVoid(structLayout(sequenceLayout(1, C_INT.withName("x")))) }, 102 { FunctionDescriptor.ofVoid(C_POINTER), 103 FunctionDescriptor.ofVoid(C_POINTER.withName("x")) }, 104 { FunctionDescriptor.ofVoid(C_POINTER.withTargetLayout(C_INT)), 105 FunctionDescriptor.ofVoid(C_POINTER.withTargetLayout(C_INT.withName("x"))) }, 106 { FunctionDescriptor.ofVoid(C_POINTER.withTargetLayout(C_INT)), 107 FunctionDescriptor.ofVoid(C_POINTER.withName("x").withTargetLayout(C_INT.withName("x"))) }, 108 })); 109 110 if (!IS_FALLBACK_LINKER) { 111 cases.add(new Object[]{ FunctionDescriptor.ofVoid(unionLayout(C_INT)), 112 FunctionDescriptor.ofVoid(unionLayout(C_INT).withName("x")) }); 113 cases.add(new Object[]{ FunctionDescriptor.ofVoid(unionLayout(C_INT)), 114 FunctionDescriptor.ofVoid(unionLayout(C_INT.withName("x"))) }); 115 } 116 117 return cases.toArray(Object[][]::new); 118 } 119 120 @DataProvider 121 public static Object[][] invalidIndexCases() { 122 return new Object[][]{ 123 { -1, }, 124 { 42, }, 125 }; 126 } 127 128 @Test(dataProvider = "invalidIndexCases", 129 expectedExceptions = IllegalArgumentException.class, 130 expectedExceptionsMessageRegExp = ".*not in bounds for descriptor.*") 131 public void testInvalidOption(int invalidIndex) { 132 Linker.Option option = Linker.Option.firstVariadicArg(invalidIndex); 133 FunctionDescriptor desc = FunctionDescriptor.ofVoid(); 134 Linker.nativeLinker().downcallHandle(desc, option); // throws 135 } 136 137 @Test(expectedExceptions = IllegalArgumentException.class, 138 expectedExceptionsMessageRegExp = ".*Unknown name.*") 139 public void testInvalidPreservedValueName() { 140 Linker.Option.captureCallState("foo"); // throws 141 } 142 143 }