-
Notifications
You must be signed in to change notification settings - Fork 58
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
Is it UB for a uninhibited type, like the never type, value to exist? #337
Comments
This is definitely UB, and Miri complains about your example. |
Whoops, I was running |
After talking with @djkoloski, I didn't accurately represent what hyper is doing. Instead, what they're doing is ultimately transmuting a enum Impossible {}
#[repr(transparent)]
struct Item<T> {
_value: T,
_impossible: Impossible,
}
struct Container<T>(Vec<Item<T>>);
impl<T: std::fmt::Debug> Container<T> {
fn new(values: Vec<T>) -> Self {
assert_eq!(std::mem::size_of::<T>(), std::mem::size_of::<Item<T>>());
let values = unsafe { std::mem::transmute(values) };
Container(values)
}
fn print(&self) {
let values: &Vec<T> = unsafe { &*(&self.0 as *const _ as *const _) };
println!("{:?}", values);
}
}
fn main() {
let items = vec![5u8];
let container = Container::new(items);
container.print();
} This passes a miri check:
|
I think you meant I don't think the layout compatibility is guaranteed here: Other than layout issues, there should not be any problems with this code. Validity does not traverse the data pointer in |
Oops, thanks, I've corrected my example.
Thanks for this. Is that a property of pointer types? I swapped |
This is #77 . It is still open (not many issues actually get closed around here...) but the popular opinion seems to be that validity should not be recursive through pointer types, so |
Note however that
|
Is it safe to create a value that contains an uninhibited type, like the never type, if that value is never read from? For example:
This thread suggests that this is UB, because we require the
transmute
to execute. Running this will warn that the type is unreachable and that the value is uninhabited, but the program will compile, and fail with a SIGILL if the uninhibited value is accessed:This came up from a review of hyper's proto::h2 module. It feels a little suspicious to me, but
cargo miri
didn't complain about it.(Incidentally it appears this code is unnecessary, so we're trying to remove it).
The text was updated successfully, but these errors were encountered: