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

Causes UB by creating NULL reference #1

Open
RalfJung opened this issue Oct 18, 2019 · 0 comments
Open

Causes UB by creating NULL reference #1

RalfJung opened this issue Oct 18, 2019 · 0 comments

Comments

@RalfJung
Copy link

RalfJung commented Oct 18, 2019

This is a heads-up that the crate does not compile any more with the current Rust beta (to become stable Nov 7th). The reason is that there is UB in this const-code:

let x: &'static #ident = TransmuterPtr { int: 0 }.ptr;

References must always be non-null, dereferencable and aligned as well as point to a valid instance of T (see the Rust Reference for details). Violating that invariant is UB, and it is the responsibility of the unsafe code author to ensure that there is no UB.

Unfortunately, there never was a UB-free way to implement offsetof! in Rust. This operation is just not supported by the language right now, and the only way to fix that is to figure out what needs to change in the language to let Rust support this operation, and write an appropriate RFC. I did that and that RFC has in fact recently been accepted, but an implementation is still pending.

The memoffset crate uses an approach that's as good as we can currently make it, and we know it works with current compilers (including the latest nightly), but it could break any time as it, too, is outside the bounds of stable Rust. Also, the approach it uses does not work for CTFE; enabling that is still work-in-progress.

Is there a way for this crate to do the offset computation at run-time instead of trying to do it at compile-time? Then it could depend on memoffset to avoid this problem. I'm afraid for compile-time offsetof there is currently no solution.

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

1 participant