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.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package jdk.incubator.foreign;
26 
27 import jdk.internal.access.JavaLangAccess;
28 import jdk.internal.access.SharedSecrets;
29 import jdk.internal.reflect.CallerSensitive;
30 import jdk.internal.reflect.Reflection;
31 
32 import java.util.Objects;
33 import java.util.Optional;
34 
35 /**
36  * A symbol lookup. Exposes a lookup operation for searching symbol addresses by name, see {@link SymbolLookup#lookup(String)}.
37  * A symbol lookup can be used to look up a symbol in a loaded library. Clients can obtain a {@linkplain #loaderLookup() loader lookup},
38  * which can be used to search symbols in libraries loaded by the current classloader (e.g. using {@link System#load(String)},
39  * or {@link System#loadLibrary(String)}).
40  * Alternatively, clients can search symbols in the standard C library using a {@link CLinker}, which conveniently
41  * implements this interface.
42  * <p> Unless otherwise specified, passing a {@code null} argument, or an array argument containing one or more {@code null}
43  * elements to a method in this class causes a {@link NullPointerException NullPointerException} to be thrown. </p>
44  */
45 @FunctionalInterface
46 public interface SymbolLookup {
47 
48     /**
49      * Looks up a symbol with given name in this lookup.
50      *
51      * @param name the symbol name.
52      * @return the lookup symbol (if any).
53      */
54     Optional<NativeSymbol> lookup(String name);
55 
56     /**
57      * Obtains a symbol lookup suitable to find symbols in native libraries associated with the caller's classloader
58      * (that is, libraries loaded using {@link System#loadLibrary} or {@link System#load}).
59      * <p>
60      * This method is <a href="package-summary.html#restricted"><em>restricted</em></a>.
61      * Restricted methods are unsafe, and, if used incorrectly, their use might crash
62      * the JVM or, worse, silently result in memory corruption. Thus, clients should refrain from depending on
63      * restricted methods, and use safe and supported functionalities, where possible.
64      *
65      * @return a symbol lookup suitable to find symbols in libraries loaded by the caller's classloader.
66      * @throws IllegalCallerException if access to this method occurs from a module {@code M} and the command line option
67      * {@code --enable-native-access} is either absent, or does not mention the module name {@code M}, or
68      * {@code ALL-UNNAMED} in case {@code M} is an unnamed module.
69      */
70     @CallerSensitive
71     static SymbolLookup loaderLookup() {
72         Class<?> caller = Reflection.getCallerClass();
73         Reflection.ensureNativeAccess(caller);
74         ClassLoader loader = Objects.requireNonNull(caller.getClassLoader());
75         return name -> {
76             Objects.requireNonNull(name);
77             JavaLangAccess javaLangAccess = SharedSecrets.getJavaLangAccess();
78             MemoryAddress addr = MemoryAddress.ofLong(javaLangAccess.findNative(loader, name));
79             return addr == MemoryAddress.NULL? Optional.empty() : Optional.of(NativeSymbol.ofAddress(name, addr, ResourceScope.globalScope()));
80         };
81     }
82 
83 }