< prev index next >

test/jdk/java/foreign/ThrowingUpcall.java

Print this page

 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 }

 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.NativeSymbol;
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 
34 public class ThrowingUpcall extends NativeTestHelper {
35 
36     private static final MethodHandle downcallVoid;
37     private static final MethodHandle downcallNonVoid;

38     public static final MethodHandle MH_throwException;
39 
40     static {
41         System.loadLibrary("TestUpcall");
42         SymbolLookup lookup = SymbolLookup.loaderLookup();
43         downcallVoid = CLinker.systemCLinker().downcallHandle(
44             lookup.lookup("f0_V__").orElseThrow(),
45                 FunctionDescriptor.ofVoid(C_POINTER)
46         );
47         downcallNonVoid = CLinker.systemCLinker().downcallHandle(
48             lookup.lookup("f10_I_I_").orElseThrow(),
49                 FunctionDescriptor.of(C_INT, C_INT, C_POINTER)
50         );
51 
52         try {
53             MH_throwException = MethodHandles.lookup().findStatic(ThrowingUpcall.class, "throwException",
54                     MethodType.methodType(void.class));
55         } catch (ReflectiveOperationException e) {
56             throw new ExceptionInInitializerError(e);
57         }
58     }
59 
60     public static void throwException() throws Throwable {
61         throw new Throwable("Testing upcall exceptions");
62     }
63 
64     public static void main(String[] args) throws Throwable {
65         if (args[0].equals("void")) {
66             testVoid();
67         } else {
68             testNonVoid();
69         }
70     }
71 
72     public static void testVoid() throws Throwable {
73         MethodHandle handle = MH_throwException;
74         MethodHandle invoker = MethodHandles.exactInvoker(MethodType.methodType(void.class));
75         handle = MethodHandles.insertArguments(invoker, 0, handle);
76 
77         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
78             NativeSymbol stub = CLinker.systemCLinker().upcallStub(handle, FunctionDescriptor.ofVoid(), scope);
79 
80             downcallVoid.invoke(stub); // should call Shutdown.exit(1);
81         }
82     }
83 
84     public static void testNonVoid() throws Throwable {
85         MethodHandle handle = MethodHandles.identity(int.class);
86         handle = MethodHandles.collectArguments(handle, 0, MH_throwException);
87         MethodHandle invoker = MethodHandles.exactInvoker(MethodType.methodType(int.class, int.class));
88         handle = MethodHandles.insertArguments(invoker, 0, handle);
89 
90         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
91             NativeSymbol stub = CLinker.systemCLinker().upcallStub(handle, FunctionDescriptor.of(C_INT, C_INT), scope);
92 
93             downcallNonVoid.invoke(42, stub); // should call Shutdown.exit(1);
94         }
95     }
96 
97 }
< prev index next >