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