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

Libc.errno() and GC safepoints #55895

Open
vchuravy opened this issue Sep 26, 2024 · 4 comments
Open

Libc.errno() and GC safepoints #55895

vchuravy opened this issue Sep 26, 2024 · 4 comments

Comments

@vchuravy
Copy link
Member

errno is a peculiar function since one must guarantee that no other code was executed on the thread between the function that set errno and the query.

As far as I know we currently don't have a way to express that kind of atomicity, and we currently rely on the fact that in most cases the Libc.errno() function is inlined.

If that function were not inlined there is the possibility for it to hit the safepoint on function entry. As far as I know errno is not interrupt safe in general.

This does not seem to be a problem in practice, but we maybe need a function annotation that blocks safepoint on entry, or at the very least mark

errno() = ccall(:jl_errno, Cint, ())
as @inline.

We could also consider making sigatomic more user-friendly and recommending it's usage in the context of errno

@vtjnash
Copy link
Member

vtjnash commented Sep 26, 2024

errno is async and safepoint safe (and codegen, etc). Did you run into any counterexample that someone broke recently?

@gbaraldi
Copy link
Member

gbaraldi commented Sep 26, 2024

Wouldn't errno potentially get changed if we run a gc in between and somehow that thread does a syscall?

@vtjnash
Copy link
Member

vtjnash commented Sep 26, 2024

Only if someone has introduced a bug into the GC recently (which may be possible, as Windows is known to fairly eagerly smash errno, unlike posix which is only supposed to smash it on failures):

julia/src/gc-stock.c

Lines 3308 to 3311 in e4b29f7

int last_errno = errno;
#ifdef _OS_WINDOWS_
DWORD last_error = GetLastError();
#endif

@vchuravy
Copy link
Member Author

I don't have a concrete failure in mind, this was more prompted by a recent Slack conversation where I realized I didn't know the rules of the Game.

I see that the usr2 handler is restoring errno, but I didn't see the same logic in the segv handler for the safepoint. Now maybe all that code doesn't touch errno xD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants