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