Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force resolution of fstat64 symbol with JNA #110807

Merged
merged 1 commit into from
Jul 12, 2024

Conversation

rjernst
Copy link
Member

@rjernst rjernst commented Jul 11, 2024

When JNA loads libraries it creates a proxy object for the library. Unfortunately it doesn't actually inspect any of the methods, those get bound lazily at runtime when the method is called through the proxy. For fstat64 we need to know at load time whether the symbol exists, so that we can fallback to an alternate function if it doesn't.

This commit looks up the NativeLibrary object from JNA for libc and checks if fstat64 exists during load time.

When JNA loads libraries it creates a proxy object for the library.
Unfortunately it doesn't actually inspect any of the methods, those get
bound lazily at runtime when the method is called through the proxy. For
fstat64 we need to know at load time whether the symbol exists, so that
we can fallback to an alternate function if it doesn't.

This commit looks up the NativeLibrary object from JNA for libc and
checks if fstat64 exists during load time.
@rjernst rjernst added >non-issue :Core/Infra/Core Core issues without another label labels Jul 11, 2024
@rjernst rjernst requested a review from a team as a code owner July 11, 2024 22:51
@elasticsearchmachine elasticsearchmachine added v8.16.0 Team:Core/Infra Meta label for core/infra team labels Jul 11, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

fstat64 = Native.load("c", FStat64Function.class);
} catch (UnsatisfiedLinkError e) {
// TODO: explain
// fstat has a long history in linux from the 32-bit architecture days. On some modern linux systems,
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this make the class name JnaPosixCLibrary a bit of a misnomer?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe, but "fstat" is part of posix, it's just that the implementation is a bit wonky due to historical reasons (and thus why we need to sometimes peek into the implementation side).

// fstat has a long history in linux from the 32-bit architecture days. On some modern linux systems,
// fstat64 doesn't exist as a symbol in glibc. Instead, the compiler replaces fstat64 calls with
// the internal __fxstat method. Here we fall back to __fxstat, and staticall bind the special
// "version" argument so that the call site looks the same as that of fstat64
var fxstat = Native.load("c", FXStatFunction.class);
int version = System.getProperty("os.arch").equals("aarch64") ? 0 : 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm this seems worth documenting.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what I was doing here with the comment. Do you see a better place to document it?

@rjernst rjernst merged commit e4349f8 into elastic:main Jul 12, 2024
15 checks passed
@rjernst rjernst deleted the native/jna_fstat branch July 12, 2024 21:53
rjernst added a commit to rjernst/elasticsearch that referenced this pull request Jul 13, 2024
Native preallocation has several issues, introduced in a refactoring for
8.13. First, the native allocator is never even tried, it always decides
to fall back to the Java setLength method. Second, the stat method did
not work correctly on all systems, see elastic#110807. This commit fixes
native preallocate to properly execute on Linux, as well as MacOS. It
also adds direct tests of preallocation.

Note that this is meant as a bugfix for 8.15, so as minimal a change as
possible is made here. The code has completely changed in main. Some
things like the new test and fixes for macos will be forward ported to
main, but I did not want to make larger changes in a bugfix.
rjernst added a commit that referenced this pull request Jul 15, 2024
Native preallocation has several issues, introduced in a refactoring for
8.13. First, the native allocator is never even tried, it always decides
to fall back to the Java setLength method. Second, the stat method did
not work correctly on all systems, see #110807. This commit fixes
native preallocate to properly execute on Linux, as well as MacOS. It
also adds direct tests of preallocation.

Note that this is meant as a bugfix for 8.15, so as minimal a change as
possible is made here. The code has completely changed in main. Some
things like the new test and fixes for macos will be forward ported to
main, but I did not want to make larger changes in a bugfix.
rjernst added a commit to rjernst/elasticsearch that referenced this pull request Oct 24, 2024
The test issue was fixed by elastic#110807

closes elastic#110801
elasticsearchmachine pushed a commit that referenced this pull request Oct 24, 2024
The test issue was fixed by #110807

closes #110801
georgewallace pushed a commit to georgewallace/elasticsearch that referenced this pull request Oct 25, 2024
jfreden pushed a commit to jfreden/elasticsearch that referenced this pull request Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Core Core issues without another label >non-issue Team:Core/Infra Meta label for core/infra team v8.16.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants