Since nightly-2023-04-01, this Rust code can panic:
unsafe {*(1 as *const i32)}
because rustc now inserts a check that raw pointers are aligned before dereferencing them.
https://github.com/rust-lang/rust/pull/98112
This check is only inserted when debug assertions are enabled. If you have any notable experience with this, drop me a line!
Just based on the PR itself, so far this has caught a bug in the Rust test suite and three (3) bugs in real code.
Defaults are good. Checks are good.
Now 6 bugs in real code
Also possibly one bug in the implementation
I am working on another check similar to this that detects using bytes as a type which would be invalid because the type's niche is occupied by the bytes.
You can read the prototype code here: https://github.com/rust-lang/rust/pull/104862
For example, this check detects transmuting 0 to a NonZero type, or 4 to a bool.
It may also detect using raw pointers to write an invalid discriminant to an enum, but only if you try to access the bytes that you wrote as the enum type. This is the "validity on typed copy" rule that Miri implements. In a real sense, you can Fuck Around, so long as you don't tell the type system what you're doing.
@saethlin I'll be sure to only access my raw pointers via inline asm from now on!
@streganil @Lokathor That doesn't defeat the check! It makes your code well-defined :D
@saethlin No, it would still be UB ;P