You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
handle impossible errors from the kernel with error.Unexpected instead of unreachable #6389
enhancementSolving this issue will likely involve adding new logic or components to the codebase.standard libraryThis issue involves writing Zig code for the standard library.
On Linux, e.g. in os.zig the std lib will often interrogate errno and map to Zig errors, or unreachable if the std lib wants to assert that the std lib implementation would never cause an EINVAL or EFAULT, e.g.:
EINVAL => unreachable,
EFAULT => unreachable,
However, while implementing #6356, I was about to follow this pattern but then I realized that the kernel often overloads errors in new kernel versions, which is particularly the case for the io_uring syscalls.
This means that we might think our std lib implementation cannot cause EINVAL, and then the kernel adds a new feature which could, leading to undefined behavior instead of a safe error.
In other words, we need to start going through the std lib and make this usage of unreachable an anti-pattern because there's no way we can assert what the kernel can or cannot be returning like this.
Reacted by GalaxySnail, Pat Tullmann, logitprop, Padraig X. Lamont, 190n, Eric Joldasov, Joseph Harkness, Alex Rønne Petersen, Michael Pfaff, Lee Cannon and 2 more
changed the title [-]unsafe error handling in the std lib of overloaded kernel errors[/-][+]std: unsafe error handling of overloaded kernel errors[/+]on Sep 21, 2020
changed the title [-]std: unsafe error handling of overloaded kernel errors[/-][+]std: unsafe handling of errors overloaded by the kernel[/+]on Sep 21, 2020
Either that, or we need an unreachable which doesn't become undefined behavior in fast builds, or another separate keyword. To be clear, I don't mean to suggest it's necessarily a good idea to add keywords...
Do you have any example of a syscall that have changed its behaviour, except ioring stuff? If its specific to that feature, then do not use this pattern with these apis.
I haven't looked, but it's not about changing behavior so much as adding behavior, i.e. two error states (one old, one new) map to one error code, and Zig only checks one error state (because that was the only error state that existed at the time in the man pages) or assumes unreachable.
I don't know much about Linux; maybe I'm spouting nonsense, but couldn't the opposite, hypothetically, be true as well? If a syscall is "upgraded" to loosen its preconditions, then what was previously an error now succeeds. If the program relies on that logic, it breaks semantically (which is its own class of undefined behaviour).
To me it seems like the actual solution would be to instead put an upper limit on the supported kernel version (so do a version check and @panic at run time if it is too high/new). While it's a shame supporting every new release would mean having to manually verify none of the syscall behaviour has changed, fwict that's the only reliable way to go about it.
(Really there'd need to be a "compatibility profile" mode when these changes happen, like OpenGL has, though it's of course easier to just not change the interface. Assuming the latter, I too would be inclined to think this is an io_uring-specific growth pain / symptom, though again, I know nothing.)
but couldn't the opposite, hypothetically, be true as well? If a syscall is "upgraded" to loosen its preconditions, then what was previously an error now succeeds.
Thanks @rohlem, for the interesting inverse example. That's certainly true, but I think at least it wouldn't lead to undefined behavior from a safety point of view? The program simply wouldn't receive an error from the kernel, so this kind of code branch wouldn't be reached.
Unless, then again, it could be a safety issue if the program required the kernel to return an error, and fell back to unreachable if it did not.
To me it seems like the actual solution would be to instead put an upper limit on the supported kernel version (so do a version check and @Panic at run time if it is too high/new). While it's a shame supporting every new release would mean having to manually verify none of the syscall behaviour has changed, fwict that's the only reliable way to go about it.
Perhaps at first glance, but remember the kernel is free to backport features. Ultimately, checking kernel versions is a slippery slope and should be avoided in favor of relying on the error mechanism the kernel already exposes, albeit without using unreachable to assert what the kernel can/cannot return as an error.
Its expected that OSes have some backward compatibility, if the system has a breaking change thats not our problem (everything would break if for example read syscall suddenly changed). All documented errors should be handled and nothing more.
We discussed this in the meeting today and decided that we'll start by changing all => unreachable to => return error.Unexpected in std.posix and std.os.
We'll then consider a follow-up enhancement: Add an std.Options field which enables panicking in code paths that, barring silly seccomp behavior and the like, always indicate programmer error.
Reacted by Pat Tullmann, Padraig X. Lamont, Aurélien Vallée and Jari Vetoniemi
We discussed this in the meeting today and decided that we'll start by changing all => unreachable to => return error.Unexpected in std.posix and std.os.
Just noting that I'm postponing this until wrangle-writer-buffering is merged to avoid making Andrew's work on this branch even more of a slog than it already is.
enhancementSolving this issue will likely involve adding new logic or components to the codebase.standard libraryThis issue involves writing Zig code for the standard library.
Activity
[-]unsafe error handling in the std lib of overloaded kernel errors[/-][+]std: unsafe error handling of overloaded kernel errors[/+][-]std: unsafe error handling of overloaded kernel errors[/-][+]std: unsafe handling of errors overloaded by the kernel[/+]jorangreef commentedon Sep 21, 2020
Either that, or we need an
unreachablewhich doesn't become undefined behavior in fast builds, or another separate keyword. To be clear, I don't mean to suggest it's necessarily a good idea to add keywords...pfgithub commentedon Sep 21, 2020
I think
@panic()does thisRocknest commentedon Sep 21, 2020
Do you have any example of a syscall that have changed its behaviour, except ioring stuff? If its specific to that feature, then do not use this pattern with these apis.
jorangreef commentedon Sep 21, 2020
I haven't looked, but it's not about changing behavior so much as adding behavior, i.e. two error states (one old, one new) map to one error code, and Zig only checks one error state (because that was the only error state that existed at the time in the man pages) or assumes unreachable.
rohlem commentedon Sep 23, 2020
I don't know much about Linux; maybe I'm spouting nonsense, but couldn't the opposite, hypothetically, be true as well? If a syscall is "upgraded" to loosen its preconditions, then what was previously an error now succeeds. If the program relies on that logic, it breaks semantically (which is its own class of undefined behaviour).
To me it seems like the actual solution would be to instead put an upper limit on the supported kernel version (so do a version check and
@panicat run time if it is too high/new). While it's a shame supporting every new release would mean having to manually verify none of the syscall behaviour has changed, fwict that's the only reliable way to go about it.(Really there'd need to be a "compatibility profile" mode when these changes happen, like OpenGL has, though it's of course easier to just not change the interface. Assuming the latter, I too would be inclined to think this is an
io_uring-specific growth pain / symptom, though again, I know nothing.)jorangreef commentedon Sep 24, 2020
Thanks @rohlem, for the interesting inverse example. That's certainly true, but I think at least it wouldn't lead to undefined behavior from a safety point of view? The program simply wouldn't receive an error from the kernel, so this kind of code branch wouldn't be reached.
Unless, then again, it could be a safety issue if the program required the kernel to return an error, and fell back to unreachable if it did not.
Perhaps at first glance, but remember the kernel is free to backport features. Ultimately, checking kernel versions is a slippery slope and should be avoided in favor of relying on the error mechanism the kernel already exposes, albeit without using unreachable to assert what the kernel can/cannot return as an error.
Rocknest commentedon Sep 25, 2020
Its expected that OSes have some backward compatibility, if the system has a breaking change thats not our problem (everything would break if for example read syscall suddenly changed). All documented errors should be handled and nothing more.
33 remaining items
alexrp commentedon May 20, 2025
We discussed this in the meeting today and decided that we'll start by changing all
=> unreachableto=> return error.Unexpectedinstd.posixandstd.os.We'll then consider a follow-up enhancement: Add an
std.Optionsfield which enables panicking in code paths that, barring silly seccomp behavior and the like, always indicate programmer error.alexrp commentedon Jun 4, 2025
Just noting that I'm postponing this until
wrangle-writer-bufferingis merged to avoid making Andrew's work on this branch even more of a slog than it already is.alexrp commentedon Apr 24, 2026
I believe this is basically done with
std.Io.Threaded. Any further work can be follow-up issues.