1 /* 2 * Copyright (c) 2022, 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 * @test 26 * @enablePreview 27 * @requires jdk.foreign.linker != "UNSUPPORTED" 28 * @requires (!(os.name == "Mac OS X" & os.arch == "aarch64") | jdk.foreign.linker != "FALLBACK") 29 * @modules java.base/jdk.internal.foreign 30 * @build NativeTestHelper CallGeneratorHelper TestUpcallBase 31 * 32 * @run testng/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies 33 * --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=17 34 * TestUpcallStack 35 */ 36 37 import java.lang.foreign.Arena; 38 import java.lang.foreign.FunctionDescriptor; 39 import java.lang.foreign.MemorySegment; 40 41 import org.testng.annotations.Test; 42 43 import java.lang.invoke.MethodHandle; 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 TestUpcallStack extends TestUpcallBase { 50 51 static { 52 System.loadLibrary("TestUpcallStack"); 53 } 54 55 @Test(dataProvider="functions", dataProviderClass=CallGeneratorHelper.class) 56 public void testUpcallsStack(int count, String fName, Ret ret, List<ParamType> paramTypes, 57 List<StructFieldType> fields) throws Throwable { 58 List<Consumer<Object>> returnChecks = new ArrayList<>(); 59 List<Consumer<Object>> argChecks = new ArrayList<>(); 60 MemorySegment addr = findNativeOrThrow("s" + fName); 61 try (Arena arena = Arena.ofConfined()) { 62 FunctionDescriptor descriptor = functionStack(ret, paramTypes, fields); 63 MethodHandle mh = downcallHandle(LINKER, addr, arena, descriptor); 64 AtomicReference<Object[]> capturedArgs = new AtomicReference<>(); 65 Object[] args = makeArgsStack(capturedArgs, arena, descriptor, returnChecks, argChecks); 66 67 Object res = mh.invokeWithArguments(args); 68 69 if (ret == Ret.NON_VOID) { 70 returnChecks.forEach(c -> c.accept(res)); 71 } 72 73 Object[] capturedArgsArr = capturedArgs.get(); 74 for (int capturedIdx = STACK_PREFIX_LAYOUTS.size(), checkIdx = 0; 75 capturedIdx < capturedArgsArr.length; 76 capturedIdx++, checkIdx++) { 77 argChecks.get(checkIdx).accept(capturedArgsArr[capturedIdx]); 78 } 79 } 80 } 81 82 static FunctionDescriptor functionStack(Ret ret, List<ParamType> params, List<StructFieldType> fields) { 83 return function(ret, params, fields, STACK_PREFIX_LAYOUTS); 84 } 85 86 static Object[] makeArgsStack(AtomicReference<Object[]> capturedArgs, Arena session, FunctionDescriptor descriptor, 87 List<Consumer<Object>> checks, List<Consumer<Object>> argChecks) { 88 return makeArgs(capturedArgs, session, descriptor, checks, argChecks, STACK_PREFIX_LAYOUTS.size()); 89 } 90 91 }