1 /* 2 * Copyright (c) 2020, 2023, 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 * @modules java.base/jdk.internal.foreign 28 * @build NativeTestHelper CallGeneratorHelper TestUpcallHighArity 29 * 30 * @run testng/othervm/native 31 * --enable-native-access=ALL-UNNAMED 32 * TestUpcallHighArity 33 */ 34 35 import java.lang.foreign.*; 36 37 import org.testng.annotations.DataProvider; 38 import org.testng.annotations.Test; 39 40 import java.lang.invoke.MethodHandle; 41 import java.lang.invoke.MethodType; 42 import java.util.ArrayList; 43 import java.util.List; 44 import java.util.concurrent.atomic.AtomicReference; 45 import java.util.function.Consumer; 46 47 public class TestUpcallHighArity extends CallGeneratorHelper { 48 static final MethodHandle MH_do_upcall; 49 50 static { 51 System.loadLibrary("TestUpcallHighArity"); 52 MH_do_upcall = LINKER.downcallHandle( 53 findNativeOrThrow("do_upcall"), 54 FunctionDescriptor.ofVoid(C_POINTER, 55 S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER, 56 S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER, 57 S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER, 58 S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER) 59 ); 60 } 61 62 @Test(dataProvider = "args") 63 public void testUpcall(MethodHandle downcall, MethodType upcallType, 64 FunctionDescriptor upcallDescriptor) throws Throwable { 65 AtomicReference<Object[]> capturedArgs = new AtomicReference<>(); 66 try (Arena arena = Arena.ofConfined()) { 67 Object[] args = new Object[upcallType.parameterCount() + 1]; 68 args[0] = makeArgSaverCB(upcallDescriptor, arena, capturedArgs, -1); 69 List<MemoryLayout> argLayouts = upcallDescriptor.argumentLayouts(); 70 List<Consumer<Object>> checks = new ArrayList<>(); 71 for (int i = 1; i < args.length; i++) { 72 TestValue testValue = genTestValue(argLayouts.get(i - 1), arena); 73 args[i] = testValue.value(); 74 checks.add(testValue.check()); 75 } 76 77 downcall.invokeWithArguments(args); 78 79 Object[] capturedArgsArr = capturedArgs.get(); 80 for (int i = 0; i < capturedArgsArr.length; i++) { 81 checks.get(i).accept(capturedArgsArr[i]); 82 } 83 } 84 } 85 86 @DataProvider 87 public static Object[][] args() { 88 return new Object[][]{ 89 { MH_do_upcall, 90 MethodType.methodType(void.class, 91 MemorySegment.class, int.class, double.class, MemorySegment.class, 92 MemorySegment.class, int.class, double.class, MemorySegment.class, 93 MemorySegment.class, int.class, double.class, MemorySegment.class, 94 MemorySegment.class, int.class, double.class, MemorySegment.class), 95 FunctionDescriptor.ofVoid( 96 S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER, 97 S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER, 98 S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER, 99 S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER) 100 } 101 }; 102 } 103 104 }