Fix the soundness bug in the representation of extern types#5021
Draft
tgross35 wants to merge 3 commits intorust-lang:mainfrom
Draft
Fix the soundness bug in the representation of extern types#5021tgross35 wants to merge 3 commits intorust-lang:mainfrom
tgross35 wants to merge 3 commits intorust-lang:mainfrom
Conversation
Since the very first import dafaca9 ("Initial import of liblibc"), `libc` has used uninhabited enums to represent C's incomplete/opaque types. While this is, as far as I know, techincally okay when working behind raw pointers, it means that using reference types like `&FILE` can lead to easy UB. Resolve this by changing the representation to a `!Sync + !Send + !Unpin` ZST, as recommended by the nomicon [1]. The loss of auto traits technically makes this user-visible, but it is unlikely that anybody who is doing sound things was relying on these. I also used this as an opportunity to add a forward-compatibility note about intended use that should allow us to switch to real extern types once those are available. [1]: https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs
Collaborator
Contributor
Author
|
Looks like I need to skip these in tests, and the ZST flags |
The new extern type representation raises the lint prior to 1.72 because ZSTs behind pointers were not allowed.
RalfJung
reviewed
Mar 17, 2026
| // Representation based on the Nomicon: | ||
| // <https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs>. | ||
| // | ||
| // FIXME(1.0): the type is uninhabited so these traits are unreachable and could be |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since the very first import dafaca9 ("Initial import of liblibc"),
libchas used uninhabited enums to represent C's incomplete/opaque types. While this is, as far as I know, techincally okay when working behind raw pointers, it means that using reference types like&FILEcan lead to easy UB.Resolve this by changing the representation to a
!Sync + !Send + !UnpinZST, as recommended by the nomicon 1. The loss of auto traits technically makes this user-visible, but it is unlikely that anybody who is doing sound things was relying on these.I also used this as an opportunity to add a forward-compatibility note about intended use that should allow us to switch to real extern types once those are available.
Cc @RalfJung