vptr icon indicating copy to clipboard operation
vptr copied to clipboard

Causes UB by creating NULL reference

Open RalfJung opened this issue 4 years ago • 0 comments

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:

https://github.com/ogoffart/vptr/blob/ca32aa416b97311afc569564eb08b2e18f1a4e15/macros/macros.rs#L125

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.

RalfJung avatar Oct 18 '19 08:10 RalfJung