You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2021/12/21 12:13:44 UTC

[lucene] branch branch_9_0 updated: LUCENE-10330: Provide better message if unmmaping failed in MMapDirectory by module system

This is an automated email from the ASF dual-hosted git repository.

uschindler pushed a commit to branch branch_9_0
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_9_0 by this push:
     new 0afc6e2  LUCENE-10330: Provide better message if unmmaping failed in MMapDirectory by module system
0afc6e2 is described below

commit 0afc6e2605bbd3d1b723f1940d688d9f3b97bb2b
Author: Uwe Schindler <us...@apache.org>
AuthorDate: Tue Dec 21 13:13:34 2021 +0100

    LUCENE-10330: Provide better message if unmmaping failed in MMapDirectory by module system
---
 lucene/CHANGES.txt                                   |  4 +++-
 .../java/org/apache/lucene/store/MMapDirectory.java  | 20 ++++++++++----------
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 4b50938..167b251 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -7,7 +7,9 @@ http://s.apache.org/luceneversions
 
 Bug Fixes
 ---------------------
-(No changes)
+
+* LUCENE-10330: Provide better message if unmmaping failed in MMapDirectory by module system.
+  (Uwe Schindler)
 
 ======================= Lucene 9.0.0 =======================
 
diff --git a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
index 161cc38..6e5b181 100644
--- a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
@@ -346,9 +346,7 @@ public class MMapDirectory extends FSDirectory {
     }
   }
 
-  @SuppressForbidden(
-      reason =
-          "Needs access to private APIs in DirectBuffer, sun.misc.Cleaner, and sun.misc.Unsafe to enable hack")
+  @SuppressForbidden(reason = "Needs access to sun.misc.Unsafe to enable hack")
   private static Object unmapHackImpl() {
     final Lookup lookup = lookup();
     try {
@@ -363,29 +361,31 @@ public class MMapDirectory extends FSDirectory {
       final Field f = unsafeClass.getDeclaredField("theUnsafe");
       f.setAccessible(true);
       final Object theUnsafe = f.get(null);
-      return newBufferCleaner(ByteBuffer.class, unmapper.bindTo(theUnsafe));
+      return newBufferCleaner(unmapper.bindTo(theUnsafe));
     } catch (SecurityException se) {
       return "Unmapping is not supported, because not all required permissions are given to the Lucene JAR file: "
           + se
           + " [Please grant at least the following permissions: RuntimePermission(\"accessClassInPackage.sun.misc\") "
           + " and ReflectPermission(\"suppressAccessChecks\")]";
     } catch (ReflectiveOperationException | RuntimeException e) {
+      final Module module = MMapDirectory.class.getModule();
+      final ModuleLayer layer = module.getLayer();
+      // classpath / unnamed module has no layer, so we need to check:
+      if (layer != null
+          && layer.findModule("jdk.unsupported").map(module::canRead).orElse(false) == false) {
+        return "Unmapping is not supported, because Lucene cannot read 'jdk.unsupported' module.";
+      }
       return "Unmapping is not supported on this platform, because internal Java APIs are not compatible with this Lucene version: "
           + e;
     }
   }
 
-  private static BufferCleaner newBufferCleaner(
-      final Class<?> unmappableBufferClass, final MethodHandle unmapper) {
+  private static BufferCleaner newBufferCleaner(final MethodHandle unmapper) {
     assert Objects.equals(methodType(void.class, ByteBuffer.class), unmapper.type());
     return (String resourceDescription, ByteBuffer buffer) -> {
       if (!buffer.isDirect()) {
         throw new IllegalArgumentException("unmapping only works with direct buffers");
       }
-      if (!unmappableBufferClass.isInstance(buffer)) {
-        throw new IllegalArgumentException(
-            "buffer is not an instance of " + unmappableBufferClass.getName());
-      }
       final Throwable error =
           AccessController.doPrivileged(
               (PrivilegedAction<Throwable>)