From f4b98dea73ae52ab110fa80e0e0e419d1e86e500 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 3 Nov 2019 17:21:37 +0100 Subject: [PATCH 01/15] mem::zeroed/uninit: panic on types that do not permit zero-initialization --- src/libcore/intrinsics.rs | 5 + src/libcore/mem/mod.rs | 6 ++ src/librustc/ty/layout.rs | 30 ------ src/librustc_codegen_ssa/mir/block.rs | 25 ++++- src/librustc_index/vec.rs | 6 +- src/librustc_target/abi/mod.rs | 89 +++++++++++++++ src/librustc_target/lib.rs | 3 + src/librustc_typeck/check/intrinsic.rs | 1 + .../intrinsics/panic-uninitialized-zeroed.rs | 98 +++++++++++++++++ .../never_type/panic-uninitialized-zeroed.rs | 102 ------------------ 10 files changed, 227 insertions(+), 138 deletions(-) create mode 100644 src/test/ui/intrinsics/panic-uninitialized-zeroed.rs delete mode 100644 src/test/ui/never_type/panic-uninitialized-zeroed.rs diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index b02acce2d00bb..0c187ee98fac3 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -696,6 +696,11 @@ extern "rust-intrinsic" { /// This will statically either panic, or do nothing. pub fn panic_if_uninhabited(); + /// A guard for unsafe functions that cannot ever be executed if `T` does not permit + /// zero-initialization: This will statically either panic, or do nothing. + #[cfg(not(bootstrap))] + pub fn panic_if_non_zero(); + /// Gets a reference to a static `Location` indicating where it was called. pub fn caller_location() -> &'static crate::panic::Location<'static>; diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs index 4e8ba8131f7a6..41dfc47d9c462 100644 --- a/src/libcore/mem/mod.rs +++ b/src/libcore/mem/mod.rs @@ -495,6 +495,9 @@ pub const fn needs_drop() -> bool { #[allow(deprecated)] #[rustc_diagnostic_item = "mem_zeroed"] pub unsafe fn zeroed() -> T { + #[cfg(not(bootstrap))] + intrinsics::panic_if_non_zero::(); + #[cfg(bootstrap)] intrinsics::panic_if_uninhabited::(); intrinsics::init() } @@ -528,6 +531,9 @@ pub unsafe fn zeroed() -> T { #[allow(deprecated)] #[rustc_diagnostic_item = "mem_uninitialized"] pub unsafe fn uninitialized() -> T { + #[cfg(not(bootstrap))] + intrinsics::panic_if_non_zero::(); + #[cfg(bootstrap)] intrinsics::panic_if_uninhabited::(); intrinsics::uninit() } diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index c7278dc4fac70..c76b61303ed76 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -1910,36 +1910,6 @@ impl<'tcx, T: HasTyCtxt<'tcx>> HasTyCtxt<'tcx> for LayoutCx<'tcx, T> { } } -pub trait MaybeResult { - type Error; - - fn from(x: Result) -> Self; - fn to_result(self) -> Result; -} - -impl MaybeResult for T { - type Error = !; - - fn from(x: Result) -> Self { - let Ok(x) = x; - x - } - fn to_result(self) -> Result { - Ok(self) - } -} - -impl MaybeResult for Result { - type Error = E; - - fn from(x: Result) -> Self { - x - } - fn to_result(self) -> Result { - self - } -} - pub type TyLayout<'tcx> = ::rustc_target::abi::TyLayout<'tcx, Ty<'tcx>>; impl<'tcx> LayoutOf for LayoutCx<'tcx, TyCtxt<'tcx>> { diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs index dabd097b000b5..a70fac9c7e1c6 100644 --- a/src/librustc_codegen_ssa/mir/block.rs +++ b/src/librustc_codegen_ssa/mir/block.rs @@ -541,11 +541,30 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } // Emit a panic or a no-op for `panic_if_uninhabited`. - if intrinsic == Some("panic_if_uninhabited") { + // These are intrinsics that compile to panics so that we can get panics + // which mention the offending type, even from a const context. + #[derive(Debug, PartialEq)] + enum PanicIntrinsic { IfUninhabited, IfNonZero }; + let panic_intrinsic = intrinsic.and_then(|i| match i { + "panic_if_uninhabited" => Some(PanicIntrinsic::IfUninhabited), + "panic_if_non_zero" => Some(PanicIntrinsic::IfNonZero), + _ => None + }); + if let Some(intrinsic) = panic_intrinsic { + use PanicIntrinsic::*; let ty = instance.unwrap().substs.type_at(0); let layout = bx.layout_of(ty); - if layout.abi.is_uninhabited() { - let msg_str = format!("Attempted to instantiate uninhabited type {}", ty); + let do_panic = match intrinsic { + IfUninhabited => layout.abi.is_uninhabited(), + IfNonZero => !layout.might_permit_zero_init(&bx).unwrap(), // error type is `!` + }; + if do_panic { + let msg_str = if layout.abi.is_uninhabited() { + // Use this error even for IfNonZero as it is more precise. + format!("attempted to instantiate uninhabited type `{}`", ty) + } else { + format!("attempted to zero-initialize non-zero type `{}`", ty) + }; let msg = bx.const_str(Symbol::intern(&msg_str)); let location = self.get_caller_location(&mut bx, span).immediate(); diff --git a/src/librustc_index/vec.rs b/src/librustc_index/vec.rs index 6e80b48a68560..7e5efcfb30f4a 100644 --- a/src/librustc_index/vec.rs +++ b/src/librustc_index/vec.rs @@ -182,12 +182,12 @@ macro_rules! newtype_index { impl Idx for $type { #[inline] fn new(value: usize) -> Self { - Self::from(value) + Self::from_usize(value) } #[inline] fn index(self) -> usize { - usize::from(self) + self.as_usize() } } @@ -400,7 +400,7 @@ macro_rules! newtype_index { (@decodable $type:ident) => ( impl ::rustc_serialize::Decodable for $type { fn decode(d: &mut D) -> Result { - d.read_u32().map(Self::from) + d.read_u32().map(Self::from_u32) } } ); diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index b0bc052929768..af5e32edd2188 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -971,6 +971,7 @@ impl<'a, Ty> Deref for TyLayout<'a, Ty> { } } +/// Trait for context types that can compute layouts of things. pub trait LayoutOf { type Ty; type TyLayout; @@ -981,6 +982,39 @@ pub trait LayoutOf { } } +/// The `TyLayout` above will always be a `MaybeResult>`. +/// We can't add the bound due to the lifetime, but this trait is still useful when +/// writing code that's generic over the `LayoutOf` impl. +pub trait MaybeResult { + type Error; + + fn from(x: Result) -> Self; + fn to_result(self) -> Result; +} + +impl MaybeResult for T { + type Error = !; + + fn from(x: Result) -> Self { + let Ok(x) = x; + x + } + fn to_result(self) -> Result { + Ok(self) + } +} + +impl MaybeResult for Result { + type Error = E; + + fn from(x: Result) -> Self { + x + } + fn to_result(self) -> Result { + self + } +} + #[derive(Copy, Clone, PartialEq, Eq)] pub enum PointerKind { /// Most general case, we know no restrictions to tell LLVM. @@ -1022,10 +1056,14 @@ impl<'a, Ty> TyLayout<'a, Ty> { where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { Ty::for_variant(self, cx, variant_index) } + + /// Callers might want to use `C: LayoutOf>` + /// to allow recursion (see `might_permit_zero_init` below for an example). pub fn field(self, cx: &C, i: usize) -> C::TyLayout where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { Ty::field(self, cx, i) } + pub fn pointee_info_at(self, cx: &C, offset: Size) -> Option where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { Ty::pointee_info_at(self, cx, offset) @@ -1048,4 +1086,55 @@ impl<'a, Ty> TyLayout<'a, Ty> { Abi::Aggregate { sized } => sized && self.size.bytes() == 0 } } + + /// Determines if zero-initializing this type might be okay. + /// This is conservative: in doubt, it will answer `true`. + pub fn might_permit_zero_init( + &self, + cx: &C, + ) -> Result + where + Self: Copy, + Ty: TyLayoutMethods<'a, C>, + C: LayoutOf> + { + fn scalar_allows_zero(s: &Scalar) -> bool { + (*s.valid_range.start() <= 0) || // `&& *s.valid_range.end() >= 0` would be redundant + (*s.valid_range.start() > *s.valid_range.end()) // wrap-around allows 0 + } + + // Abi is the most informative here. + let res = match &self.abi { + Abi::Uninhabited => false, // definitely UB + Abi::Scalar(s) => scalar_allows_zero(s), + Abi::ScalarPair(s1, s2) => + scalar_allows_zero(s1) && scalar_allows_zero(s2), + Abi::Vector { element: s, count } => + *count == 0 || scalar_allows_zero(s), + Abi::Aggregate { .. } => { + // For aggregates, recurse. + let inner = match self.variants { + Variants::Multiple { .. } => // FIXME: get variant with "0" discriminant. + return Ok(true), + Variants::Single { index } => self.for_variant(&cx, index), + }; + + match inner.fields { + FieldPlacement::Union(..) => true, // An all-0 unit is fine. + FieldPlacement::Array { count, .. } => + count == 0 || // 0-length arrays are always okay. + inner.field(cx, 0).to_result()?.might_permit_zero_init(cx)?, + FieldPlacement::Arbitrary { ref offsets, .. } => + // Check that all fields accept zero-init. + (0..offsets.len()).try_fold(true, |accu, idx| + Ok(accu && + inner.field(cx, idx).to_result()?.might_permit_zero_init(cx)? + ) + )? + } + } + }; + trace!("might_permit_zero_init({:?}) = {}", self.details, res); + Ok(res) + } } diff --git a/src/librustc_target/lib.rs b/src/librustc_target/lib.rs index 6a1498e98e816..4fa2d2e5bf73c 100644 --- a/src/librustc_target/lib.rs +++ b/src/librustc_target/lib.rs @@ -13,6 +13,9 @@ #![feature(bool_to_option)] #![feature(nll)] #![feature(slice_patterns)] +#![feature(never_type)] +#![feature(associated_type_bounds)] +#![feature(exhaustive_patterns)] #[macro_use] extern crate log; diff --git a/src/librustc_typeck/check/intrinsic.rs b/src/librustc_typeck/check/intrinsic.rs index cec831147c44f..cff7441bdbb01 100644 --- a/src/librustc_typeck/check/intrinsic.rs +++ b/src/librustc_typeck/check/intrinsic.rs @@ -149,6 +149,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) { "rustc_peek" => (1, vec![param(0)], param(0)), "caller_location" => (0, vec![], tcx.caller_location_ty()), "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()), + "panic_if_non_zero" => (1, Vec::new(), tcx.mk_unit()), "init" => (1, Vec::new(), param(0)), "uninit" => (1, Vec::new(), param(0)), "forget" => (1, vec![param(0)], tcx.mk_unit()), diff --git a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs new file mode 100644 index 0000000000000..ec1b0eb83ff65 --- /dev/null +++ b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs @@ -0,0 +1,98 @@ +// run-pass +// ignore-wasm32-bare compiled with panic=abort by default + +// This test checks panic emitted from `mem::{uninitialized,zeroed}`. + +#![feature(never_type)] +#![allow(deprecated, invalid_value)] + +use std::{mem, panic}; +use std::ptr::NonNull; + +#[allow(dead_code)] +struct Foo { + x: u8, + y: !, +} + +enum Bar {} + +fn test_panic_msg(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::()).map(|s| &**s), + Some(msg) + ); +} + +fn main() { + unsafe { + // Uninitialized types + test_panic_msg( + || mem::uninitialized::(), + "attempted to instantiate uninhabited type `!`" + ); + test_panic_msg( + || mem::zeroed::(), + "attempted to instantiate uninhabited type `!`" + ); + test_panic_msg( + || mem::MaybeUninit::::uninit().assume_init(), + "attempted to instantiate uninhabited type `!`" + ); + + test_panic_msg( + || mem::uninitialized::(), + "attempted to instantiate uninhabited type `Foo`" + ); + test_panic_msg( + || mem::zeroed::(), + "attempted to instantiate uninhabited type `Foo`" + ); + test_panic_msg( + || mem::MaybeUninit::::uninit().assume_init(), + "attempted to instantiate uninhabited type `Foo`" + ); + + test_panic_msg( + || mem::uninitialized::(), + "attempted to instantiate uninhabited type `Bar`" + ); + test_panic_msg( + || mem::zeroed::(), + "attempted to instantiate uninhabited type `Bar`" + ); + test_panic_msg( + || mem::MaybeUninit::::uninit().assume_init(), + "attempted to instantiate uninhabited type `Bar`" + ); + + // Types that do not like zero-initialziation + test_panic_msg( + || mem::uninitialized::(), + "attempted to zero-initialize non-zero type `fn()`" + ); + test_panic_msg( + || mem::zeroed::(), + "attempted to zero-initialize non-zero type `fn()`" + ); + + test_panic_msg( + || mem::uninitialized::<*const dyn Send>(), + "attempted to zero-initialize non-zero type `*const dyn std::marker::Send`" + ); + test_panic_msg( + || mem::zeroed::<*const dyn Send>(), + "attempted to zero-initialize non-zero type `*const dyn std::marker::Send`" + ); + + test_panic_msg( + || mem::uninitialized::<(NonNull, u32, u32)>(), + "attempted to zero-initialize non-zero type `(std::ptr::NonNull, u32, u32)`" + ); + test_panic_msg( + || mem::zeroed::<(NonNull, u32, u32)>(), + "attempted to zero-initialize non-zero type `(std::ptr::NonNull, u32, u32)`" + ); + } +} diff --git a/src/test/ui/never_type/panic-uninitialized-zeroed.rs b/src/test/ui/never_type/panic-uninitialized-zeroed.rs deleted file mode 100644 index e0c30160b9e94..0000000000000 --- a/src/test/ui/never_type/panic-uninitialized-zeroed.rs +++ /dev/null @@ -1,102 +0,0 @@ -// run-pass -// ignore-wasm32-bare compiled with panic=abort by default -// This test checks that instantiating an uninhabited type via `mem::{uninitialized,zeroed}` results -// in a runtime panic. - -#![feature(never_type)] -#![allow(deprecated, invalid_value)] - -use std::{mem, panic}; - -#[allow(dead_code)] -struct Foo { - x: u8, - y: !, -} - -enum Bar {} - -fn main() { - unsafe { - assert_eq!( - panic::catch_unwind(|| { - mem::uninitialized::() - }).err().and_then(|a| a.downcast_ref::().map(|s| { - s == "Attempted to instantiate uninhabited type !" - })), - Some(true) - ); - - assert_eq!( - panic::catch_unwind(|| { - mem::zeroed::() - }).err().and_then(|a| a.downcast_ref::().map(|s| { - s == "Attempted to instantiate uninhabited type !" - })), - Some(true) - ); - - assert_eq!( - panic::catch_unwind(|| { - mem::MaybeUninit::::uninit().assume_init() - }).err().and_then(|a| a.downcast_ref::().map(|s| { - s == "Attempted to instantiate uninhabited type !" - })), - Some(true) - ); - - assert_eq!( - panic::catch_unwind(|| { - mem::uninitialized::() - }).err().and_then(|a| a.downcast_ref::().map(|s| { - s == "Attempted to instantiate uninhabited type Foo" - })), - Some(true) - ); - - assert_eq!( - panic::catch_unwind(|| { - mem::zeroed::() - }).err().and_then(|a| a.downcast_ref::().map(|s| { - s == "Attempted to instantiate uninhabited type Foo" - })), - Some(true) - ); - - assert_eq!( - panic::catch_unwind(|| { - mem::MaybeUninit::::uninit().assume_init() - }).err().and_then(|a| a.downcast_ref::().map(|s| { - s == "Attempted to instantiate uninhabited type Foo" - })), - Some(true) - ); - - assert_eq!( - panic::catch_unwind(|| { - mem::uninitialized::() - }).err().and_then(|a| a.downcast_ref::().map(|s| { - s == "Attempted to instantiate uninhabited type Bar" - })), - Some(true) - ); - - assert_eq!( - panic::catch_unwind(|| { - mem::zeroed::() - }).err().and_then(|a| a.downcast_ref::().map(|s| { - s == "Attempted to instantiate uninhabited type Bar" - })), - Some(true) - ); - - assert_eq!( - panic::catch_unwind(|| { - mem::MaybeUninit::::uninit().assume_init() - }).err().and_then(|a| a.downcast_ref::().map(|s| { - s == "Attempted to instantiate uninhabited type Bar" - })), - Some(true) - ); - } -} From 7734560ce4b8cfed705d5d0225a50c441a066dda Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 3 Nov 2019 17:31:09 +0100 Subject: [PATCH 02/15] clarify comment --- src/librustc_codegen_ssa/mir/block.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs index a70fac9c7e1c6..9febf51992a76 100644 --- a/src/librustc_codegen_ssa/mir/block.rs +++ b/src/librustc_codegen_ssa/mir/block.rs @@ -541,8 +541,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } // Emit a panic or a no-op for `panic_if_uninhabited`. - // These are intrinsics that compile to panics so that we can get panics - // which mention the offending type, even from a const context. + // These are intrinsics that compile to panics so that we can get a message + // which mentions the offending type, even from a const context. #[derive(Debug, PartialEq)] enum PanicIntrinsic { IfUninhabited, IfNonZero }; let panic_intrinsic = intrinsic.and_then(|i| match i { From 5dab4ebb5c3c1094299cd00754ae07f6c48d6d17 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 4 Nov 2019 08:41:27 +0100 Subject: [PATCH 03/15] also test some things to not panic --- src/test/ui/intrinsics/panic-uninitialized-zeroed.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs index ec1b0eb83ff65..75c2fb87000e1 100644 --- a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs +++ b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs @@ -17,6 +17,9 @@ struct Foo { enum Bar {} +#[allow(dead_code)] +enum OneVariant { Variant(i32) } + fn test_panic_msg(op: impl (FnOnce() -> T) + panic::UnwindSafe, msg: &str) { let err = panic::catch_unwind(op).err(); assert_eq!( @@ -94,5 +97,10 @@ fn main() { || mem::zeroed::<(NonNull, u32, u32)>(), "attempted to zero-initialize non-zero type `(std::ptr::NonNull, u32, u32)`" ); + + // Some things that should work. + let _val = mem::zeroed::(); + let _val = mem::zeroed::(); + let _val = mem::zeroed::>(); } } From ec8c51ef11b6f2cde32a145b218be6c49da3798c Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 4 Nov 2019 16:35:44 +0100 Subject: [PATCH 04/15] don't check in arrays, to give smallvec users some time --- src/librustc_target/abi/mod.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index af5e32edd2188..22433edc86afa 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -1121,9 +1121,11 @@ impl<'a, Ty> TyLayout<'a, Ty> { match inner.fields { FieldPlacement::Union(..) => true, // An all-0 unit is fine. - FieldPlacement::Array { count, .. } => - count == 0 || // 0-length arrays are always okay. - inner.field(cx, 0).to_result()?.might_permit_zero_init(cx)?, + FieldPlacement::Array { .. } => + // FIXME: The widely use smallvec 0.6 creates uninit arrays + // with any element type, so let us not (yet) complain about that. + // count == 0 || inner.field(cx, 0).to_result()?.might_permit_zero_init(cx)? + true, FieldPlacement::Arbitrary { ref offsets, .. } => // Check that all fields accept zero-init. (0..offsets.len()).try_fold(true, |accu, idx| From 598df90820cd4dc15d472773ab35e6a65851b069 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 5 Nov 2019 13:00:18 +0100 Subject: [PATCH 05/15] more readable test if range contains 0 --- src/librustc_target/abi/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 22433edc86afa..62ddd15cdac0b 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -1099,7 +1099,7 @@ impl<'a, Ty> TyLayout<'a, Ty> { C: LayoutOf> { fn scalar_allows_zero(s: &Scalar) -> bool { - (*s.valid_range.start() <= 0) || // `&& *s.valid_range.end() >= 0` would be redundant + s.valid_range.contains(&0) || (*s.valid_range.start() > *s.valid_range.end()) // wrap-around allows 0 } From 0317a2293e5e5c7e1e57e46682af5b3a006da27c Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 5 Nov 2019 13:05:01 +0100 Subject: [PATCH 06/15] avoid try_fold --- src/librustc_target/abi/mod.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 62ddd15cdac0b..09f642c24c8ae 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -1126,13 +1126,16 @@ impl<'a, Ty> TyLayout<'a, Ty> { // with any element type, so let us not (yet) complain about that. // count == 0 || inner.field(cx, 0).to_result()?.might_permit_zero_init(cx)? true, - FieldPlacement::Arbitrary { ref offsets, .. } => + FieldPlacement::Arbitrary { ref offsets, .. } => { // Check that all fields accept zero-init. - (0..offsets.len()).try_fold(true, |accu, idx| - Ok(accu && - inner.field(cx, idx).to_result()?.might_permit_zero_init(cx)? - ) - )? + for idx in 0..offsets.len() { + if !inner.field(cx, idx).to_result()?.might_permit_zero_init(cx)? { + return Ok(false); + } + } + // No bad field found, we are good. + true + } } } }; From e702fc68351769ab201caab8ac8f9b8cc12c9163 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 5 Nov 2019 13:36:14 +0100 Subject: [PATCH 07/15] add separate intrinsic for uninit memory, and do a stronger check there --- src/libcore/intrinsics.rs | 7 +++- src/libcore/mem/mod.rs | 4 +- src/librustc_codegen_ssa/mir/block.rs | 16 +++++--- src/librustc_target/abi/mod.rs | 38 ++++++++++++------- src/librustc_typeck/check/intrinsic.rs | 6 ++- .../intrinsics/panic-uninitialized-zeroed.rs | 19 +++++++--- 6 files changed, 61 insertions(+), 29 deletions(-) diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index 0c187ee98fac3..c3ddc4316b51e 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -699,7 +699,12 @@ extern "rust-intrinsic" { /// A guard for unsafe functions that cannot ever be executed if `T` does not permit /// zero-initialization: This will statically either panic, or do nothing. #[cfg(not(bootstrap))] - pub fn panic_if_non_zero(); + pub fn panic_if_zero_invalid(); + + /// A guard for unsafe functions that cannot ever be executed if `T` has invalid + /// bit patterns: This will statically either panic, or do nothing. + #[cfg(not(bootstrap))] + pub fn panic_if_any_invalid(); /// Gets a reference to a static `Location` indicating where it was called. pub fn caller_location() -> &'static crate::panic::Location<'static>; diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs index 41dfc47d9c462..6ed5f15c69a8e 100644 --- a/src/libcore/mem/mod.rs +++ b/src/libcore/mem/mod.rs @@ -496,7 +496,7 @@ pub const fn needs_drop() -> bool { #[rustc_diagnostic_item = "mem_zeroed"] pub unsafe fn zeroed() -> T { #[cfg(not(bootstrap))] - intrinsics::panic_if_non_zero::(); + intrinsics::panic_if_zero_invalid::(); #[cfg(bootstrap)] intrinsics::panic_if_uninhabited::(); intrinsics::init() @@ -532,7 +532,7 @@ pub unsafe fn zeroed() -> T { #[rustc_diagnostic_item = "mem_uninitialized"] pub unsafe fn uninitialized() -> T { #[cfg(not(bootstrap))] - intrinsics::panic_if_non_zero::(); + intrinsics::panic_if_any_invalid::(); #[cfg(bootstrap)] intrinsics::panic_if_uninhabited::(); intrinsics::uninit() diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs index 9febf51992a76..94fcc0037dd8c 100644 --- a/src/librustc_codegen_ssa/mir/block.rs +++ b/src/librustc_codegen_ssa/mir/block.rs @@ -544,10 +544,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { // These are intrinsics that compile to panics so that we can get a message // which mentions the offending type, even from a const context. #[derive(Debug, PartialEq)] - enum PanicIntrinsic { IfUninhabited, IfNonZero }; + enum PanicIntrinsic { IfUninhabited, IfZeroInvalid, IfAnyInvalid }; let panic_intrinsic = intrinsic.and_then(|i| match i { "panic_if_uninhabited" => Some(PanicIntrinsic::IfUninhabited), - "panic_if_non_zero" => Some(PanicIntrinsic::IfNonZero), + "panic_if_zero_invalid" => Some(PanicIntrinsic::IfZeroInvalid), + "panic_if_any_invalid" => Some(PanicIntrinsic::IfAnyInvalid), _ => None }); if let Some(intrinsic) = panic_intrinsic { @@ -556,14 +557,19 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let layout = bx.layout_of(ty); let do_panic = match intrinsic { IfUninhabited => layout.abi.is_uninhabited(), - IfNonZero => !layout.might_permit_zero_init(&bx).unwrap(), // error type is `!` + IfZeroInvalid => // We unwrap as the error type is `!`. + !layout.might_permit_raw_init(&bx, /*zero:*/ true).unwrap(), + IfAnyInvalid => // We unwrap as the error type is `!`. + !layout.might_permit_raw_init(&bx, /*zero:*/ false).unwrap(), }; if do_panic { let msg_str = if layout.abi.is_uninhabited() { - // Use this error even for IfNonZero as it is more precise. + // Use this error even for the other intrinsics as it is more precise. format!("attempted to instantiate uninhabited type `{}`", ty) + } else if intrinsic == IfZeroInvalid { + format!("attempted to zero-initialize type `{}`, which is invalid", ty) } else { - format!("attempted to zero-initialize non-zero type `{}`", ty) + format!("attempted to leave type `{}` uninitialized, which is invalid", ty) }; let msg = bx.const_str(Symbol::intern(&msg_str)); let location = self.get_caller_location(&mut bx, span).immediate(); diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 09f642c24c8ae..f66c6a1d2f152 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -1087,34 +1087,45 @@ impl<'a, Ty> TyLayout<'a, Ty> { } } - /// Determines if zero-initializing this type might be okay. + /// Determines if this type permits "raw" initialization by just transmuting some + /// memory into an instance of `T`. + /// `zero` indicates if the memory is zero-initialized, or alternatively + /// left entirely uninitialized. /// This is conservative: in doubt, it will answer `true`. - pub fn might_permit_zero_init( + pub fn might_permit_raw_init( &self, cx: &C, + zero: bool, ) -> Result where Self: Copy, Ty: TyLayoutMethods<'a, C>, C: LayoutOf> { - fn scalar_allows_zero(s: &Scalar) -> bool { - s.valid_range.contains(&0) || - (*s.valid_range.start() > *s.valid_range.end()) // wrap-around allows 0 - } + let scalar_allows_raw_init = move |s: &Scalar| -> bool { + let range = &s.valid_range; + if zero { + // The range must contain 0. + range.contains(&0) || + (*range.start() > *range.end()) // wrap-around allows 0 + } else { + // The range must include all values. + *range.start() == range.end().wrapping_add(1) + } + }; // Abi is the most informative here. let res = match &self.abi { Abi::Uninhabited => false, // definitely UB - Abi::Scalar(s) => scalar_allows_zero(s), + Abi::Scalar(s) => scalar_allows_raw_init(s), Abi::ScalarPair(s1, s2) => - scalar_allows_zero(s1) && scalar_allows_zero(s2), + scalar_allows_raw_init(s1) && scalar_allows_raw_init(s2), Abi::Vector { element: s, count } => - *count == 0 || scalar_allows_zero(s), + *count == 0 || scalar_allows_raw_init(s), Abi::Aggregate { .. } => { // For aggregates, recurse. let inner = match self.variants { - Variants::Multiple { .. } => // FIXME: get variant with "0" discriminant. + Variants::Multiple { .. } => // FIXME: could we be more precise here? return Ok(true), Variants::Single { index } => self.for_variant(&cx, index), }; @@ -1124,12 +1135,13 @@ impl<'a, Ty> TyLayout<'a, Ty> { FieldPlacement::Array { .. } => // FIXME: The widely use smallvec 0.6 creates uninit arrays // with any element type, so let us not (yet) complain about that. - // count == 0 || inner.field(cx, 0).to_result()?.might_permit_zero_init(cx)? + // count == 0 || + // inner.field(cx, 0).to_result()?.might_permit_raw_init(cx, zero)? true, FieldPlacement::Arbitrary { ref offsets, .. } => { // Check that all fields accept zero-init. for idx in 0..offsets.len() { - if !inner.field(cx, idx).to_result()?.might_permit_zero_init(cx)? { + if !inner.field(cx, idx).to_result()?.might_permit_raw_init(cx, zero)? { return Ok(false); } } @@ -1139,7 +1151,7 @@ impl<'a, Ty> TyLayout<'a, Ty> { } } }; - trace!("might_permit_zero_init({:?}) = {}", self.details, res); + trace!("might_permit_raw_init({:?}, zero={}) = {}", self.details, zero, res); Ok(res) } } diff --git a/src/librustc_typeck/check/intrinsic.rs b/src/librustc_typeck/check/intrinsic.rs index cff7441bdbb01..748f0c567ddfa 100644 --- a/src/librustc_typeck/check/intrinsic.rs +++ b/src/librustc_typeck/check/intrinsic.rs @@ -148,8 +148,10 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) { } "rustc_peek" => (1, vec![param(0)], param(0)), "caller_location" => (0, vec![], tcx.caller_location_ty()), - "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()), - "panic_if_non_zero" => (1, Vec::new(), tcx.mk_unit()), + "panic_if_uninhabited" | + "panic_if_zero_invalid" | + "panic_if_any_invalid" => + (1, Vec::new(), tcx.mk_unit()), "init" => (1, Vec::new(), param(0)), "uninit" => (1, Vec::new(), param(0)), "forget" => (1, vec![param(0)], tcx.mk_unit()), diff --git a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs index 75c2fb87000e1..937f949a7b00b 100644 --- a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs +++ b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs @@ -73,29 +73,36 @@ fn main() { // Types that do not like zero-initialziation test_panic_msg( || mem::uninitialized::(), - "attempted to zero-initialize non-zero type `fn()`" + "attempted to leave type `fn()` uninitialized, which is invalid" ); test_panic_msg( || mem::zeroed::(), - "attempted to zero-initialize non-zero type `fn()`" + "attempted to zero-initialize type `fn()`, which is invalid" ); test_panic_msg( || mem::uninitialized::<*const dyn Send>(), - "attempted to zero-initialize non-zero type `*const dyn std::marker::Send`" + "attempted to leave type `*const dyn std::marker::Send` uninitialized, which is invalid" ); test_panic_msg( || mem::zeroed::<*const dyn Send>(), - "attempted to zero-initialize non-zero type `*const dyn std::marker::Send`" + "attempted to zero-initialize type `*const dyn std::marker::Send`, which is invalid" ); test_panic_msg( || mem::uninitialized::<(NonNull, u32, u32)>(), - "attempted to zero-initialize non-zero type `(std::ptr::NonNull, u32, u32)`" + "attempted to leave type `(std::ptr::NonNull, u32, u32)` uninitialized, \ + which is invalid" ); test_panic_msg( || mem::zeroed::<(NonNull, u32, u32)>(), - "attempted to zero-initialize non-zero type `(std::ptr::NonNull, u32, u32)`" + "attempted to zero-initialize type `(std::ptr::NonNull, u32, u32)`, \ + which is invalid" + ); + + test_panic_msg( + || mem::uninitialized::(), + "attempted to leave type `bool` uninitialized, which is invalid" ); // Some things that should work. From 99514454b29f1141e594de67cf5cd67a8a2ef7af Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 5 Nov 2019 13:49:12 +0100 Subject: [PATCH 08/15] refactor --- src/librustc_target/abi/mod.rs | 55 ++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index f66c6a1d2f152..19a42c17428f8 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -1093,7 +1093,7 @@ impl<'a, Ty> TyLayout<'a, Ty> { /// left entirely uninitialized. /// This is conservative: in doubt, it will answer `true`. pub fn might_permit_raw_init( - &self, + self, cx: &C, zero: bool, ) -> Result @@ -1123,30 +1123,39 @@ impl<'a, Ty> TyLayout<'a, Ty> { Abi::Vector { element: s, count } => *count == 0 || scalar_allows_raw_init(s), Abi::Aggregate { .. } => { - // For aggregates, recurse. - let inner = match self.variants { - Variants::Multiple { .. } => // FIXME: could we be more precise here? - return Ok(true), - Variants::Single { index } => self.for_variant(&cx, index), - }; - - match inner.fields { - FieldPlacement::Union(..) => true, // An all-0 unit is fine. - FieldPlacement::Array { .. } => - // FIXME: The widely use smallvec 0.6 creates uninit arrays - // with any element type, so let us not (yet) complain about that. - // count == 0 || - // inner.field(cx, 0).to_result()?.might_permit_raw_init(cx, zero)? - true, - FieldPlacement::Arbitrary { ref offsets, .. } => { - // Check that all fields accept zero-init. - for idx in 0..offsets.len() { - if !inner.field(cx, idx).to_result()?.might_permit_raw_init(cx, zero)? { - return Ok(false); + match self.variants { + Variants::Multiple { .. } => + if zero { + // FIXME: could we identify the variant with discriminant 0, check that? + true + } else { + // FIXME: This needs to have some sort of discriminant, + // which cannot be undef. But for now we are conservative. + true + }, + Variants::Single { .. } => { + // For aggregates, recurse. + match self.fields { + FieldPlacement::Union(..) => true, // An all-0 unit is fine. + FieldPlacement::Array { .. } => + // FIXME: The widely use smallvec 0.6 creates uninit arrays + // with any element type, so let us not (yet) complain about that. + // count == 0 || + // self.field(cx, 0).to_result()?.might_permit_raw_init(cx, zero)? + true, + FieldPlacement::Arbitrary { ref offsets, .. } => { + let mut res = true; + // Check that all fields accept zero-init. + for idx in 0..offsets.len() { + let field = self.field(cx, idx).to_result()?; + if !field.might_permit_raw_init(cx, zero)? { + res = false; + break; + } + } + res } } - // No bad field found, we are good. - true } } } From 7582bf8d73ba7f6ba9427f65ab986cf6bf5eb91d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 6 Nov 2019 11:07:00 +0100 Subject: [PATCH 09/15] reference tracking issue --- src/librustc_target/abi/mod.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 19a42c17428f8..5438120f26107 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -1126,10 +1126,11 @@ impl<'a, Ty> TyLayout<'a, Ty> { match self.variants { Variants::Multiple { .. } => if zero { - // FIXME: could we identify the variant with discriminant 0, check that? + // FIXME(#66151): + // could we identify the variant with discriminant 0, check that? true } else { - // FIXME: This needs to have some sort of discriminant, + // FIXME(#66151): This needs to have some sort of discriminant, // which cannot be undef. But for now we are conservative. true }, @@ -1138,7 +1139,7 @@ impl<'a, Ty> TyLayout<'a, Ty> { match self.fields { FieldPlacement::Union(..) => true, // An all-0 unit is fine. FieldPlacement::Array { .. } => - // FIXME: The widely use smallvec 0.6 creates uninit arrays + // FIXME(#66151): The widely use smallvec 0.6 creates uninit arrays // with any element type, so let us not (yet) complain about that. // count == 0 || // self.field(cx, 0).to_result()?.might_permit_raw_init(cx, zero)? From 129c991022575c6b24a2337e5e44ed7c09e98803 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 9 Nov 2019 12:19:44 +0100 Subject: [PATCH 10/15] use valid_range_exclusive for correct overflow handling --- src/librustc_target/abi/mod.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 5438120f26107..71f53f8abe246 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -1100,17 +1100,20 @@ impl<'a, Ty> TyLayout<'a, Ty> { where Self: Copy, Ty: TyLayoutMethods<'a, C>, - C: LayoutOf> + C: LayoutOf> + HasDataLayout { let scalar_allows_raw_init = move |s: &Scalar| -> bool { - let range = &s.valid_range; if zero { + let range = &s.valid_range; // The range must contain 0. range.contains(&0) || (*range.start() > *range.end()) // wrap-around allows 0 } else { - // The range must include all values. - *range.start() == range.end().wrapping_add(1) + // The range must include all values. `valid_range_exclusive` handles + // the wrap-around using target arithmetic; with wrap-around then the full + // range is one where `start == end`. + let range = s.valid_range_exclusive(cx); + range.start == range.end } }; From 0bdb6b86de66c39d2bcf7daf7125544c81b1f1b0 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 9 Nov 2019 12:24:26 +0100 Subject: [PATCH 11/15] test some more things that should not panic --- .../intrinsics/panic-uninitialized-zeroed.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs index 937f949a7b00b..9c869947bfa10 100644 --- a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs +++ b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs @@ -6,8 +6,11 @@ #![feature(never_type)] #![allow(deprecated, invalid_value)] -use std::{mem, panic}; -use std::ptr::NonNull; +use std::{ + mem::{self, MaybeUninit}, + panic, + ptr::NonNull, +}; #[allow(dead_code)] struct Foo { @@ -40,7 +43,7 @@ fn main() { "attempted to instantiate uninhabited type `!`" ); test_panic_msg( - || mem::MaybeUninit::::uninit().assume_init(), + || MaybeUninit::::uninit().assume_init(), "attempted to instantiate uninhabited type `!`" ); @@ -53,7 +56,7 @@ fn main() { "attempted to instantiate uninhabited type `Foo`" ); test_panic_msg( - || mem::MaybeUninit::::uninit().assume_init(), + || MaybeUninit::::uninit().assume_init(), "attempted to instantiate uninhabited type `Foo`" ); @@ -66,7 +69,7 @@ fn main() { "attempted to instantiate uninhabited type `Bar`" ); test_panic_msg( - || mem::MaybeUninit::::uninit().assume_init(), + || MaybeUninit::::uninit().assume_init(), "attempted to instantiate uninhabited type `Bar`" ); @@ -109,5 +112,11 @@ fn main() { let _val = mem::zeroed::(); let _val = mem::zeroed::(); let _val = mem::zeroed::>(); + let _val = mem::zeroed::>>(); + let _val = mem::uninitialized::>(); + + // We don't panic for these just to be conservative. They are UB as of now (2019-11-09). + let _val = mem::uninitialized::(); + let _val = mem::uninitialized::<*const ()>(); } } From 076f27a8700ee4d8acba6642e5a8cec9518ba3f0 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 13 Nov 2019 09:00:29 +0100 Subject: [PATCH 12/15] note a FIXME --- src/librustc_codegen_ssa/mir/block.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs index 94fcc0037dd8c..175f45d8385ad 100644 --- a/src/librustc_codegen_ssa/mir/block.rs +++ b/src/librustc_codegen_ssa/mir/block.rs @@ -546,6 +546,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { #[derive(Debug, PartialEq)] enum PanicIntrinsic { IfUninhabited, IfZeroInvalid, IfAnyInvalid }; let panic_intrinsic = intrinsic.and_then(|i| match i { + // FIXME: Move to symbols instead of strings. "panic_if_uninhabited" => Some(PanicIntrinsic::IfUninhabited), "panic_if_zero_invalid" => Some(PanicIntrinsic::IfZeroInvalid), "panic_if_any_invalid" => Some(PanicIntrinsic::IfAnyInvalid), From 5c5a10e69bb3d16a2329a434ca42121ed60035f2 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 14 Nov 2019 15:39:27 +0100 Subject: [PATCH 13/15] yet another FIXME --- src/librustc_codegen_ssa/mir/block.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs index 175f45d8385ad..7b0f168870584 100644 --- a/src/librustc_codegen_ssa/mir/block.rs +++ b/src/librustc_codegen_ssa/mir/block.rs @@ -576,6 +576,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let location = self.get_caller_location(&mut bx, span).immediate(); // Obtain the panic entry point. + // FIXME: dedup this with `codegen_assert_terminator` above. let def_id = common::langcall(bx.tcx(), Some(span), "", lang_items::PanicFnLangItem); let instance = ty::Instance::mono(bx.tcx(), def_id); From e52d94e23212cda03769cadc864999f7cbe1cbe7 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 15 Nov 2019 21:55:47 +0100 Subject: [PATCH 14/15] make it even more conservative --- src/librustc_target/abi/mod.rs | 14 +++++++++----- .../ui/intrinsics/panic-uninitialized-zeroed.rs | 2 ++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 71f53f8abe246..7a0e41dd6cc6f 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -1144,11 +1144,14 @@ impl<'a, Ty> TyLayout<'a, Ty> { FieldPlacement::Array { .. } => // FIXME(#66151): The widely use smallvec 0.6 creates uninit arrays // with any element type, so let us not (yet) complain about that. - // count == 0 || - // self.field(cx, 0).to_result()?.might_permit_raw_init(cx, zero)? + /* count == 0 || + self.field(cx, 0).to_result()?.might_permit_raw_init(cx, zero)? */ true, - FieldPlacement::Arbitrary { ref offsets, .. } => { - let mut res = true; + FieldPlacement::Arbitrary { .. } => { + // FIXME(#66151) cargo depends on sized-chunks 0.3.0 which + // has some illegal zero-initialization, so let us not (yet) + // complain about aggregates either. + /* let mut res = true; // Check that all fields accept zero-init. for idx in 0..offsets.len() { let field = self.field(cx, idx).to_result()?; @@ -1157,7 +1160,8 @@ impl<'a, Ty> TyLayout<'a, Ty> { break; } } - res + res */ + true } } } diff --git a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs index 9c869947bfa10..a1b2a1af2c434 100644 --- a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs +++ b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs @@ -92,6 +92,7 @@ fn main() { "attempted to zero-initialize type `*const dyn std::marker::Send`, which is invalid" ); + /* FIXME(#66151) we conservatively do not error here yet. test_panic_msg( || mem::uninitialized::<(NonNull, u32, u32)>(), "attempted to leave type `(std::ptr::NonNull, u32, u32)` uninitialized, \ @@ -102,6 +103,7 @@ fn main() { "attempted to zero-initialize type `(std::ptr::NonNull, u32, u32)`, \ which is invalid" ); + */ test_panic_msg( || mem::uninitialized::(), From f57cfe4af2c40173eb57df63a6d74f6171e24031 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 22 Dec 2019 14:01:57 +0100 Subject: [PATCH 15/15] note a possible future implementation strategy --- src/librustc_target/abi/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 7a0e41dd6cc6f..249909b29092c 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -1092,6 +1092,10 @@ impl<'a, Ty> TyLayout<'a, Ty> { /// `zero` indicates if the memory is zero-initialized, or alternatively /// left entirely uninitialized. /// This is conservative: in doubt, it will answer `true`. + /// + /// FIXME: Once we removed all the conservatism, we could alternatively + /// create an all-0/all-undef constant and run the vonst value validator to see if + /// this is a valid value for the given type. pub fn might_permit_raw_init( self, cx: &C,