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.CLinker;
 27 
 28 import java.lang.invoke.MethodHandle;
 29 import java.lang.invoke.MethodHandles;
 30 import java.lang.invoke.MethodType;
 31 
 32 import jdk.incubator.foreign.Addressable;
 33 import jdk.incubator.foreign.SymbolLookup;
 34 import jdk.incubator.foreign.MemoryAddress;
 35 import jdk.incubator.foreign.MemorySegment;
 36 import jdk.incubator.foreign.ResourceScope;
 37 
 38 import jdk.incubator.foreign.VaList;
 39 import jdk.incubator.foreign.ValueLayout;
 40 import org.testng.annotations.*;
 41 
 42 public class MethodHandleLookup {
 43 
 44     @Test(dataProvider = "restrictedMethods")
 45     public void testRestrictedHandles(MethodHandle handle, String testName) throws Throwable {
 46         new handle.invoker.MethodHandleInvoker().call(handle);
 47     }
 48 
 49     @DataProvider(name = "restrictedMethods")
 50     static Object[][] restrictedMethods() {
 51         try {
 52             return new Object[][]{
 53                     { MethodHandles.lookup().findStatic(CLinker.class, "systemCLinker",
 54                             MethodType.methodType(CLinker.class)), "ForeignLinker::systemCLinker" },
 55                     { MethodHandles.lookup().findStatic(VaList.class, "ofAddress",
 56                             MethodType.methodType(VaList.class, MemoryAddress.class, ResourceScope.class)),
 57                             "VaList::ofAddress/1" },
 58                     { MethodHandles.lookup().findStatic(SymbolLookup.class, "loaderLookup",
 59                             MethodType.methodType(SymbolLookup.class)),
 60                             "SymbolLookup::loaderLookup" },
 61                     { MethodHandles.lookup().findStatic(MemorySegment.class, "ofAddressNative",
 62                             MethodType.methodType(MemorySegment.class, MemoryAddress.class, long.class, ResourceScope.class)),
 63                             "MemorySegment::ofAddressNative" },
 64                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "getUtf8String",
 65                             MethodType.methodType(String.class, long.class)),
 66                             "MemoryAddress::getUtf8String" },
 67                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "setUtf8String",
 68                             MethodType.methodType(void.class, long.class, String.class)),
 69                             "MemoryAddress::setUtf8String" },
 70                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "get",
 71                             MethodType.methodType(byte.class, ValueLayout.OfByte.class, long.class)),
 72                             "MemoryAddress::get/byte" },
 73                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "get",
 74                             MethodType.methodType(boolean.class, ValueLayout.OfBoolean.class, long.class)),
 75                             "MemoryAddress::get/boolean" },
 76                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "get",
 77                             MethodType.methodType(char.class, ValueLayout.OfChar.class, long.class)),
 78                             "MemoryAddress::get/char" },
 79                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "get",
 80                             MethodType.methodType(short.class, ValueLayout.OfShort.class, long.class)),
 81                             "MemoryAddress::get/short" },
 82                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "get",
 83                             MethodType.methodType(int.class, ValueLayout.OfInt.class, long.class)),
 84                             "MemoryAddress::get/int" },
 85                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "get",
 86                             MethodType.methodType(float.class, ValueLayout.OfFloat.class, long.class)),
 87                             "MemoryAddress::get/float" },
 88                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "get",
 89                             MethodType.methodType(long.class, ValueLayout.OfLong.class, long.class)),
 90                             "MemoryAddress::get/long" },
 91                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "get",
 92                             MethodType.methodType(double.class, ValueLayout.OfDouble.class, long.class)),
 93                             "MemoryAddress::get/double" },
 94                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "get",
 95                             MethodType.methodType(MemoryAddress.class, ValueLayout.OfAddress.class, long.class)),
 96                             "MemoryAddress::get/address" },
 97                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "set",
 98                             MethodType.methodType(void.class, ValueLayout.OfByte.class, long.class, byte.class)),
 99                             "MemoryAddress::set/byte" },
100                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "set",
101                             MethodType.methodType(void.class, ValueLayout.OfBoolean.class, long.class, boolean.class)),
102                             "MemoryAddress::set/boolean" },
103                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "set",
104                             MethodType.methodType(void.class, ValueLayout.OfChar.class, long.class, char.class)),
105                             "MemoryAddress::set/char" },
106                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "set",
107                             MethodType.methodType(void.class, ValueLayout.OfShort.class, long.class, short.class)),
108                             "MemoryAddress::set/short" },
109                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "set",
110                             MethodType.methodType(void.class, ValueLayout.OfInt.class, long.class, int.class)),
111                             "MemoryAddress::set/int" },
112                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "set",
113                             MethodType.methodType(void.class, ValueLayout.OfFloat.class, long.class, float.class)),
114                             "MemoryAddress::set/float" },
115                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "set",
116                             MethodType.methodType(void.class, ValueLayout.OfLong.class, long.class, long.class)),
117                             "MemoryAddress::set/long" },
118                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "set",
119                             MethodType.methodType(void.class, ValueLayout.OfDouble.class, long.class, double.class)),
120                             "MemoryAddress::set/double" },
121                     { MethodHandles.lookup().findVirtual(MemoryAddress.class, "set",
122                             MethodType.methodType(void.class, ValueLayout.OfAddress.class, long.class, Addressable.class)),
123                             "MemoryAddress::set/address" },
124             };
125         } catch (Throwable ex) {
126             throw new ExceptionInInitializerError((ex));
127         }
128     }
129 }