Path: blob/master/test/jdk/java/foreign/TestDowncall.java
51495 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test26* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"27* @modules jdk.incubator.foreign/jdk.internal.foreign28* @build NativeTestHelper CallGeneratorHelper TestDowncall29*30* @run testng/othervm/timeout=24031* --enable-native-access=ALL-UNNAMED32* TestDowncall33*/3435import jdk.incubator.foreign.CLinker;36import jdk.incubator.foreign.FunctionDescriptor;37import jdk.incubator.foreign.SymbolLookup;38import jdk.incubator.foreign.MemoryAddress;39import jdk.incubator.foreign.MemoryLayout;4041import java.lang.invoke.MethodHandle;42import java.lang.invoke.MethodType;43import java.util.ArrayList;44import java.util.List;45import java.util.function.Consumer;4647import jdk.incubator.foreign.MemorySegment;48import jdk.incubator.foreign.SegmentAllocator;49import org.testng.annotations.*;50import static org.testng.Assert.*;5152public class TestDowncall extends CallGeneratorHelper {5354static CLinker abi = CLinker.getInstance();55static {56System.loadLibrary("TestDowncall");57}5859static final SymbolLookup LOOKUP = SymbolLookup.loaderLookup();6061@Test(dataProvider="functions", dataProviderClass=CallGeneratorHelper.class)62public void testDowncall(int count, String fName, Ret ret, List<ParamType> paramTypes, List<StructFieldType> fields) throws Throwable {63List<Consumer<Object>> checks = new ArrayList<>();64MemoryAddress addr = LOOKUP.lookup(fName).get();65MethodType mt = methodType(ret, paramTypes, fields);66FunctionDescriptor descriptor = function(ret, paramTypes, fields);67Object[] args = makeArgs(paramTypes, fields, checks);68try (NativeScope scope = new NativeScope()) {69boolean needsScope = mt.returnType().equals(MemorySegment.class);70Object res = doCall(addr, scope, mt, descriptor, args);71if (ret == Ret.NON_VOID) {72checks.forEach(c -> c.accept(res));73if (needsScope) {74// check that return struct has indeed been allocated in the native scope75assertEquals(((MemorySegment) res).scope(), scope.scope());76assertEquals(scope.allocatedBytes(), descriptor.returnLayout().get().byteSize());77} else {78// if here, there should be no allocation through the scope!79assertEquals(scope.allocatedBytes(), 0L);80}81} else {82// if here, there should be no allocation through the scope!83assertEquals(scope.allocatedBytes(), 0L);84}85}86}8788@Test(dataProvider="functions", dataProviderClass=CallGeneratorHelper.class)89public void testDowncallNoScope(int count, String fName, Ret ret, List<ParamType> paramTypes, List<StructFieldType> fields) throws Throwable {90List<Consumer<Object>> checks = new ArrayList<>();91MemoryAddress addr = LOOKUP.lookup(fName).get();92MethodType mt = methodType(ret, paramTypes, fields);93FunctionDescriptor descriptor = function(ret, paramTypes, fields);94Object[] args = makeArgs(paramTypes, fields, checks);95boolean needsScope = mt.returnType().equals(MemorySegment.class);96if (count % 100 == 0) {97System.gc();98}99Object res = doCall(addr, IMPLICIT_ALLOCATOR, mt, descriptor, args);100if (ret == Ret.NON_VOID) {101checks.forEach(c -> c.accept(res));102if (needsScope) {103// check that return struct has indeed been allocated in the default scope104try {105((MemorySegment)res).scope().close(); // should throw106fail("Expected exception!");107} catch (UnsupportedOperationException ex) {108// ok109}110}111}112}113114Object doCall(MemoryAddress addr, SegmentAllocator allocator, MethodType type, FunctionDescriptor descriptor, Object[] args) throws Throwable {115MethodHandle mh = abi.downcallHandle(addr, allocator, type, descriptor);116Object res = mh.invokeWithArguments(args);117return res;118}119120static MethodType methodType(Ret ret, List<ParamType> params, List<StructFieldType> fields) {121MethodType mt = ret == Ret.VOID ?122MethodType.methodType(void.class) : MethodType.methodType(paramCarrier(params.get(0).layout(fields)));123for (ParamType p : params) {124mt = mt.appendParameterTypes(paramCarrier(p.layout(fields)));125}126return mt;127}128129static FunctionDescriptor function(Ret ret, List<ParamType> params, List<StructFieldType> fields) {130MemoryLayout[] paramLayouts = params.stream().map(p -> p.layout(fields)).toArray(MemoryLayout[]::new);131return ret == Ret.VOID ?132FunctionDescriptor.ofVoid(paramLayouts) :133FunctionDescriptor.of(paramLayouts[0], paramLayouts);134}135136static Object[] makeArgs(List<ParamType> params, List<StructFieldType> fields, List<Consumer<Object>> checks) throws ReflectiveOperationException {137Object[] args = new Object[params.size()];138for (int i = 0 ; i < params.size() ; i++) {139args[i] = makeArg(params.get(i).layout(fields), checks, i == 0);140}141return args;142}143}144145146