Fix the soundness bug in the representation of extern types - #5021
Merged
Conversation
Collaborator
Contributor
Author
|
Looks like I need to skip these in tests, and the ZST flags |
tgross35
marked this pull request as draft
March 16, 2026 22:42
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 |
Contributor
Author
There was a problem hiding this comment.
You're right; we should still remove the traits but the reason needs updating (will fix)
JohnTitor
approved these changes
Mar 19, 2026
tgross35
force-pushed
the
extern-types
branch
3 times, most recently
from
June 24, 2026 04:16
bec3759 to
6be7f0b
Compare
tgross35
force-pushed
the
extern-types
branch
5 times, most recently
from
June 24, 2026 06:17
0eb46ed to
fd50378
Compare
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
The new extern type representation raises the lint prior to 1.72 because ZSTs behind pointers were not allowed.
tgross35
marked this pull request as ready for review
June 24, 2026 06:26
tgross35
enabled auto-merge
June 24, 2026 06:27
This was referenced Jun 24, 2026
Merged
tgross35
added a commit
to tgross35/rust-libc
that referenced
this pull request
Jul 20, 2026
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 (backport <rust-lang#5021>) (cherry picked from commit 1cf49df)
tgross35
added a commit
to tgross35/rust-libc
that referenced
this pull request
Jul 20, 2026
(backport <rust-lang#5021>) (cherry picked from commit fbaea87)
tgross35
added a commit
to tgross35/rust-libc
that referenced
this pull request
Jul 20, 2026
The new extern type representation raises the lint prior to 1.72 because ZSTs behind pointers were not allowed. (backport <rust-lang#5021>) (cherry picked from commit 96a27a4)
tgross35
added a commit
to tgross35/rust-libc
that referenced
this pull request
Jul 20, 2026
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 (backport <rust-lang#5021>) (cherry picked from commit 1cf49df)
tgross35
added a commit
to tgross35/rust-libc
that referenced
this pull request
Jul 20, 2026
(backport <rust-lang#5021>) (cherry picked from commit fbaea87)
tgross35
added a commit
to tgross35/rust-libc
that referenced
this pull request
Jul 20, 2026
The new extern type representation raises the lint prior to 1.72 because ZSTs behind pointers were not allowed. (backport <rust-lang#5021>) (cherry picked from commit 96a27a4)
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, technically 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