-
Notifications
You must be signed in to change notification settings - Fork 58
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
Decide on the validity of function pointers #440
Comments
This comment was marked as outdated.
This comment was marked as outdated.
While I did make the argument to not bundle this with the other simple validity, I have no concerns with this actually being the validity requirement for function pointers. Even in the mentioned case where ABI expects function pointers to be aligned (say, to pack bool arguments as logically part of the ABI rather than part of the pointer), this can be made to be a requirement of the ABI rather than directly of function pointer layout. |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Now again with T-opsem label... |
Team member @RalfJung has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
Added to our FCP list of "decisions we made", so the issue can be closed. |
We decide
that the validity invariant for function pointers is: they need to be non-null.
Transmuting any provenance-free non-null input to a function pointer is definitely allowed without causing immediate UB.
When and whether these function pointers can actually be invoked is a different question and not answered here.
We do not decide what happens when the input has provenance. This is tracked here. In particular, values such as
&0
(that have provenance) might or might not be legal to transmute to function pointers.Rationale
The non-null requirement is already out there since forever and plenty of code relies on
Option<fn()>
having a null niche, so that part is not up for debate any more.We considered requiring function pointers to be aligned to some target-specific alignment. However, for all currently supported targets, that alignment would be 1. (Note that even on ARM, where functions have alignment requirements, the lowest bit is used to indicate Thumb mode. So while functions are aligned, function pointers are not.) Furthermore, C clearly allows casting 0 to function pointers; it will be hard to argue that casting 1 or -1 to a function pointer is UB in C, so a target that makes alignment requirements for function pointers a validity concern is likely to be incompatible with C. Common practice conforms this: for instance, 1 is a common value for
SIG_IGN
which is cast to a function pointer to be stored in thesa_handler
field ofsigaction
-- and while a new target could of course pick a higher-aligned value forSIG_IGN
, there is likely more C code out there using various sentinel values. SQLite uses-1
as a sentinel. Even Rust code using 1 as a sentinel function pointer has been spotted in the wild.Examples
The following pieces of code cause UB (as in, the UB arises when executing the code, not just potentially later):
The following piece of code is well-defined:
The following is not decided by this FCP:
The following functions are sound (as in, safe code invoking these functions can never have UB):
Prior discussion
The text was updated successfully, but these errors were encountered: