Path: blob/master/test/jdk/java/foreign/TestUpcallHighArity.java
41144 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 TestUpcallHighArity29*30* @run testng/othervm/native31* --enable-native-access=ALL-UNNAMED32* TestUpcallHighArity33*/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;40import jdk.incubator.foreign.MemorySegment;41import jdk.incubator.foreign.ResourceScope;42import org.testng.annotations.DataProvider;43import org.testng.annotations.Test;4445import java.lang.invoke.MethodHandle;46import java.lang.invoke.MethodHandles;47import java.lang.invoke.MethodType;48import java.util.List;49import java.util.concurrent.atomic.AtomicReference;5051import static jdk.incubator.foreign.CLinker.*;52import static org.testng.Assert.assertEquals;5354public class TestUpcallHighArity extends CallGeneratorHelper {55static final MethodHandle MH_do_upcall;56static final MethodHandle MH_passAndSave;57static final CLinker LINKER = CLinker.getInstance();5859// struct S_PDI { void* p0; double p1; int p2; };60static final MemoryLayout S_PDI_LAYOUT = MemoryLayout.structLayout(61C_POINTER.withName("p0"),62C_DOUBLE.withName("p1"),63C_INT.withName("p2")64);6566static {67try {68System.loadLibrary("TestUpcallHighArity");69SymbolLookup lookup = SymbolLookup.loaderLookup();70MH_do_upcall = LINKER.downcallHandle(71lookup.lookup("do_upcall").get(),72MethodType.methodType(void.class, MemoryAddress.class,73MemorySegment.class, int.class, double.class, MemoryAddress.class,74MemorySegment.class, int.class, double.class, MemoryAddress.class,75MemorySegment.class, int.class, double.class, MemoryAddress.class,76MemorySegment.class, int.class, double.class, MemoryAddress.class),77FunctionDescriptor.ofVoid(C_POINTER,78S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,79S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,80S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,81S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER)82);83MH_passAndSave = MethodHandles.lookup().findStatic(TestUpcallHighArity.class, "passAndSave",84MethodType.methodType(void.class, Object[].class, AtomicReference.class));85} catch (ReflectiveOperationException e) {86throw new InternalError(e);87}88}8990static void passAndSave(Object[] o, AtomicReference<Object[]> ref) {91for (int i = 0; i < o.length; i++) {92if (o[i] instanceof MemorySegment) {93MemorySegment ms = (MemorySegment) o[i];94MemorySegment copy = MemorySegment.allocateNative(ms.byteSize(), ResourceScope.newImplicitScope());95copy.copyFrom(ms);96o[i] = copy;97}98}99ref.set(o);100}101102@Test(dataProvider = "args")103public void testUpcall(MethodHandle downcall, MethodType upcallType,104FunctionDescriptor upcallDescriptor) throws Throwable {105AtomicReference<Object[]> capturedArgs = new AtomicReference<>();106MethodHandle target = MethodHandles.insertArguments(MH_passAndSave, 1, capturedArgs)107.asCollector(Object[].class, upcallType.parameterCount())108.asType(upcallType);109try (ResourceScope scope = ResourceScope.newConfinedScope()) {110MemoryAddress upcallStub = LINKER.upcallStub(target, upcallDescriptor, scope);111Object[] args = new Object[upcallType.parameterCount() + 1];112args[0] = upcallStub.address();113List<MemoryLayout> argLayouts = upcallDescriptor.argumentLayouts();114for (int i = 1; i < args.length; i++) {115args[i] = makeArg(argLayouts.get(i - 1), null, false);116}117118downcall.invokeWithArguments(args);119120Object[] capturedArgsArr = capturedArgs.get();121for (int i = 0; i < capturedArgsArr.length; i++) {122if (upcallType.parameterType(i) == MemorySegment.class) {123assertStructEquals((MemorySegment) capturedArgsArr[i], (MemorySegment) args[i + 1], argLayouts.get(i));124} else {125assertEquals(capturedArgsArr[i], args[i + 1]);126}127}128}129}130131@DataProvider132public static Object[][] args() {133return new Object[][]{134{ MH_do_upcall,135MethodType.methodType(void.class,136MemorySegment.class, int.class, double.class, MemoryAddress.class,137MemorySegment.class, int.class, double.class, MemoryAddress.class,138MemorySegment.class, int.class, double.class, MemoryAddress.class,139MemorySegment.class, int.class, double.class, MemoryAddress.class),140FunctionDescriptor.ofVoid(141S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,142S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,143S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,144S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER)145}146};147}148149}150151152