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