1 /*
   2  * Copyright (c) 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 jdk.internal.nicl;
  24 
  25 import jdk.internal.misc.Unsafe;
  26 import jdk.internal.org.objectweb.asm.Type;
  27 
  28 import java.lang.invoke.MethodHandle;
  29 import java.lang.invoke.MethodType;

  30 import java.nicl.Library;

  31 import java.nicl.metadata.LibraryDependencies;
  32 import java.nicl.metadata.LibraryDependency;
  33 import java.nicl.metadata.NativeType;



  34 import java.util.Arrays;
  35 import java.util.Map;
  36 import java.util.WeakHashMap;

  37 import java.security.AccessController;
  38 import java.security.PrivilegedAction;
  39 
  40 public class NativeLibraryImpl {
  41     enum ImplType {
  42         HEADER, CIVILIZED;
  43 
  44         public String getImplName() {
  45             return name().charAt(0) + name().substring(1).toLowerCase();
  46         }
  47     }
  48 
  49     // Map of interface -> impl class (per ImplType)
  50     @SuppressWarnings("unchecked")
  51     private static final Map<Class<?>, Class<?>>[] IMPLEMENTATIONS = (Map<Class<?>, Class<?>>[]) new Map<?, ?>[ImplType.values().length];
  52 
  53     static {
  54         for (int i = 0; i < IMPLEMENTATIONS.length; i++) {
  55             IMPLEMENTATIONS[i] = new WeakHashMap<>();
  56         }
  57     }
  58 
  59     private static final Unsafe U = Unsafe.getUnsafe();
  60 
  61     private static String generateImplName(Class<?> c, ImplType type) {
  62         return Type.getInternalName(c) + "$" + type.getImplName() + "Impl";
  63     }
  64 
  65     public static <T> Class<? extends T> getImplClass(Class<T> c) {
  66         return getOrCreateImpl(c, getSymbolLookupForClass(c));
  67     }
  68 
  69     /**
  70      * Look up the implementation for an interface, or generate it if needed
  71      *
  72      * @param c the interface for which to return an implementation class
  73      * @param generator a generator capable of generating an implementation, if needed
  74      * @return a class implementing the interface
  75      */
  76     @SuppressWarnings("unchecked")
  77     private static <T> Class<? extends T> getOrCreateImpl(ImplType type, Class<T> c, ImplGenerator generator) {
  78         Class<? extends T> implCls;
  79 
  80         Map<Class<?>, Class<?>> map = IMPLEMENTATIONS[type.ordinal()];
  81 
  82         synchronized (map) {
  83             implCls = (Class<? extends T>) map.get(c);
  84         }
  85 
  86         if (implCls == null) {
  87             Class<? extends T> newCls;
  88             try {
  89                 newCls = (Class<? extends T>)AccessController.doPrivileged((PrivilegedAction<Class<?>>) () -> {
  90                         return generator.generate();
  91                     });
  92             } catch (Exception e) {
  93                 throw new RuntimeException("Failed to generate " + type + " for class " + c, e);
  94             }
  95 
  96             synchronized (map) {
  97                 implCls = (Class<? extends T>) map.get(c);
  98                 if (implCls == null) {
  99                     map.put(c, newCls);
 100                     implCls = newCls;
 101                 }
 102             }
 103         }
 104 
 105         return implCls;
 106     }
 107 
 108     /**
 109      * Generate an implementation class for a header type
 110      *
 111      * @param c an interface representing a header file - must have an @Header annotation
 112      * @param lookup the symbol lookup to use to look up native symbols
 113      * @return a class implementing the header
 114      */
 115     private static <T> Class<? extends T> getOrCreateImpl(Class<T> c, SymbolLookup lookup)
 116             throws SecurityException, InternalError {
 117         /*
 118         if (!c.isAnnotationPresent(Header.class)) {
 119             throw new IllegalArgumentException("No @Header annotation on class " + c);
 120         }
 121         */
 122 
 123         String implClassName = generateImplName(c, ImplType.HEADER);
 124 
 125         boolean isRecordType = c.isAnnotationPresent(NativeType.class) && c.getAnnotation(NativeType.class).isRecordType();
 126 
 127         return getOrCreateImpl(ImplType.HEADER, c, new HeaderImplGenerator(c, implClassName, c, lookup, isRecordType));
 128     }
 129 
 130     private static <T> Class<? extends T> getOrCreateCivilizedImpl(Class<T> c, T rawInstance)
 131             throws SecurityException, InternalError {
 132         /*
 133         if (!c.isAnnotationPresent(Header.class)) {
 134             throw new IllegalArgumentException("No @Header annotation on class " + c);
 135         }
 136         */
 137 
 138         String implClassName = generateImplName(c, ImplType.CIVILIZED);
 139 
 140         return getOrCreateImpl(ImplType.CIVILIZED, c, new CivilizedHeaderImplGenerator<>(c, implClassName, c, rawInstance));
 141     }
 142 
 143     private static Library loadLibrary(String name, boolean isAbsolute) {
 144         return Platform.getInstance().loadLibrary(name, isAbsolute);
 145     }
 146 
 147     public static Library loadLibrary(String name) {

 148         return loadLibrary(name, false);
 149     }
 150 
 151     public static Library loadLibraryFile(String name) {
 152         return loadLibrary(name, true);
 153     }
 154 
 155     private static Library loadLibrary(LibraryDependency dep) {
 156         if (dep.isAbsolute()) {
 157             return loadLibraryFile(dep.name());











 158         } else {
 159             return loadLibrary(dep.name());






 160         }
 161     }
 162 
 163     private static LibraryDependency[] getLibraryDependenciesForClass(Class<?> c) {
 164         if (c.isAnnotationPresent(LibraryDependencies.class)) {
 165             return c.getAnnotation(LibraryDependencies.class).value();
 166         } else if (c.isAnnotationPresent(LibraryDependency.class)) {
 167             return new LibraryDependency[] { c.getAnnotation(LibraryDependency.class) };
 168         } else {
 169             return null;
 170         }
 171     }
 172 
 173     private static SymbolLookup getSymbolLookupForClass(Class<?> c) {
 174         LibraryDependency[] deps = getLibraryDependenciesForClass(c);
 175 
 176         Library[] libs;
 177 
 178         if (deps == null) {
 179             // FIXME: Require @LibraryDependency on all relevant classes
 180             //System.err.println("WARNING: No @LibraryDependency annotation on class " + c.getName());
 181             //throw new IllegalArgumentException("No @LibraryDependency annotation on class " + c.getName());
 182             libs = new Library[] { getDefaultLibrary() };
 183         } else {
 184             libs = Arrays.stream(deps).map(NativeLibraryImpl::loadLibrary).toArray(Library[]::new);
 185         }
 186 
 187         return new SymbolLookup(libs);
 188     }
 189 
 190     public static Library getDefaultLibrary() {
 191         return Platform.getInstance().defaultLibrary();
 192     }
 193 
 194     @Deprecated
 195     public static <T> T bindRaw(Class<T> c, Library lib) {
 196         return bindRaw(c, new SymbolLookup(new Library[] { lib }));
 197     }
 198 
 199     private static <T> T bindRaw(Class<T> c, SymbolLookup lookup) {
 200         Class<? extends T> cls = getOrCreateImpl(c, lookup);
 201 
 202         try {
 203             //FIXME: Run some constructor here...?
 204             @SuppressWarnings("unchecked")
 205             T instance = (T) U.allocateInstance(cls);
 206             return instance;
 207         } catch (ReflectiveOperationException e) {
 208             throw new Error(e);
 209         }
 210     }
 211 
 212     public static <T> T bindRaw(Class<T> c) {
 213         return bindRaw(c, getSymbolLookupForClass(c));
 214     }
 215 
 216     private static <T> Object bind(Class<T> c, SymbolLookup lookup) {
 217         try {
 218             T rawInstance = bindRaw(c);
 219 
 220             Class<?> civilizedCls = NativeLibraryImpl.getOrCreateCivilizedImpl(c, rawInstance);
 221 
 222             return U.allocateInstance(civilizedCls);
 223         } catch (Throwable t) {
 224             throw new RuntimeException(t);
 225         }
 226     }
 227 
 228     @Deprecated
 229     public static <T> Object bind(Class<T> c, Library lib) {
 230         return bind(c, new SymbolLookup(lib));
 231     }
 232 
 233     public static <T> Object bind(Class<T> c) {
 234         return bind(c, getSymbolLookupForClass(c));
 235     }
 236 
 237     public static MethodHandle lookupNativeMethod(Library[] libs, String symbolName, MethodType methodType, boolean isVarArgs) throws NoSuchMethodException, IllegalAccessException {
 238         NativeInvoker invoker = new NativeInvoker(methodType, isVarArgs, new SymbolLookup(libs), symbolName);
 239         return invoker.getBoundMethodHandle().asCollector(Object[].class, methodType.parameterCount());
 240     }
 241 }
--- EOF ---