/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/SymbolLookup.java

https://github.com/openjdk/valhalla · Java · 82 lines · 23 code · 5 blank · 54 comment · 1 complexity · 800fbe114c6b21481a5563d17d38c432 MD5 · raw file

  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. import jdk.internal.access.JavaLangAccess;
  27. import jdk.internal.access.SharedSecrets;
  28. import jdk.internal.reflect.CallerSensitive;
  29. import jdk.internal.reflect.Reflection;
  30. import java.util.Objects;
  31. import java.util.Optional;
  32. /**
  33. * A symbol lookup. Exposes a lookup operation for searching symbol addresses by name, see {@link SymbolLookup#lookup(String)}.
  34. * A symbol lookup can be used to lookup a symbol in a loaded library. Clients can obtain a {@linkplain #loaderLookup() loader lookup},
  35. * which can be used to search symbols in libraries loaded by the current classloader (e.g. using {@link System#load(String)},
  36. * or {@link System#loadLibrary(String)}).
  37. * Alternatively, clients can obtain a {@linkplain CLinker#systemLookup() platform-dependent lookup}, to search symbols
  38. * in the standard C library.
  39. * <p> Unless otherwise specified, passing a {@code null} argument, or an array argument containing one or more {@code null}
  40. * elements to a method in this class causes a {@link NullPointerException NullPointerException} to be thrown. </p>
  41. */
  42. @FunctionalInterface
  43. public interface SymbolLookup {
  44. /**
  45. * Looks up a symbol with given name in this lookup.
  46. *
  47. * @param name the symbol name.
  48. * @return the memory address associated with the symbol (if any).
  49. */
  50. Optional<MemoryAddress> lookup(String name);
  51. /**
  52. * Obtains a symbol lookup suitable to find symbols in native libraries associated with the caller's classloader
  53. * (that is, libraries loaded using {@link System#loadLibrary} or {@link System#load}).
  54. * <p>
  55. * This method is <a href="package-summary.html#restricted"><em>restricted</em></a>.
  56. * Restricted methods are unsafe, and, if used incorrectly, their use might crash
  57. * the JVM or, worse, silently result in memory corruption. Thus, clients should refrain from depending on
  58. * restricted methods, and use safe and supported functionalities, where possible.
  59. *
  60. * @return a symbol lookup suitable to find symbols in libraries loaded by the caller's classloader.
  61. * @throws IllegalCallerException if access to this method occurs from a module {@code M} and the command line option
  62. * {@code --enable-native-access} is either absent, or does not mention the module name {@code M}, or
  63. * {@code ALL-UNNAMED} in case {@code M} is an unnamed module.
  64. */
  65. @CallerSensitive
  66. static SymbolLookup loaderLookup() {
  67. Class<?> caller = Reflection.getCallerClass();
  68. Reflection.ensureNativeAccess(caller);
  69. ClassLoader loader = Objects.requireNonNull(caller.getClassLoader());
  70. return name -> {
  71. Objects.requireNonNull(name);
  72. JavaLangAccess javaLangAccess = SharedSecrets.getJavaLangAccess();
  73. MemoryAddress addr = MemoryAddress.ofLong(javaLangAccess.findNative(loader, name));
  74. return addr == MemoryAddress.NULL? Optional.empty() : Optional.of(addr);
  75. };
  76. }
  77. }