Skip to content

[GR-75238] [GR-76153] Correctly capture errno.#992

Merged
graalvmbot merged 7 commits into
masterfrom
fa/GR-75238/correctly_capture_errno
Jun 29, 2026
Merged

[GR-75238] [GR-76153] Correctly capture errno.#992
graalvmbot merged 7 commits into
masterfrom
fa/GR-75238/correctly_capture_errno

Conversation

@graalvmbot

Copy link
Copy Markdown
Collaborator

This fixes a known issue with errno handling.
When calling native functions that set errno (i.e. most of the POSIX functions), we read the errno value after the downcall returned. However, at that time, errno may already have been clobbered by, e.g., the GC.

Therefore, Java's FFM API provides a mechanism to capture the value of errno before actually returning to Java (i.e. before the thread-state transition).

In this PR, I'm adding support for that. I've extended annotation @DowncallSignature such that one can specify if the call state should be captured. The captured state will be stored in a thread-local variable in NativeContext.
With NativeContext.getErrno() and NativeContext.getLastError(), one can access those values.

However, I also had a look at the hosted compiler graphs of the NativePosixSupport.PosixNativeFunctionInvoker.call_* methods and it turns out that those look not very good.
Further, FFM API call state capturing is unconditional but in case of a POSIX function returned w/o an error, errno does not need to be captured.
Since POSIX performance is crucial, I decided to do a custom solution for it.
Hence, in NativePosixSupport, we allocate a thread-local buffer where we store the captured errno value. The pointer to the buffer is passed as argument (name errno_out) to each call_* function. Our POSIX function wrappers then capture the value but only if needed (usually in the error case indicated by the return value). That should be even better than what we did before because we save additional downcalls to read errno.

@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Jun 26, 2026
@graalvmbot graalvmbot closed this Jun 29, 2026
@graalvmbot graalvmbot merged commit 3fd5d5f into master Jun 29, 2026
40 of 42 checks passed
@graalvmbot graalvmbot deleted the fa/GR-75238/correctly_capture_errno branch June 29, 2026 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants