2019-11-03 16:21:37 +00:00
|
|
|
// run-pass
|
2021-12-03 15:32:51 +00:00
|
|
|
// needs-unwind
|
2022-05-23 15:44:05 +00:00
|
|
|
// revisions: mir thir strict
|
2021-05-27 18:37:49 +00:00
|
|
|
// [thir]compile-flags: -Zthir-unsafeck
|
2022-05-23 15:44:05 +00:00
|
|
|
// [strict]compile-flags: -Zstrict-init-checks
|
2021-09-20 16:22:55 +00:00
|
|
|
// ignore-tidy-linelength
|
2019-11-03 16:21:37 +00:00
|
|
|
|
|
|
|
// This test checks panic emitted from `mem::{uninitialized,zeroed}`.
|
|
|
|
|
2022-04-06 01:21:10 +00:00
|
|
|
#![feature(never_type)]
|
2019-11-03 16:21:37 +00:00
|
|
|
#![allow(deprecated, invalid_value)]
|
|
|
|
|
2019-11-09 11:24:26 +00:00
|
|
|
use std::{
|
2020-02-17 08:21:02 +00:00
|
|
|
mem::{self, MaybeUninit, ManuallyDrop},
|
2019-11-09 11:24:26 +00:00
|
|
|
panic,
|
|
|
|
ptr::NonNull,
|
2020-02-17 08:21:02 +00:00
|
|
|
num,
|
2019-11-09 11:24:26 +00:00
|
|
|
};
|
2019-11-03 16:21:37 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
struct Foo {
|
|
|
|
x: u8,
|
|
|
|
y: !,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Bar {}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
enum OneVariant { Variant(i32) }
|
|
|
|
|
2020-04-18 07:54:05 +00:00
|
|
|
#[allow(dead_code, non_camel_case_types)]
|
|
|
|
enum OneVariant_NonZero {
|
|
|
|
Variant(i32, i32, num::NonZeroI32),
|
|
|
|
DeadVariant(Bar),
|
|
|
|
}
|
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
#[allow(dead_code, non_camel_case_types)]
|
|
|
|
enum OneVariant_Ref {
|
|
|
|
Variant(&'static i32),
|
|
|
|
DeadVariant(Bar),
|
|
|
|
}
|
|
|
|
|
2020-04-18 07:54:05 +00:00
|
|
|
// An `Aggregate` abi enum where 0 is not a valid discriminant.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[repr(i32)]
|
|
|
|
enum NoNullVariant {
|
|
|
|
Variant1(i32, i32) = 1,
|
|
|
|
Variant2(i32, i32) = 2,
|
|
|
|
}
|
|
|
|
|
2020-02-17 08:21:02 +00:00
|
|
|
// An enum with ScalarPair layout
|
|
|
|
#[allow(dead_code)]
|
|
|
|
enum LR {
|
|
|
|
Left(i64),
|
|
|
|
Right(i64),
|
|
|
|
}
|
|
|
|
#[allow(dead_code, non_camel_case_types)]
|
|
|
|
enum LR_NonZero {
|
|
|
|
Left(num::NonZeroI64),
|
|
|
|
Right(num::NonZeroI64),
|
|
|
|
}
|
|
|
|
|
2022-05-23 15:44:05 +00:00
|
|
|
struct ZeroSized;
|
|
|
|
|
2022-07-14 21:42:47 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[repr(i32)]
|
|
|
|
enum ZeroIsValid {
|
|
|
|
Zero(u8) = 0,
|
|
|
|
One(NonNull<()>) = 1,
|
|
|
|
}
|
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
#[track_caller]
|
2019-11-03 16:21:37 +00:00
|
|
|
fn test_panic_msg<T>(op: impl (FnOnce() -> T) + panic::UnwindSafe, msg: &str) {
|
|
|
|
let err = panic::catch_unwind(op).err();
|
|
|
|
assert_eq!(
|
2020-10-19 20:36:45 +00:00
|
|
|
err.as_ref().and_then(|a| a.downcast_ref::<&str>()),
|
|
|
|
Some(&msg)
|
2019-11-03 16:21:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
#[track_caller]
|
|
|
|
fn test_panic_msg_only_if_strict<T>(op: impl (FnOnce() -> T) + panic::UnwindSafe, msg: &str) {
|
|
|
|
let err = panic::catch_unwind(op).err();
|
|
|
|
assert_eq!(
|
|
|
|
err.as_ref().and_then(|a| a.downcast_ref::<&str>()),
|
|
|
|
if cfg!(strict) { Some(&msg) } else { None },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-03 16:21:37 +00:00
|
|
|
fn main() {
|
|
|
|
unsafe {
|
2020-02-17 08:21:02 +00:00
|
|
|
// Uninhabited types
|
2019-11-03 16:21:37 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<!>(),
|
|
|
|
"attempted to instantiate uninhabited type `!`"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<!>(),
|
|
|
|
"attempted to instantiate uninhabited type `!`"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
2019-11-09 11:24:26 +00:00
|
|
|
|| MaybeUninit::<!>::uninit().assume_init(),
|
2019-11-03 16:21:37 +00:00
|
|
|
"attempted to instantiate uninhabited type `!`"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<Foo>(),
|
|
|
|
"attempted to instantiate uninhabited type `Foo`"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<Foo>(),
|
|
|
|
"attempted to instantiate uninhabited type `Foo`"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
2019-11-09 11:24:26 +00:00
|
|
|
|| MaybeUninit::<Foo>::uninit().assume_init(),
|
2019-11-03 16:21:37 +00:00
|
|
|
"attempted to instantiate uninhabited type `Foo`"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<Bar>(),
|
|
|
|
"attempted to instantiate uninhabited type `Bar`"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<Bar>(),
|
|
|
|
"attempted to instantiate uninhabited type `Bar`"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
2019-11-09 11:24:26 +00:00
|
|
|
|| MaybeUninit::<Bar>::uninit().assume_init(),
|
2019-11-03 16:21:37 +00:00
|
|
|
"attempted to instantiate uninhabited type `Bar`"
|
|
|
|
);
|
|
|
|
|
2022-03-27 18:10:19 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<[Foo; 2]>(),
|
|
|
|
"attempted to instantiate uninhabited type `[Foo; 2]`"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<[Foo; 2]>(),
|
|
|
|
"attempted to instantiate uninhabited type `[Foo; 2]`"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| MaybeUninit::<[Foo; 2]>::uninit().assume_init(),
|
|
|
|
"attempted to instantiate uninhabited type `[Foo; 2]`"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<[Bar; 2]>(),
|
|
|
|
"attempted to instantiate uninhabited type `[Bar; 2]`"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<[Bar; 2]>(),
|
|
|
|
"attempted to instantiate uninhabited type `[Bar; 2]`"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| MaybeUninit::<[Bar; 2]>::uninit().assume_init(),
|
|
|
|
"attempted to instantiate uninhabited type `[Bar; 2]`"
|
|
|
|
);
|
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
// Types that don't allow either.
|
2019-11-03 16:21:37 +00:00
|
|
|
test_panic_msg(
|
2022-09-20 09:33:16 +00:00
|
|
|
|| mem::zeroed::<&i32>(),
|
|
|
|
"attempted to zero-initialize type `&i32`, which is invalid"
|
2019-11-03 16:21:37 +00:00
|
|
|
);
|
|
|
|
test_panic_msg(
|
2022-09-20 09:33:16 +00:00
|
|
|
|| mem::uninitialized::<&i32>(),
|
|
|
|
"attempted to leave type `&i32` uninitialized, which is invalid"
|
2019-11-03 16:21:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
2022-09-20 09:33:16 +00:00
|
|
|
|| mem::zeroed::<Box<[i32; 0]>>(),
|
|
|
|
"attempted to zero-initialize type `alloc::boxed::Box<[i32; 0]>`, which is invalid"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<Box<[i32; 0]>>(),
|
|
|
|
"attempted to leave type `alloc::boxed::Box<[i32; 0]>` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<Box<u8>>(),
|
|
|
|
"attempted to zero-initialize type `alloc::boxed::Box<u8>`, which is invalid"
|
2019-11-03 16:21:37 +00:00
|
|
|
);
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<Box<u8>>(),
|
|
|
|
"attempted to leave type `alloc::boxed::Box<u8>` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<&[i32]>(),
|
|
|
|
"attempted to zero-initialize type `&[i32]`, which is invalid"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<&[i32]>(),
|
|
|
|
"attempted to leave type `&[i32]` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<&(u8, [u8])>(),
|
|
|
|
"attempted to zero-initialize type `&(u8, [u8])`, which is invalid"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<&(u8, [u8])>(),
|
|
|
|
"attempted to leave type `&(u8, [u8])` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<&dyn Send>(),
|
|
|
|
"attempted to zero-initialize type `&dyn core::marker::Send`, which is invalid"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<&dyn Send>(),
|
|
|
|
"attempted to leave type `&dyn core::marker::Send` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
2019-11-03 16:21:37 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<*const dyn Send>(),
|
2021-09-20 16:22:55 +00:00
|
|
|
"attempted to zero-initialize type `*const dyn core::marker::Send`, which is invalid"
|
2019-11-03 16:21:37 +00:00
|
|
|
);
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<*const dyn Send>(),
|
|
|
|
"attempted to leave type `*const dyn core::marker::Send` uninitialized, which is invalid"
|
|
|
|
);
|
2019-11-03 16:21:37 +00:00
|
|
|
|
|
|
|
test_panic_msg(
|
2022-09-20 09:33:16 +00:00
|
|
|
|| mem::uninitialized::<NoNullVariant>(),
|
|
|
|
"attempted to leave type `NoNullVariant` uninitialized, \
|
|
|
|
which is invalid"
|
|
|
|
);
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<NoNullVariant>(),
|
|
|
|
"attempted to zero-initialize type `NoNullVariant`, \
|
2019-11-03 16:21:37 +00:00
|
|
|
which is invalid"
|
|
|
|
);
|
2022-07-14 21:42:47 +00:00
|
|
|
|
2019-11-03 16:21:37 +00:00
|
|
|
test_panic_msg(
|
2022-09-20 09:33:16 +00:00
|
|
|
|| mem::zeroed::<OneVariant_Ref>(),
|
|
|
|
"attempted to zero-initialize type `OneVariant_Ref`, \
|
2019-11-03 16:21:37 +00:00
|
|
|
which is invalid"
|
|
|
|
);
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<OneVariant_Ref>(),
|
|
|
|
"attempted to leave type `OneVariant_Ref` uninitialized, which is invalid"
|
|
|
|
);
|
2020-04-18 07:54:05 +00:00
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
// Types where both are invalid, but we allow uninit since the 0x01-filling is not LLVM UB.
|
2020-04-18 07:54:05 +00:00
|
|
|
test_panic_msg(
|
2022-09-20 09:33:16 +00:00
|
|
|
|| mem::zeroed::<fn()>(),
|
|
|
|
"attempted to zero-initialize type `fn()`, which is invalid"
|
|
|
|
);
|
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<fn()>(),
|
|
|
|
"attempted to leave type `fn()` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<&()>(),
|
|
|
|
"attempted to zero-initialize type `&()`, which is invalid"
|
|
|
|
);
|
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<&()>(),
|
|
|
|
"attempted to leave type `&()` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<&[u8]>(),
|
|
|
|
"attempted to zero-initialize type `&[u8]`, which is invalid"
|
|
|
|
);
|
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<&[u8]>(),
|
|
|
|
"attempted to leave type `&[u8]` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<&str>(),
|
|
|
|
"attempted to zero-initialize type `&str`, which is invalid"
|
|
|
|
);
|
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<&str>(),
|
|
|
|
"attempted to leave type `&str` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<(NonNull<u32>, u32, u32)>(),
|
|
|
|
"attempted to zero-initialize type `(core::ptr::non_null::NonNull<u32>, u32, u32)`, \
|
2020-04-18 07:54:05 +00:00
|
|
|
which is invalid"
|
|
|
|
);
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<(NonNull<u32>, u32, u32)>(),
|
|
|
|
"attempted to leave type `(core::ptr::non_null::NonNull<u32>, u32, u32)` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
2020-04-18 07:54:05 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::zeroed::<OneVariant_NonZero>(),
|
|
|
|
"attempted to zero-initialize type `OneVariant_NonZero`, \
|
|
|
|
which is invalid"
|
|
|
|
);
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<OneVariant_NonZero>(),
|
|
|
|
"attempted to leave type `OneVariant_NonZero` uninitialized, which is invalid"
|
|
|
|
);
|
2020-04-18 07:54:05 +00:00
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
// Types where both are invalid but we allow the zeroed form since it is not LLVM UB.
|
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::zeroed::<LR_NonZero>(),
|
|
|
|
"attempted to zero-initialize type `LR_NonZero`, which is invalid"
|
|
|
|
);
|
2022-07-14 21:42:47 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<LR_NonZero>(),
|
|
|
|
"attempted to leave type `LR_NonZero` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::zeroed::<ManuallyDrop<LR_NonZero>>(),
|
|
|
|
"attempted to zero-initialize type `core::mem::manually_drop::ManuallyDrop<LR_NonZero>`, \
|
|
|
|
which is invalid"
|
|
|
|
);
|
2022-07-14 21:42:47 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<ManuallyDrop<LR_NonZero>>(),
|
|
|
|
"attempted to leave type `core::mem::manually_drop::ManuallyDrop<LR_NonZero>` uninitialized, \
|
|
|
|
which is invalid"
|
|
|
|
);
|
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
// Some strict-only things
|
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<i32>(),
|
|
|
|
"attempted to leave type `i32` uninitialized, which is invalid"
|
2020-04-18 07:54:05 +00:00
|
|
|
);
|
2022-07-14 21:42:47 +00:00
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<*const ()>(),
|
|
|
|
"attempted to leave type `*const ()` uninitialized, which is invalid"
|
2020-04-18 07:54:05 +00:00
|
|
|
);
|
2019-11-03 16:21:37 +00:00
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<[i32; 1]>(),
|
|
|
|
"attempted to leave type `[i32; 1]` uninitialized, which is invalid"
|
2019-11-03 16:21:37 +00:00
|
|
|
);
|
2022-07-14 21:42:47 +00:00
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::zeroed::<[NonNull<()>; 1]>(),
|
|
|
|
"attempted to zero-initialize type `[core::ptr::non_null::NonNull<()>; 1]`, which is invalid"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Types that can be zero, but not uninit (though some are mitigated).
|
|
|
|
let _val = mem::zeroed::<LR>();
|
2020-02-17 08:21:02 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<LR>(),
|
|
|
|
"attempted to leave type `LR` uninitialized, which is invalid"
|
|
|
|
);
|
2022-07-14 21:42:47 +00:00
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
let _val = mem::zeroed::<ManuallyDrop<LR>>();
|
2020-02-17 08:21:02 +00:00
|
|
|
test_panic_msg(
|
|
|
|
|| mem::uninitialized::<ManuallyDrop<LR>>(),
|
2021-09-20 16:22:55 +00:00
|
|
|
"attempted to leave type `core::mem::manually_drop::ManuallyDrop<LR>` uninitialized, which is invalid"
|
2020-02-17 08:21:02 +00:00
|
|
|
);
|
2019-11-03 16:21:37 +00:00
|
|
|
|
|
|
|
let _val = mem::zeroed::<bool>();
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<bool>(),
|
|
|
|
"attempted to leave type `bool` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
2019-11-03 16:21:37 +00:00
|
|
|
let _val = mem::zeroed::<OneVariant>();
|
2022-09-20 09:33:16 +00:00
|
|
|
test_panic_msg_only_if_strict(
|
|
|
|
|| mem::uninitialized::<OneVariant>(),
|
|
|
|
"attempted to leave type `OneVariant` uninitialized, which is invalid"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Some things that are actually allowed.
|
2019-11-03 16:21:37 +00:00
|
|
|
let _val = mem::zeroed::<Option<&'static i32>>();
|
2019-11-09 11:24:26 +00:00
|
|
|
let _val = mem::zeroed::<MaybeUninit<NonNull<u32>>>();
|
2022-03-27 18:10:19 +00:00
|
|
|
let _val = mem::zeroed::<[!; 0]>();
|
2022-07-14 21:42:47 +00:00
|
|
|
let _val = mem::zeroed::<ZeroIsValid>();
|
2019-11-09 11:24:26 +00:00
|
|
|
let _val = mem::uninitialized::<MaybeUninit<bool>>();
|
2022-03-27 18:10:19 +00:00
|
|
|
let _val = mem::uninitialized::<[!; 0]>();
|
2022-05-23 15:44:05 +00:00
|
|
|
let _val = mem::uninitialized::<()>();
|
|
|
|
let _val = mem::uninitialized::<ZeroSized>();
|
2019-11-03 16:21:37 +00:00
|
|
|
}
|
|
|
|
}
|