Skip to content

Apache Lucene open-source search software

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

Uwe Schindler6b5ce61bcd8b
diff --git a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsAndRandomizationPlugin.java b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsAndRandomizationPlugin.java
index 8874a40bd17..d87c45fd3c6 100644
--- a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsAndRandomizationPlugin.java
+++ b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsAndRandomizationPlugin.java
@@ -497,8 +497,10 @@ public class TestsAndRandomizationPlugin extends LuceneGradlePlugin {
":lucene:distribution.tests",
":lucene:test-framework" ->
"ALL-UNNAMED";
+ case ":lucene:sandbox" -> "org.apache.lucene.core,ALL-UNNAMED";
default -> "org.apache.lucene.core";
});
+ task.jvmArgs("--illegal-native-access=deny");
var loggingFileProvider =
project.getObjects().newInstance(LoggingFileArgumentProvider.class);
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index a2bb435fe27..d96058f1e63 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -192,15 +192,22 @@ Other
* GITHUB#14613: Rewrite APIJAR extractor to use Java 24 classfile API and kill ASM dependency also for build system. (Uwe Schindler)
* GITHUB#14705: Use Comparators for some PriorityQueue implementations. (Simon Cooper)
+
* GITHUB#14761: Use more Comparators for PriorityQueue implementations. (Simon Cooper)
+
* GITHUB#14817: Refactor some complex uses of PriorityQueue to use Comparators. (Simon Cooper)
* GITHUB#14607: Index open performs version check on each segment, ignores indexCreatedVersionMajor (Rahul Goswami)
+
* GITHUB#15454: Restore binary readability for 8.x indexes like previously and throw IndexFormatTooOldException for an older default codec not found on classpath (Rahul Goswami)
-GITHUB#15821: Pass immutable list directly from BooleanQuery.Builder to private constructor,
+* GITHUB#15821: Pass immutable list directly from BooleanQuery.Builder to private constructor,
avoiding unnecessary list→array→list conversions. (Sagar Upadhyaya)
+* GITHUB#15844: Detect JVM bitness using Panama's ValueLayout#ADDRESS instead
+ of undocumented system properties. (Uwe Schindler)
+
+
======================= Lucene 10.5.0 =======================
API Changes
@@ -268,6 +275,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/java/org/apache/lucene/store/PosixNativeAccess.java b/lucene/core/src/java/org/apache/lucene/store/PosixNativeAccess.java
index 1ab0169f244..124cc49b150 100644
--- a/lucene/core/src/java/org/apache/lucene/store/PosixNativeAccess.java
+++ b/lucene/core/src/java/org/apache/lucene/store/PosixNativeAccess.java
@@ -60,29 +60,36 @@ 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 (IllegalCallerException _) {
- 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 (IllegalCallerException _) {
+ 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/java/org/apache/lucene/util/Constants.java b/lucene/core/src/java/org/apache/lucene/util/Constants.java
index a8aa066b247..99eb2208efe 100644
--- a/lucene/core/src/java/org/apache/lucene/util/Constants.java
+++ b/lucene/core/src/java/org/apache/lucene/util/Constants.java
@@ -16,6 +16,7 @@
*/
package org.apache.lucene.util;
+import java.lang.foreign.ValueLayout;
import java.util.Locale;
import java.util.Optional;
import org.apache.lucene.store.ReadAdvice;
@@ -71,16 +72,7 @@ public final class Constants {
HotspotVMOptions.get("UseJVMCICompiler").map(Boolean::valueOf).orElse(false);
/** True iff running on a 64bit JVM */
- public static final boolean JRE_IS_64BIT = is64Bit();
-
- private static boolean is64Bit() {
- final String datamodel = getSysProp("sun.arch.data.model");
- if (datamodel != null) {
- return datamodel.contains("64");
- } else {
- return (OS_ARCH != null && OS_ARCH.contains("64"));
- }
- }
+ public static final boolean JRE_IS_64BIT = (ValueLayout.ADDRESS.byteSize() == Long.BYTES);
/** true if FMA likely means a cpu instruction and not BigDecimal logic. */
private static final boolean HAS_FMA =
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 33e12782334..3d040d1ca85 100644
--- a/lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java
+++ b/lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java
@@ -106,8 +106,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());
}