1 /* 2 * Copyright (c) 2015, 2016, 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 package java.nicl; 24 25 import java.io.File; 26 import jdk.internal.nicl.Errno; 27 import jdk.internal.nicl.NativeLibraryImpl; 28 import jdk.internal.nicl.types.BindingRegistry; 29 import jdk.internal.nicl.types.Function; 30 import jdk.internal.nicl.types.Type; 31 import jdk.internal.nicl.types.LayoutTypeImpl; 32 33 import java.lang.invoke.MethodHandle; 34 import java.lang.invoke.MethodType; 35 import java.nicl.types.LayoutType; 36 import java.util.Objects; 37 38 public final class NativeLibrary { 39 private static final Errno ERRNO = Errno.platformHasErrno() ? new Errno() : null; 40 41 // don't create 42 private NativeLibrary() {} 43 44 /** 45 * Create a raw, uncivilized version of the interface 46 * 47 * @param c the class to bind 48 * @param lib the library in which to look for native symbols 49 * @return 50 */ 51 @Deprecated 52 public static <T> T bindRaw(Class<T> c, Library lib) { 53 SecurityManager security = System.getSecurityManager(); 54 if (security != null) { 55 security.checkPermission(new RuntimePermission("java.nicl.bindRaw")); 56 } 57 return NativeLibraryImpl.bindRaw(c, lib); 58 } 59 60 /** 61 * Create a raw, uncivilized version of the interface 62 * 63 * @param c the class to bind 64 * @return 65 */ 66 public static <T> T bindRaw(Class<T> c) { 67 SecurityManager security = System.getSecurityManager(); 68 if (security != null) { 69 security.checkPermission(new RuntimePermission("java.nicl.bindRaw")); 70 } 71 return NativeLibraryImpl.bindRaw(c); 72 } 73 74 /** 75 * Create a civilized version of the interface 76 * 77 * @param c the raw, uncivilized version of the interface 78 * @return 79 */ 80 @Deprecated 81 public static Object bind(Class<?> c) { 82 SecurityManager security = System.getSecurityManager(); 83 if (security != null) { 84 security.checkPermission(new RuntimePermission("java.nicl.bind")); 85 } 86 return NativeLibraryImpl.bind(c); 87 } 88 89 /** 90 * Create a civilized version of the interface 91 * 92 * @param c the raw, uncivilized version of the interface 93 * @param lib the library in which to look for native symbols 94 * @return 95 */ 96 @Deprecated 97 public static Object bind(Class<?> c, Library lib) { 98 SecurityManager security = System.getSecurityManager(); 99 if (security != null) { 100 security.checkPermission(new RuntimePermission("java.nicl.bind")); 101 } 102 return NativeLibraryImpl.bind(c, lib); 103 } 104 105 /** 106 * Lookup a method handle for a native function with the specified arguments 107 * 108 * @param libs the libraries in which to look for native symbols 109 * @param symbolName the name of the symbol to look up 110 * @param methodType the type of the returned method handle 111 * @param isVarArgs true if the function is a varargs method 112 * @return a method handle which, when invoked, will call the target native function 113 * @throws NoSuchMethodException 114 * @throws IllegalAccessException 115 */ 116 public static MethodHandle lookupNativeMethod(Library[] libs, String symbolName, MethodType methodType, boolean isVarArgs) throws NoSuchMethodException, IllegalAccessException { 117 SecurityManager security = System.getSecurityManager(); 118 if (security != null) { 119 security.checkPermission(new RuntimePermission("java.nicl.lookupNative", symbolName)); 120 } 121 return NativeLibraryImpl.lookupNativeMethod(libs, symbolName, methodType, isVarArgs); 122 } 123 124 public static MethodHandle lookupNativeMethod(Library lib, String symbolName, MethodType methodType, boolean isVarArgs) throws NoSuchMethodException, IllegalAccessException { 125 return lookupNativeMethod(new Library[] { lib }, symbolName, methodType, isVarArgs); 126 } 127 128 /** 129 * Loads the native library specified by the <code>libname</code> 130 * argument. The <code>libname</code> argument must not contain any platform 131 * specific prefix, file extension or path. 132 * 133 * Otherwise, the libname argument is loaded from a system library 134 * location and mapped to a native library image in an implementation- 135 * dependent manner. 136 * <p> 137 * 138 * @param libname the name of the library. 139 * @exception SecurityException if a security manager exists and its 140 * <code>checkLink</code> method doesn't allow 141 * loading of the specified dynamic library 142 * @exception UnsatisfiedLinkError if either the libname argument 143 * contains a file path, the native library is not statically 144 * linked with the VM, or the library cannot be mapped to a 145 * native library image by the host system. 146 * @exception NullPointerException if <code>libname</code> is 147 * <code>null</code> 148 * @see java.lang.SecurityManager#checkLink(java.lang.String) 149 */ 150 public static Library loadLibrary(String filename) { 151 Objects.requireNonNull(filename); 152 SecurityManager security = System.getSecurityManager(); 153 if (security != null) { 154 security.checkLink(filename); 155 } 156 if (filename.indexOf(File.separatorChar) != -1) { 157 throw new UnsatisfiedLinkError( 158 "Directory separator should not appear in library name: " + filename); 159 } 160 return NativeLibraryImpl.loadLibrary(filename); 161 } 162 163 /** 164 * Loads the native library specified by the filename argument. The filename 165 * argument must be an absolute path name. 166 * 167 * @param filename the file to load. 168 * @exception SecurityException if a security manager exists and its 169 * <code>checkLink</code> method doesn't allow 170 * loading of the specified dynamic library 171 * @exception UnsatisfiedLinkError if either the filename is not an 172 * absolute path name, the native library is not statically 173 * linked with the VM, or the library cannot be mapped to 174 * a native library image by the host system. 175 * @exception NullPointerException if <code>filename</code> is 176 * <code>null</code> 177 * @see java.lang.SecurityManager#checkLink(java.lang.String) 178 */ 179 public static Library load(String filename) { 180 Objects.requireNonNull(filename); 181 SecurityManager security = System.getSecurityManager(); 182 if (security != null) { 183 security.checkLink(filename); 184 } 185 if (!(new File(filename).isAbsolute())) { 186 throw new UnsatisfiedLinkError( 187 "Expecting an absolute path of the library: " + filename); 188 } 189 return NativeLibraryImpl.load(filename); 190 } 191 192 public static Library getDefaultLibrary() { 193 SecurityManager security = System.getSecurityManager(); 194 if (security != null) { 195 security.checkPermission(new RuntimePermission("java.nicl.getDefaultLibrary")); 196 } 197 return NativeLibraryImpl.getDefaultLibrary(); 198 } 199 200 public static <T> LayoutType<T> createLayout(Class<T> c) { 201 return LayoutTypeImpl.create(c); 202 } 203 204 public static int errno() { 205 if (ERRNO == null) { 206 throw new UnsupportedOperationException(); 207 } 208 209 return ERRNO.errno(); 210 } 211 212 public static void throwErrnoException(String msg) throws Exception { 213 throw new Exception(msg + ": " + ERRNO.strerror(errno())); 214 } 215 216 public static void throwErrnoException() throws Exception { 217 throw new Exception(ERRNO.strerror(errno())); 218 } 219 220 /** 221 * Lookup a method handle for a native function with the specified arguments 222 * 223 * @param lib the library in which to look for the function 224 * @param symbol the name of the symbol to look up 225 * @param type the type descriptor of the native function 226 */ 227 public static MethodHandle lookupNativeMethod(Library lib, String symbol, String type) 228 throws NoSuchMethodException, IllegalAccessException { 229 Function fn = (Function) Type.of(type); 230 MethodType mt = BindingRegistry.getInstance().defaultMethodType(fn); 231 return lookupNativeMethod(lib, symbol, mt, fn.isVarArg()); 232 } 233 } --- EOF ---