-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Conversation
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.
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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
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.
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.
The test issue was fixed by elastic#110807 closes elastic#110801
The test issue was fixed by elastic#110807 closes elastic#110801
The test issue was fixed by elastic#110807 closes elastic#110801
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.