Skip to content

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

Merged
uschindler merged 10 commits intoapache:mainfrom
uschindler:dev/fix-PosixNativeAccess-clinit
Mar 22, 2026
Merged

Prevent class initialization failure when JVM has no support for native code at all (wrong platform)#15844
uschindler merged 10 commits intoapache:mainfrom
uschindler:dev/fix-PosixNativeAccess-clinit

Conversation

@uschindler
Copy link
Contributor

@uschindler uschindler commented Mar 20, 2026

See #15843 for more details. Actually the problem is that looking up the default linker may throw UnuspportedEx when the platform does not allow native code at all.

This was caused by the refactoring where the symbol and linker lookup was moved outside the try-catch: 3003731

This could cause class loading issue es described in #15834 because it may break initializing of NativeAccess class.

The PR also catches the general problem case if the underlying linker does not support the FunctionDescriptor.

It also disables madvise on 32 bits: We could support it, but then we would need to ask the linker for the java type behind size_t (this works), but depending on the type we would need to map the returned MethodHandle by explicit casting to adapt to long (our Java call signature). This is not worth it, because MMapDir is not used on 32 bit platforms by defaulr.

@uschindler
Copy link
Contributor Author

10.x has the same problem, I will backport.

@uschindler uschindler changed the title Fix loading of PosixNativeAccess class when native access is not working. A previous refactoring broke this Prevent class initialization failure when JVM has no support for native code at all (wrong platform) Mar 20, 2026
@github-actions github-actions bot added this to the 10.5.0 milestone Mar 20, 2026
@uschindler
Copy link
Contributor Author

uschindler commented Mar 20, 2026

I tested this again with several hardcoded changes, an integration test is still missing:

  • I ran on linux, but changed the Gradle Testing plugin to no longer allow native access in modules => works correctly
  • I ran on windows but disabled the windows check, the class still correctly initialized although there was no symbol to madvise

To allow better testing I added the new Java 24 command line flag to enforce "deny" when native access is done. As we see, this breaks FAISS, so I will look into this.

@uschindler
Copy link
Contributor Author

FAISS was fixed by passing correct command line

@uschindler
Copy link
Contributor Author

In this PR I also added an extra check for 64bit JVM, because 32 bit support of madvise won't work on 32 bit (wrong posix_madvise signature). This is not really an issue because MMapDir is not used by default and many platform with 32 bits are no longer supported in Java 25, but we should still check this.

@uschindler
Copy link
Contributor Author

It is hard to add an integration test for disabled native access. I leave this open for a separate PR.

@uschindler
Copy link
Contributor Author

I am not sure why the Github runner fails. Looks like a bug on Github at moment. Will try to run workflow later.

@rmuir
Copy link
Member

rmuir commented Mar 20, 2026

Here is the error:

The action gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 is not allowed in apache/lucene

@uschindler
Copy link
Contributor Author

See my message on the dev list. We need to update our actions. I contacted asfinfra on Slack and theres heavy committing at moment.

@uschindler
Copy link
Contributor Author

Thanks @rmuir for updating main branch, looks like checks run again.

@uschindler
Copy link
Contributor Author

Hi,
as suggested by @cortlepp, I added some extra catch block for RuntimeException for safety, because it may indeed happen that the linker does not support our FunctionDescriptor.

In addition, I changed Constants.JRE_IS_64BIT constant to use ValueLayout.ADRRESS size to get bitness of platform. This works with Java 25 as its public API. We still can't get page size, so we require the native call. @dweiss what do you think?

@uschindler uschindler requested a review from dweiss March 21, 2026 08:28
@github-actions github-actions bot removed this from the 10.5.0 milestone Mar 21, 2026
@github-actions github-actions bot added this to the 11.0.0 milestone Mar 21, 2026
@uschindler uschindler modified the milestones: 11.0.0, 10.5.0 Mar 21, 2026
@uschindler uschindler added the skip-changelog Apply to PRs that don't need a changelog entry, stopping the automated changelog check. label Mar 21, 2026
@uschindler
Copy link
Contributor Author

About the page size I opened: https://bugs.openjdk.org/browse/JDK-8380572

return (OS_ARCH != null && OS_ARCH.contains("64"));
}
}
public static final boolean JRE_IS_64BIT = (ValueLayout.ADDRESS.byteSize() == Long.BYTES);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever. I like it.

@uschindler uschindler merged commit 6b5ce61 into apache:main Mar 22, 2026
13 checks passed
@uschindler uschindler deleted the dev/fix-PosixNativeAccess-clinit branch March 22, 2026 16:28
asf-gitbox-commits pushed a commit that referenced this pull request Mar 22, 2026
…ve 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:build-infra module:core/store skip-changelog Apply to PRs that don't need a changelog entry, stopping the automated changelog check.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants