From a5082338ace7869fe48b1896ecb4a9cce124b936 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Tue, 20 Nov 2018 12:41:31 -0500 Subject: [PATCH] Allow raw pointers to be undef in Miri mode The doctest at https://doc.rust-lang.org/std/mem/fn.uninitialized.html#examples creates an uninitialized Vec - which (transitively) contains an uninitialized raw pointer. Since this code is sound, Miri should allow raw pointers to be undefined, to allow running this kind of code in Miri. I've left this as error in const_mode in order to avoid chaning the behavior of const functions. --- src/librustc_mir/interpret/validity.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index ad7ffd291bed1..f2e4b0c74969d 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -312,20 +312,25 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> } } ty::RawPtr(..) => { - // No undef allowed here. Eventually this should be consistent with - // the integer types. - let _ptr = try_validation!(value.to_scalar_ptr(), - "undefined address in pointer", self.path); - let _meta = try_validation!(value.to_meta(), - "uninitialized data in fat pointer metadata", self.path); + if self.const_mode { + // No undef allowed here. Eventually this should be consistent with + // the integer types. + let _ptr = try_validation!(value.to_scalar_ptr(), + "undefined address in raw pointer", self.path); + let _meta = try_validation!(value.to_meta(), + "uninitialized data in raw fat pointer metadata", self.path); + } else { + // To avoid breaking existing code, we need to accept + // undef here. + } } _ if ty.is_box() || ty.is_region_ptr() => { // Handle fat pointers. // Check metadata early, for better diagnostics let ptr = try_validation!(value.to_scalar_ptr(), - "undefined address in pointer", self.path); + "undefined address in box pointer", self.path); let meta = try_validation!(value.to_meta(), - "uninitialized data in fat pointer metadata", self.path); + "uninitialized data in box fat pointer metadata", self.path); let layout = self.ecx.layout_of(value.layout.ty.builtin_deref(true).unwrap().ty)?; if layout.is_unsized() { let tail = self.ecx.tcx.struct_tail(layout.ty);