Skip to content

Fix the soundness bug in the representation of extern types#5021

Draft
tgross35 wants to merge 3 commits intorust-lang:mainfrom
tgross35:extern-types
Draft

Fix the soundness bug in the representation of extern types#5021
tgross35 wants to merge 3 commits intorust-lang:mainfrom
tgross35:extern-types

Conversation

@tgross35
Copy link
Contributor

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.

Cc @RalfJung

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
@rustbot
Copy link
Collaborator

rustbot commented Mar 16, 2026

Some changes occurred in a NetBSD-like module

cc @semarie

Some changes occurred in a solarish module

cc @jclulow, @pfmooney

@rustbot rustbot added the A-CI Area: CI-related items label Mar 16, 2026
@tgross35
Copy link
Contributor Author

Looks like I need to skip these in tests, and the ZST flags improper_ctypes until 1.72 https://rust.godbolt.org/z/TrdqKa4Wb.

@tgross35 tgross35 marked this pull request as draft March 16, 2026 22:42
The new extern type representation raises the lint prior to 1.72 because
ZSTs behind pointers were not allowed.
// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FIXME seems outdated now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-CI Area: CI-related items

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants