1 /*
2 * Copyright (c) 2019, 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 jdk.incubator.foreign/jdk.incubator.foreign.unsafe
28 * jdk.incubator.foreign/jdk.internal.foreign
29 * jdk.incubator.foreign/jdk.internal.foreign.abi
30 * java.base/sun.security.action
31 * @build NativeTestHelper CallGeneratorHelper TestDowncall
32 *
33 * @run testng/othervm -Dforeign.restricted=permit TestDowncall
34 */
35
36 import jdk.incubator.foreign.CSupport;
37 import jdk.incubator.foreign.FunctionDescriptor;
38 import jdk.incubator.foreign.LibraryLookup;
39 import jdk.incubator.foreign.MemoryAddress;
40 import jdk.incubator.foreign.MemoryLayout;
41 import jdk.incubator.foreign.ForeignLinker;
42
43 import java.lang.invoke.MethodHandle;
44 import java.lang.invoke.MethodType;
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.function.Consumer;
48
49 import org.testng.annotations.*;
50
51 public class TestDowncall extends CallGeneratorHelper {
52
53 static LibraryLookup lib = LibraryLookup.ofLibrary("TestDowncall");
54 static ForeignLinker abi = CSupport.getSystemLinker();
55
56
57 @Test(dataProvider="functions", dataProviderClass=CallGeneratorHelper.class)
58 public void testDowncall(String fName, Ret ret, List<ParamType> paramTypes, List<StructFieldType> fields) throws Throwable {
59 List<Consumer<Object>> checks = new ArrayList<>();
60 MemoryAddress addr = lib.lookup(fName);
61 MethodHandle mh = abi.downcallHandle(addr, methodType(ret, paramTypes, fields), function(ret, paramTypes, fields));
62 Object[] args = makeArgs(paramTypes, fields, checks);
63 mh = mh.asSpreader(Object[].class, paramTypes.size());
64 Object res = mh.invoke(args);
65 if (ret == Ret.NON_VOID) {
66 checks.forEach(c -> c.accept(res));
67 }
68 for (Object arg : args) {
69 cleanup(arg);
70 }
71 }
72
73 static MethodType methodType(Ret ret, List<ParamType> params, List<StructFieldType> fields) {
74 MethodType mt = ret == Ret.VOID ?
75 MethodType.methodType(void.class) : MethodType.methodType(paramCarrier(params.get(0).layout(fields)));
76 for (ParamType p : params) {
77 mt = mt.appendParameterTypes(paramCarrier(p.layout(fields)));
78 }
79 return mt;
80 }
81
82 static FunctionDescriptor function(Ret ret, List<ParamType> params, List<StructFieldType> fields) {
83 MemoryLayout[] paramLayouts = params.stream().map(p -> p.layout(fields)).toArray(MemoryLayout[]::new);
84 return ret == Ret.VOID ?
85 FunctionDescriptor.ofVoid(paramLayouts) :
86 FunctionDescriptor.of(paramLayouts[0], paramLayouts);
87 }
88
89 static Object[] makeArgs(List<ParamType> params, List<StructFieldType> fields, List<Consumer<Object>> checks) throws ReflectiveOperationException {
90 Object[] args = new Object[params.size()];
91 for (int i = 0 ; i < params.size() ; i++) {
92 args[i] = makeArg(params.get(i).layout(fields), checks, i == 0);
93 }
94 return args;
95 }
96 }