Similar to the existing nullpointer and alignment checks, this checks
for valid enum discriminants on creation of enums through unsafe
transmutes. Essentially this sanitizes patterns like the following:
```rust
let val: MyEnum = unsafe { std::mem::transmute<u32, MyEnum>(42) };
```
An extension of this check will be done in a follow-up that explicitly
sanitizes for extern enum values that come into Rust from e.g. C/C++.
This check is similar to Miri's capabilities of checking for valid
construction of enum values.
This PR is inspired by saethlin@'s PR
https://github.com/rust-lang/rust/pull/104862. Thank you so much for
keeping this code up and the detailed comments!
I also pair-programmed large parts of this together with vabr-g@.
22 lines
427 B
Rust
22 lines
427 B
Rust
//@ run-pass
|
|
//@ compile-flags: -C debug-assertions
|
|
|
|
#[allow(dead_code)]
|
|
enum Foo {
|
|
A,
|
|
B,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
struct Bar {
|
|
a: usize,
|
|
b: usize,
|
|
}
|
|
|
|
fn main() {
|
|
let _val: Option<(usize, Foo)> =
|
|
unsafe { std::mem::transmute::<_, Option<(usize, Foo)>>(Bar { a: 0, b: 0 }) };
|
|
let _val: Option<(usize, Foo)> =
|
|
unsafe { std::mem::transmute::<_, Option<(usize, Foo)>>(Bar { a: 1, b: 0 }) };
|
|
}
|