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 package handle.lookup;
25 
26 import jdk.incubator.foreign.Addressable;
27 import jdk.incubator.foreign.CLinker;
28 
29 import java.lang.invoke.MethodHandle;
30 import java.lang.invoke.MethodHandles;
31 import java.lang.invoke.MethodType;
32 import java.nio.charset.Charset;
33 import java.nio.file.Path;
34 import java.util.Optional;
35 
36 import jdk.incubator.foreign.FunctionDescriptor;
37 import jdk.incubator.foreign.SymbolLookup;
38 import jdk.incubator.foreign.MemoryAddress;
39 import jdk.incubator.foreign.MemoryLayout;
40 import jdk.incubator.foreign.MemorySegment;
41 import jdk.incubator.foreign.ResourceScope;
42 import jdk.incubator.foreign.SegmentAllocator;
43 
44 import org.testng.annotations.*;
45 
46 public class MethodHandleLookup {
47 
48     @Test(dataProvider = "restrictedMethods")
49     public void testRestrictedHandles(MethodHandle handle, String testName) throws Throwable {
50         new handle.invoker.MethodHandleInvoker().call(handle);
51     }
52 
53     @DataProvider(name = "restrictedMethods")
54     static Object[][] restrictedMethods() {
55         try {
56             return new Object[][]{
57                     { MethodHandles.lookup().findStatic(CLinker.class, "getInstance",
58                             MethodType.methodType(CLinker.class)), "CLinker::getInstance" },
59                     { MethodHandles.lookup().findStatic(CLinker.class, "toJavaString",
60                             MethodType.methodType(String.class, MemoryAddress.class)),
61                             "CLinker::toJavaString" },
62                     { MethodHandles.lookup().findStatic(CLinker.class, "allocateMemory",
63                             MethodType.methodType(MemoryAddress.class, long.class)),
64                             "CLinker::allocateMemory" },
65                     { MethodHandles.lookup().findStatic(CLinker.class, "freeMemory",
66                             MethodType.methodType(void.class, MemoryAddress.class)),
67                             "CLinker::freeMemory" },
68                     { MethodHandles.lookup().findStatic(CLinker.VaList.class, "ofAddress",
69                             MethodType.methodType(CLinker.VaList.class, MemoryAddress.class)),
70                             "VaList::ofAddress/1" },
71                     { MethodHandles.lookup().findStatic(CLinker.VaList.class, "ofAddress",
72                             MethodType.methodType(CLinker.VaList.class, MemoryAddress.class, ResourceScope.class)),
73                             "VaList::ofAddress/2" },
74                     { MethodHandles.lookup().findStatic(CLinker.class, "systemLookup",
75                             MethodType.methodType(SymbolLookup.class)),
76                             "CLinker::systemLookup" },
77                     { MethodHandles.lookup().findStatic(SymbolLookup.class, "loaderLookup",
78                             MethodType.methodType(SymbolLookup.class)),
79                             "SymbolLookup::loaderLookup" },
80                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "asSegment",
81                             MethodType.methodType(MemorySegment.class, long.class, ResourceScope.class)),
82                             "MemoryAddress::asSegment/1" },
83                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "asSegment",
84                             MethodType.methodType(MemorySegment.class, long.class, Runnable.class, ResourceScope.class)),
85                             "MemoryAddress::asSegment/2" },
86                     { MethodHandles.lookup().findStatic(MemorySegment.class, "globalNativeSegment",
87                             MethodType.methodType(MemorySegment.class)),
88                             "MemoryAddress::globalNativeSegment" }
89             };
90         } catch (Throwable ex) {
91             throw new ExceptionInInitializerError((ex));
92         }
93     }
94 }