Skip to content
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

FFI section suggests dangerous practice of using empty opaque type #29

Closed
RalfJung opened this issue Jun 24, 2017 · 5 comments
Closed

Comments

@RalfJung
Copy link
Member

The FFI section of the Nomicon currently suggests to use an empty type for "opaque pointer types". That seems pretty dangerous. This came up in the discussion at rust-lang/rfcs#1861, and also in https://internals.rust-lang.org/t/recent-change-to-make-exhaustiveness-and-uninhabited-types-play-nicer-together/4602 many were not happy with using the empty type here.

Until we have proper opaque types, the safer suggestion seems to be do use a ZST with a private field, rather than an empty type. Then at least the type is not actively lying.

@RalfJung RalfJung changed the title FFI section suggest dangerous practice of using empty opaque type FFI section suggests dangerous practice of using empty opaque type Jun 24, 2017
@sfackler
Copy link
Member

How is this dangerous?

@RalfJung
Copy link
Member Author

RalfJung commented Jun 24, 2017

with x: &Empty, match *x {} could well be (no final decision has been bade yet) insta-UB if ever executed.

Actually, just passing a &Empty to another function is not really different from passing aliasing &mut u32 -- the promise made by the type is violated. It is not unlikely for the unsafe code guidelines process to decide that doing either of these is UB.

@sfackler
Copy link
Member

&Empty is not okay, but you're working with *mut Empty and *const Empty in FFI.

@RalfJung
Copy link
Member Author

&Empty is not okay,

Glad we agree on that. :)

you're working with *mut Empty and *const Empty in FFI.

While these types are fine, I am worried that people will quickly move to variables or function arguments of type &Empty in the wrappers they build around their FFI code.

This happened e.g. in https://github.com/briansmith/ring/, where BIGNUM used to be an empty enum, and you can indeed find &BIGNUM in the code.

@SimonSapin
Copy link
Contributor

I’ve opened #44 to recommend zero-size structs with a private field, instead.

rust-lang/rust#45225 (comment) is another case of Bad Things happening with empty enums used in types that are not "impossible".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants