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 2026/03/22 16:54:08 UTC

(lucene) branch branch_10x updated: Prevent class initialization failure when JVM has no support for native code at all (wrong platform) (#15844)

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

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


The following commit(s) were added to refs/heads/branch_10x by this push:
     new 3aea083c02c Prevent class initialization failure when JVM has no support for native code at all (wrong platform) (#15844)
3aea083c02c is described below

commit 3aea083c02c38dedd8cb3820b43d0f1bc351535a
Author: Uwe Schindler <us...@apache.org>
AuthorDate: Sun Mar 22 17:53:40 2026 +0100

    Prevent class initialization failure when JVM has no support for native code at all (wrong platform) (#15844)
    
    * Fix loading of PosixNativeAccess class when native access is not working. A previous refactoring broke this
    * Only use madvise on 64 bit platforms
    * Add more safety when linker throws illegal argument Exception; improve logging
---
 lucene/CHANGES.txt                                 |  4 ++
 .../org/apache/lucene/store/PosixNativeAccess.java | 51 ++++++++++++----------
 .../org/apache/lucene/store/TestMMapDirectory.java |  4 +-
 3 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index fd91c7530c5..4e4d7f30585 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -70,6 +70,10 @@ Bug Fixes
 
 * GITHUB#15656: Fix wrong BitSet result in MemoryAccountingBitsetCollector (Binlong Gao)
 
+* GITHUB#15844: Prevent class initialization failure when JVM has no support for
+  native code at all (wrong platform); disable native code on 32 bit platforms.
+  (Uwe Schindler, thanks to Christian Ortlepp for finding the issue)
+
 Other
 ---------------------
 * GITHUB#15586: Document that scoring and ranking may change across major Lucene versions, and that applications requiring stable ranking should explicitly configure Similarity. (Parveen Saini)
diff --git a/lucene/core/src/java21/org/apache/lucene/store/PosixNativeAccess.java b/lucene/core/src/java21/org/apache/lucene/store/PosixNativeAccess.java
index 5e56fd4b18b..86c6ca59f71 100644
--- a/lucene/core/src/java21/org/apache/lucene/store/PosixNativeAccess.java
+++ b/lucene/core/src/java21/org/apache/lucene/store/PosixNativeAccess.java
@@ -61,31 +61,38 @@ final class PosixNativeAccess extends NativeAccess {
   }
 
   static {
-    final Linker linker = Linker.nativeLinker();
-    final SymbolLookup stdlib = linker.defaultLookup();
     MethodHandle adviseHandle = null;
     int pagesize = -1;
     PosixNativeAccess instance = null;
-    try {
-      adviseHandle = lookupMadvise(linker, stdlib);
-      pagesize = (int) lookupGetPageSize(linker, stdlib).invokeExact();
-      instance = new PosixNativeAccess();
-    } catch (UnsupportedOperationException uoe) {
-      LOG.warning(uoe.getMessage());
-    } catch (
-        @SuppressWarnings("unused")
-        IllegalCallerException ice) {
-      LOG.warning(
-          String.format(
-              Locale.ENGLISH,
-              "Lucene has no access to native functions. To enable access to native functions, "
-                  + "pass the following on command line: --enable-native-access=%s",
-              Optional.ofNullable(PosixNativeAccess.class.getModule().getName())
-                  .orElse("ALL-UNNAMED")));
-    } catch (RuntimeException | Error e) {
-      throw e;
-    } catch (Throwable e) {
-      throw new AssertionError(e);
+    if (ValueLayout.ADDRESS.byteSize() == Long.BYTES) {
+      // we only support 64 bits at the moment
+      try {
+        final Linker linker = Linker.nativeLinker();
+        final SymbolLookup stdlib = linker.defaultLookup();
+        adviseHandle = lookupMadvise(linker, stdlib);
+        pagesize = (int) lookupGetPageSize(linker, stdlib).invokeExact();
+        instance = new PosixNativeAccess();
+      } catch (UnsupportedOperationException uoe) {
+        LOG.warning("Cannot initialize native function bindings: " + uoe.getMessage());
+      } catch (
+          @SuppressWarnings("unused")
+          IllegalCallerException ice) {
+        LOG.warning(
+            String.format(
+                Locale.ENGLISH,
+                "Lucene has no access to native functions. To enable access to native functions, "
+                    + "pass the following on command line: --enable-native-access=%s",
+                Optional.ofNullable(PosixNativeAccess.class.getModule().getName())
+                    .orElse("ALL-UNNAMED")));
+      } catch (RuntimeException re) {
+        LOG.warning(
+            "Cannot initialize native function bindings (linker is incompatible): "
+                + re.toString());
+      } catch (Error e) {
+        throw e;
+      } catch (Throwable e) {
+        throw new AssertionError(e);
+      }
     }
     MH$posix_madvise = adviseHandle;
     PAGE_SIZE = pagesize;
diff --git a/lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java
index 99183ff390b..96cfae3e4ab 100644
--- a/lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java
+++ b/lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java
@@ -109,8 +109,8 @@ public class TestMMapDirectory extends BaseDirectoryTestCase {
 
   public void testMadviseAvail() throws Exception {
     assertEquals(
-        "madvise should be supported on Linux and Macos",
-        Constants.LINUX || Constants.MAC_OS_X,
+        "madvise should be supported on Linux and Macos (64 bit)",
+        Constants.JRE_IS_64BIT && (Constants.LINUX || Constants.MAC_OS_X),
         MMapDirectory.supportsMadvise());
   }