1 /* 2 * Copyright (c) 2021, 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 * @library /test/lib 29 * @build TestUpcallException 30 * 31 * @run testng/othervm/native 32 * --enable-native-access=ALL-UNNAMED 33 * TestUpcallException 34 */ 35 36 import org.testng.annotations.DataProvider; 37 import org.testng.annotations.Test; 38 39 import java.io.IOException; 40 import java.lang.foreign.Arena; 41 import java.lang.foreign.FunctionDescriptor; 42 import java.lang.foreign.Linker; 43 import java.lang.foreign.MemorySegment; 44 import java.lang.invoke.MethodHandle; 45 import java.lang.invoke.MethodHandles; 46 import java.lang.invoke.MethodType; 47 48 public class TestUpcallException extends UpcallTestHelper { 49 50 @Test(dataProvider = "exceptionCases") 51 public void testException(Class<?> target, boolean useSpec) throws InterruptedException, IOException { 52 runInNewProcess(target, useSpec) 53 .assertStdErrContains("Testing upcall exceptions"); 54 } 55 56 @DataProvider 57 public static Object[][] exceptionCases() { 58 return new Object[][]{ 59 { VoidUpcallRunner.class, false }, 60 { NonVoidUpcallRunner.class, false }, 61 { VoidUpcallRunner.class, true }, 62 { NonVoidUpcallRunner.class, true } 63 }; 64 } 65 66 public static class VoidUpcallRunner extends ExceptionRunnerBase { 67 public static void main(String[] args) throws Throwable { 68 try (Arena arena = Arena.ofConfined()) { 69 MemorySegment stub = Linker.nativeLinker().upcallStub(VOID_TARGET, FunctionDescriptor.ofVoid(), arena); 70 downcallVoid.invoke(stub); // should call Shutdown.exit(1); 71 } 72 } 73 } 74 75 public static class NonVoidUpcallRunner extends ExceptionRunnerBase { 76 public static void main(String[] args) throws Throwable { 77 try (Arena arena = Arena.ofConfined()) { 78 MemorySegment stub = Linker.nativeLinker().upcallStub(INT_TARGET, FunctionDescriptor.of(C_INT, C_INT), arena); 79 downcallNonVoid.invoke(42, stub); // should call Shutdown.exit(1); 80 } 81 } 82 } 83 84 // where 85 86 private static class ExceptionRunnerBase { 87 static final MethodHandle downcallVoid; 88 static final MethodHandle downcallNonVoid; 89 static final MethodHandle VOID_TARGET; 90 static final MethodHandle INT_TARGET; 91 92 static final Thread.UncaughtExceptionHandler UNCAUGHT_EXCEPTION_HANDLER 93 = (thread, throwable) -> System.out.println("From uncaught exception handler"); 94 95 static { 96 System.loadLibrary("TestUpcall"); 97 downcallVoid = Linker.nativeLinker().downcallHandle( 98 findNativeOrThrow("f0_V__"), 99 FunctionDescriptor.ofVoid(C_POINTER) 100 ); 101 downcallNonVoid = Linker.nativeLinker().downcallHandle( 102 findNativeOrThrow("f10_I_I_"), 103 FunctionDescriptor.of(C_INT, C_INT, C_POINTER) 104 ); 105 try { 106 MethodHandles.Lookup lookup = MethodHandles.lookup(); 107 VOID_TARGET = lookup.findStatic(ExceptionRunnerBase.class, "throwException", 108 MethodType.methodType(void.class)); 109 INT_TARGET = lookup.findStatic(ExceptionRunnerBase.class, "throwException", 110 MethodType.methodType(int.class, int.class)); 111 } catch (ReflectiveOperationException e) { 112 throw new ExceptionInInitializerError(e); 113 } 114 } 115 116 public static void throwException() { 117 throw new RuntimeException("Testing upcall exceptions"); 118 } 119 120 public static int throwException(int x) { 121 throw new RuntimeException("Testing upcall exceptions"); 122 } 123 } 124 }