1 /*
 2  * Copyright (c) 2021, 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 import jdk.incubator.foreign.CLinker;
25 import jdk.incubator.foreign.FunctionDescriptor;
26 import jdk.incubator.foreign.MemoryAddress;
27 import jdk.incubator.foreign.ResourceScope;
28 import jdk.incubator.foreign.SymbolLookup;
29 
30 import java.lang.invoke.MethodHandle;
31 import java.lang.invoke.MethodHandles;
32 import java.lang.invoke.MethodType;
33 import java.security.Permission;
34 
35 import static jdk.incubator.foreign.CLinker.C_POINTER;
36 
37 public class ThrowingUpcall {
38 
39     private static final MethodHandle downcall;
40     public static final MethodHandle MH_throwException;
41 
42     static {
43         System.loadLibrary("TestUpcall");
44         SymbolLookup lookup = SymbolLookup.loaderLookup();
45         downcall = CLinker.getInstance().downcallHandle(
46             lookup.lookup("f0_V__").orElseThrow(),
47             MethodType.methodType(void.class, MemoryAddress.class),
48             FunctionDescriptor.ofVoid(C_POINTER)
49         );
50 
51         try {
52             MH_throwException = MethodHandles.lookup().findStatic(ThrowingUpcall.class, "throwException",
53                     MethodType.methodType(void.class));
54         } catch (ReflectiveOperationException e) {
55             throw new ExceptionInInitializerError(e);
56         }
57     }
58 
59     public static void throwException() throws Throwable {
60         throw new Throwable("Testing upcall exceptions");
61     }
62 
63     public static void main(String[] args) throws Throwable {
64         test();
65     }
66 
67     public static void test() throws Throwable {
68         MethodHandle handle = MH_throwException;
69         MethodHandle invoker = MethodHandles.exactInvoker(MethodType.methodType(void.class));
70         handle = MethodHandles.insertArguments(invoker, 0, handle);
71 
72         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
73             MemoryAddress stub = CLinker.getInstance().upcallStub(handle, FunctionDescriptor.ofVoid(), scope);
74 
75             downcall.invokeExact(stub); // should call Shutdown.exit(1);
76         }
77     }
78 
79 }