From 524e575bb40896cfa839baa03b08ae1fd70aded6 Mon Sep 17 00:00:00 2001 From: Smitty Date: Sat, 12 Jun 2021 19:49:48 -0400 Subject: [PATCH 01/18] Support allocation failures when interperting MIR Note that this breaks Miri. Closes #79601 --- .../rustc_codegen_cranelift/src/vtable.rs | 5 ++++- compiler/rustc_codegen_ssa/src/meth.rs | 5 ++++- compiler/rustc_middle/src/lib.rs | 1 + .../src/mir/interpret/allocation.rs | 21 +++++++++++++------ .../rustc_middle/src/mir/interpret/error.rs | 5 +++++ compiler/rustc_middle/src/ty/vtable.rs | 10 ++++----- .../rustc_mir/src/const_eval/eval_queries.rs | 2 +- compiler/rustc_mir/src/const_eval/machine.rs | 2 +- compiler/rustc_mir/src/const_eval/mod.rs | 6 +++++- compiler/rustc_mir/src/interpret/intern.rs | 2 +- .../rustc_mir/src/interpret/intrinsics.rs | 2 +- .../interpret/intrinsics/caller_location.rs | 13 +++++++----- compiler/rustc_mir/src/interpret/memory.rs | 8 +++---- compiler/rustc_mir/src/interpret/place.rs | 8 +++---- compiler/rustc_mir/src/interpret/traits.rs | 2 +- compiler/rustc_mir/src/lib.rs | 1 + .../rustc_mir/src/transform/const_prop.rs | 18 +++++++++------- src/test/ui/consts/large_const_alloc.rs | 13 ++++++++++++ src/test/ui/consts/large_const_alloc.stderr | 18 ++++++++++++++++ 19 files changed, 103 insertions(+), 39 deletions(-) create mode 100644 src/test/ui/consts/large_const_alloc.rs create mode 100644 src/test/ui/consts/large_const_alloc.stderr diff --git a/compiler/rustc_codegen_cranelift/src/vtable.rs b/compiler/rustc_codegen_cranelift/src/vtable.rs index 12f7092d935a3..5788aabaadcd2 100644 --- a/compiler/rustc_codegen_cranelift/src/vtable.rs +++ b/compiler/rustc_codegen_cranelift/src/vtable.rs @@ -72,7 +72,10 @@ pub(crate) fn get_vtable<'tcx>( let vtable_ptr = if let Some(vtable_ptr) = fx.vtables.get(&(ty, trait_ref)) { *vtable_ptr } else { - let vtable_alloc_id = fx.tcx.vtable_allocation(ty, trait_ref); + let vtable_alloc_id = match fx.tcx.vtable_allocation(ty, trait_ref) { + Ok(alloc) => alloc, + Err(_) => fx.tcx.sess().fatal("allocation of constant vtable failed"), + }; let vtable_allocation = fx.tcx.global_alloc(vtable_alloc_id).unwrap_memory(); let vtable_ptr = pointer_for_allocation(fx, vtable_allocation); diff --git a/compiler/rustc_codegen_ssa/src/meth.rs b/compiler/rustc_codegen_ssa/src/meth.rs index 63245a94c8e3d..fcaafb94cfd5b 100644 --- a/compiler/rustc_codegen_ssa/src/meth.rs +++ b/compiler/rustc_codegen_ssa/src/meth.rs @@ -70,7 +70,10 @@ pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>( return val; } - let vtable_alloc_id = tcx.vtable_allocation(ty, trait_ref); + let vtable_alloc_id = match tcx.vtable_allocation(ty, trait_ref) { + Ok(alloc) => alloc, + Err(_) => tcx.sess.fatal("allocation of constant vtable failed"), + }; let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory(); let vtable_const = cx.const_data_from_alloc(vtable_allocation); let align = cx.data_layout().pointer_align.abi; diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index 649913dd025b5..57f507290e894 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -48,6 +48,7 @@ #![feature(associated_type_defaults)] #![feature(iter_zip)] #![feature(thread_local_const_init)] +#![feature(try_reserve)] #![recursion_limit = "512"] #[macro_use] diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index ee3902991e911..7405a70d39ad0 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -11,8 +11,9 @@ use rustc_data_structures::sorted_map::SortedMap; use rustc_target::abi::{Align, HasDataLayout, Size}; use super::{ - read_target_uint, write_target_uint, AllocId, InterpError, Pointer, Scalar, ScalarMaybeUninit, - UndefinedBehaviorInfo, UninitBytesAccess, UnsupportedOpInfo, + read_target_uint, write_target_uint, AllocId, InterpError, InterpResult, Pointer, + ResourceExhaustionInfo, Scalar, ScalarMaybeUninit, UndefinedBehaviorInfo, UninitBytesAccess, + UnsupportedOpInfo, }; /// This type represents an Allocation in the Miri/CTFE core engine. @@ -121,15 +122,23 @@ impl Allocation { Allocation::from_bytes(slice, Align::ONE, Mutability::Not) } - pub fn uninit(size: Size, align: Align) -> Self { - Allocation { - bytes: vec![0; size.bytes_usize()], + /// Try to create an Allocation of `size` bytes, failing if there is not enough memory + /// available to the compiler to do so. + pub fn uninit(size: Size, align: Align) -> InterpResult<'static, Self> { + let mut bytes = Vec::new(); + bytes.try_reserve(size.bytes_usize()).map_err(|_| { + InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) + })?; + bytes.resize(size.bytes_usize(), 0); + bytes.fill(0); + Ok(Allocation { + bytes: bytes, relocations: Relocations::new(), init_mask: InitMask::new(size, false), align, mutability: Mutability::Mut, extra: (), - } + }) } } diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index cb2a355697dd0..137a0bb77e3b5 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -423,6 +423,8 @@ pub enum ResourceExhaustionInfo { /// /// The exact limit is set by the `const_eval_limit` attribute. StepLimitReached, + /// There is not enough memory to perform an allocation. + MemoryExhausted, } impl fmt::Display for ResourceExhaustionInfo { @@ -435,6 +437,9 @@ impl fmt::Display for ResourceExhaustionInfo { StepLimitReached => { write!(f, "exceeded interpreter step limit (see `#[const_eval_limit]`)") } + MemoryExhausted => { + write!(f, "tried to allocate more memory than available to compiler") + } } } } diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index 3a35d8c88a478..61c146d03a328 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -1,6 +1,6 @@ use std::convert::TryFrom; -use crate::mir::interpret::{alloc_range, AllocId, Allocation, Pointer, Scalar}; +use crate::mir::interpret::{alloc_range, AllocId, Allocation, Pointer, Scalar, InterpResult}; use crate::ty::fold::TypeFoldable; use crate::ty::{self, DefId, SubstsRef, Ty, TyCtxt}; use rustc_ast::Mutability; @@ -28,11 +28,11 @@ impl<'tcx> TyCtxt<'tcx> { self, ty: Ty<'tcx>, poly_trait_ref: Option>, - ) -> AllocId { + ) -> InterpResult<'tcx, AllocId> { let tcx = self; let vtables_cache = tcx.vtables_cache.lock(); if let Some(alloc_id) = vtables_cache.get(&(ty, poly_trait_ref)).cloned() { - return alloc_id; + return Ok(alloc_id); } drop(vtables_cache); @@ -60,7 +60,7 @@ impl<'tcx> TyCtxt<'tcx> { let ptr_align = tcx.data_layout.pointer_align.abi; let vtable_size = ptr_size * u64::try_from(vtable_entries.len()).unwrap(); - let mut vtable = Allocation::uninit(vtable_size, ptr_align); + let mut vtable = Allocation::uninit(vtable_size, ptr_align)?; // No need to do any alignment checks on the memory accesses below, because we know the // allocation is correctly aligned as we created it above. Also we're only offsetting by @@ -101,6 +101,6 @@ impl<'tcx> TyCtxt<'tcx> { let alloc_id = tcx.create_memory_alloc(tcx.intern_const_alloc(vtable)); let mut vtables_cache = self.vtables_cache.lock(); vtables_cache.insert((ty, poly_trait_ref), alloc_id); - alloc_id + Ok(alloc_id) } } diff --git a/compiler/rustc_mir/src/const_eval/eval_queries.rs b/compiler/rustc_mir/src/const_eval/eval_queries.rs index 536dbad4f764d..7a7dbe50e72f9 100644 --- a/compiler/rustc_mir/src/const_eval/eval_queries.rs +++ b/compiler/rustc_mir/src/const_eval/eval_queries.rs @@ -48,7 +48,7 @@ fn eval_body_using_ecx<'mir, 'tcx>( ); let layout = ecx.layout_of(body.return_ty().subst(tcx, cid.instance.substs))?; assert!(!layout.is_unsized()); - let ret = ecx.allocate(layout, MemoryKind::Stack); + let ret = ecx.allocate(layout, MemoryKind::Stack)?; let name = with_no_trimmed_paths(|| ty::tls::with(|tcx| tcx.def_path_str(cid.instance.def_id()))); diff --git a/compiler/rustc_mir/src/const_eval/machine.rs b/compiler/rustc_mir/src/const_eval/machine.rs index 773df7d7b60c1..ddc87084e9fd1 100644 --- a/compiler/rustc_mir/src/const_eval/machine.rs +++ b/compiler/rustc_mir/src/const_eval/machine.rs @@ -306,7 +306,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, Size::from_bytes(size as u64), align, interpret::MemoryKind::Machine(MemoryKind::Heap), - ); + )?; ecx.write_scalar(Scalar::Ptr(ptr), dest)?; } _ => { diff --git a/compiler/rustc_mir/src/const_eval/mod.rs b/compiler/rustc_mir/src/const_eval/mod.rs index 6a514e9f62fce..8e379f9eb2834 100644 --- a/compiler/rustc_mir/src/const_eval/mod.rs +++ b/compiler/rustc_mir/src/const_eval/mod.rs @@ -31,7 +31,11 @@ pub(crate) fn const_caller_location( trace!("const_caller_location: {}:{}:{}", file, line, col); let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false); - let loc_place = ecx.alloc_caller_location(file, line, col); + // This can fail if rustc runs out of memory right here. Trying to emit an error would be + // pointless, since that would require allocating more memory than a Location. + let loc_place = ecx + .alloc_caller_location(file, line, col) + .expect("not enough memory to allocate location?"); if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() { bug!("intern_const_alloc_recursive should not error in this case") } diff --git a/compiler/rustc_mir/src/interpret/intern.rs b/compiler/rustc_mir/src/interpret/intern.rs index d5fec457fa19e..2862670dc7c46 100644 --- a/compiler/rustc_mir/src/interpret/intern.rs +++ b/compiler/rustc_mir/src/interpret/intern.rs @@ -428,7 +428,7 @@ impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>> &MPlaceTy<'tcx, M::PointerTag>, ) -> InterpResult<'tcx, ()>, ) -> InterpResult<'tcx, &'tcx Allocation> { - let dest = self.allocate(layout, MemoryKind::Stack); + let dest = self.allocate(layout, MemoryKind::Stack)?; f(self, &dest)?; let ptr = dest.ptr.assert_ptr(); assert_eq!(ptr.offset, Size::ZERO); diff --git a/compiler/rustc_mir/src/interpret/intrinsics.rs b/compiler/rustc_mir/src/interpret/intrinsics.rs index 4e4166dad50e2..92484054e86a3 100644 --- a/compiler/rustc_mir/src/interpret/intrinsics.rs +++ b/compiler/rustc_mir/src/interpret/intrinsics.rs @@ -137,7 +137,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { match intrinsic_name { sym::caller_location => { let span = self.find_closest_untracked_caller_location(); - let location = self.alloc_caller_location_for_span(span); + let location = self.alloc_caller_location_for_span(span)?; self.write_scalar(location.ptr, dest)?; } diff --git a/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs b/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs index 792a4749108be..d3e3a565adafb 100644 --- a/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs +++ b/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs @@ -9,7 +9,7 @@ use rustc_target::abi::LayoutOf; use crate::interpret::{ intrinsics::{InterpCx, Machine}, - MPlaceTy, MemoryKind, Scalar, + InterpResult, MPlaceTy, MemoryKind, Scalar, }; impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { @@ -79,7 +79,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { filename: Symbol, line: u32, col: u32, - ) -> MPlaceTy<'tcx, M::PointerTag> { + ) -> InterpResult<'static, MPlaceTy<'tcx, M::PointerTag>> { let file = self.allocate_str(&filename.as_str(), MemoryKind::CallerLocation, Mutability::Not); let line = Scalar::from_u32(line); @@ -91,7 +91,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { .type_of(self.tcx.require_lang_item(LangItem::PanicLocation, None)) .subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_erased.into()].iter())); let loc_layout = self.layout_of(loc_ty).unwrap(); - let location = self.allocate(loc_layout, MemoryKind::CallerLocation); + let location = self.allocate(loc_layout, MemoryKind::CallerLocation)?; // Initialize fields. self.write_immediate(file.to_ref(), &self.mplace_field(&location, 0).unwrap().into()) @@ -101,7 +101,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { self.write_scalar(col, &self.mplace_field(&location, 2).unwrap().into()) .expect("writing to memory we just allocated cannot fail"); - location + Ok(location) } crate fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) { @@ -114,7 +114,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ) } - pub fn alloc_caller_location_for_span(&mut self, span: Span) -> MPlaceTy<'tcx, M::PointerTag> { + pub fn alloc_caller_location_for_span( + &mut self, + span: Span, + ) -> InterpResult<'static, MPlaceTy<'tcx, M::PointerTag>> { let (file, line, column) = self.location_triple_for_span(span); self.alloc_caller_location(file, line, column) } diff --git a/compiler/rustc_mir/src/interpret/memory.rs b/compiler/rustc_mir/src/interpret/memory.rs index 94506808a68c3..671e3d278f366 100644 --- a/compiler/rustc_mir/src/interpret/memory.rs +++ b/compiler/rustc_mir/src/interpret/memory.rs @@ -207,9 +207,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { size: Size, align: Align, kind: MemoryKind, - ) -> Pointer { - let alloc = Allocation::uninit(size, align); - self.allocate_with(alloc, kind) + ) -> InterpResult<'static, Pointer> { + let alloc = Allocation::uninit(size, align)?; + Ok(self.allocate_with(alloc, kind)) } pub fn allocate_bytes( @@ -257,7 +257,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { // For simplicities' sake, we implement reallocate as "alloc, copy, dealloc". // This happens so rarely, the perf advantage is outweighed by the maintenance cost. - let new_ptr = self.allocate(new_size, new_align, kind); + let new_ptr = self.allocate(new_size, new_align, kind)?; let old_size = match old_size_and_align { Some((size, _align)) => size, None => self.get_raw(ptr.alloc_id)?.size(), diff --git a/compiler/rustc_mir/src/interpret/place.rs b/compiler/rustc_mir/src/interpret/place.rs index 4c53510ed00ee..42a304ce41213 100644 --- a/compiler/rustc_mir/src/interpret/place.rs +++ b/compiler/rustc_mir/src/interpret/place.rs @@ -982,7 +982,7 @@ where let (size, align) = self .size_and_align_of(&meta, &local_layout)? .expect("Cannot allocate for non-dyn-sized type"); - let ptr = self.memory.allocate(size, align, MemoryKind::Stack); + let ptr = self.memory.allocate(size, align, MemoryKind::Stack)?; let mplace = MemPlace { ptr: ptr.into(), align, meta }; if let LocalValue::Live(Operand::Immediate(value)) = local_val { // Preserve old value. @@ -1018,9 +1018,9 @@ where &mut self, layout: TyAndLayout<'tcx>, kind: MemoryKind, - ) -> MPlaceTy<'tcx, M::PointerTag> { - let ptr = self.memory.allocate(layout.size, layout.align.abi, kind); - MPlaceTy::from_aligned_ptr(ptr, layout) + ) -> InterpResult<'static, MPlaceTy<'tcx, M::PointerTag>> { + let ptr = self.memory.allocate(layout.size, layout.align.abi, kind)?; + Ok(MPlaceTy::from_aligned_ptr(ptr, layout)) } /// Returns a wide MPlace of type `&'static [mut] str` to a new 1-aligned allocation. diff --git a/compiler/rustc_mir/src/interpret/traits.rs b/compiler/rustc_mir/src/interpret/traits.rs index 5332e615bc8ea..94948948ccfac 100644 --- a/compiler/rustc_mir/src/interpret/traits.rs +++ b/compiler/rustc_mir/src/interpret/traits.rs @@ -30,7 +30,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ensure_monomorphic_enough(*self.tcx, ty)?; ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?; - let vtable_allocation = self.tcx.vtable_allocation(ty, poly_trait_ref); + let vtable_allocation = self.tcx.vtable_allocation(ty, poly_trait_ref)?; let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_allocation))?; diff --git a/compiler/rustc_mir/src/lib.rs b/compiler/rustc_mir/src/lib.rs index 1da17bddcb777..a58ded9cfd3a4 100644 --- a/compiler/rustc_mir/src/lib.rs +++ b/compiler/rustc_mir/src/lib.rs @@ -29,6 +29,7 @@ Rust MIR: a lowered representation of Rust. #![feature(option_get_or_insert_default)] #![feature(once_cell)] #![feature(control_flow_enum)] +#![feature(try_reserve)] #![recursion_limit = "256"] #[macro_use] diff --git a/compiler/rustc_mir/src/transform/const_prop.rs b/compiler/rustc_mir/src/transform/const_prop.rs index b56c247127ce4..48108b69a3702 100644 --- a/compiler/rustc_mir/src/transform/const_prop.rs +++ b/compiler/rustc_mir/src/transform/const_prop.rs @@ -385,15 +385,19 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { (), ); - let ret = ecx - .layout_of(body.return_ty().subst(tcx, substs)) - .ok() + let ret = if let Ok(layout) = ecx.layout_of(body.return_ty().subst(tcx, substs)) { // Don't bother allocating memory for ZST types which have no values // or for large values. - .filter(|ret_layout| { - !ret_layout.is_zst() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) - }) - .map(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack).into()); + if !layout.is_zst() && layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) { + // hopefully all types will allocate, since large types have already been removed, + // but check anyways + ecx.allocate(layout, MemoryKind::Stack).ok().map(Into::into) + } else { + None + } + } else { + None + }; ecx.push_stack_frame( Instance::new(def_id, substs), diff --git a/src/test/ui/consts/large_const_alloc.rs b/src/test/ui/consts/large_const_alloc.rs new file mode 100644 index 0000000000000..d5c90c0d6cf21 --- /dev/null +++ b/src/test/ui/consts/large_const_alloc.rs @@ -0,0 +1,13 @@ +// only-64bit +// on 32bit and 16bit platforms it is plausible that the maximum allocation size will succeed + +const FOO: () = { + // 128 TiB, unlikely anyone has that much RAM + let x = [0_u8; (1 << 47) - 1]; + //~^ ERROR any use of this value will cause an error + //~| WARNING this was previously accepted by the compiler but is being phased out +}; + +fn main() { + let _ = FOO; +} diff --git a/src/test/ui/consts/large_const_alloc.stderr b/src/test/ui/consts/large_const_alloc.stderr new file mode 100644 index 0000000000000..bf05cdb4a1d11 --- /dev/null +++ b/src/test/ui/consts/large_const_alloc.stderr @@ -0,0 +1,18 @@ +error: any use of this value will cause an error + --> $DIR/large_const_alloc.rs:6:13 + | +LL | / const FOO: () = { +LL | | // 128 TiB, unlikely anyone has that much RAM +LL | | let x = [0_u8; (1 << 47) - 1]; + | | ^^^^^^^^^^^^^^^^^^^^^ tried to allocate more memory than available to compiler +LL | | +LL | | +LL | | }; + | |__- + | + = note: `#[deny(const_err)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #71800 + +error: aborting due to previous error + From b40f3c10609a6f456f9a7c81fa9d49e22c10e3bc Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 15 Jun 2021 17:57:54 -0400 Subject: [PATCH 02/18] Simplify const_prop logic --- .../rustc_mir/src/transform/const_prop.rs | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_mir/src/transform/const_prop.rs b/compiler/rustc_mir/src/transform/const_prop.rs index 48108b69a3702..a660b145b3ff0 100644 --- a/compiler/rustc_mir/src/transform/const_prop.rs +++ b/compiler/rustc_mir/src/transform/const_prop.rs @@ -385,19 +385,17 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { (), ); - let ret = if let Ok(layout) = ecx.layout_of(body.return_ty().subst(tcx, substs)) { + let ret = ecx + .layout_of(body.return_ty().subst(tcx, substs)) + .ok() // Don't bother allocating memory for ZST types which have no values // or for large values. - if !layout.is_zst() && layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) { - // hopefully all types will allocate, since large types have already been removed, - // but check anyways - ecx.allocate(layout, MemoryKind::Stack).ok().map(Into::into) - } else { - None - } - } else { - None - }; + .filter(|ret_layout| { + !ret_layout.is_zst() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) + }) + // hopefully all types will allocate, since large types have already been removed + .and_then(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack).ok()) + .map(Into::into); ecx.push_stack_frame( Instance::new(def_id, substs), From dc1c6c3a253482574d796f5cec4250f323724620 Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 15 Jun 2021 18:24:39 -0400 Subject: [PATCH 03/18] Make memory exhaustion a hard error --- .../rustc_middle/src/mir/interpret/error.rs | 3 ++- src/test/ui/consts/large_const_alloc.rs | 3 +-- src/test/ui/consts/large_const_alloc.stderr | 17 ++++------------- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 137a0bb77e3b5..ab9239585c4aa 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -530,7 +530,8 @@ impl InterpError<'_> { use InterpError::*; match *self { MachineStop(ref err) => err.is_hard_err(), - InterpError::UndefinedBehavior(_) => true, + UndefinedBehavior(_) => true, + ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) => true, _ => false, } } diff --git a/src/test/ui/consts/large_const_alloc.rs b/src/test/ui/consts/large_const_alloc.rs index d5c90c0d6cf21..2771af92d30d1 100644 --- a/src/test/ui/consts/large_const_alloc.rs +++ b/src/test/ui/consts/large_const_alloc.rs @@ -4,8 +4,7 @@ const FOO: () = { // 128 TiB, unlikely anyone has that much RAM let x = [0_u8; (1 << 47) - 1]; - //~^ ERROR any use of this value will cause an error - //~| WARNING this was previously accepted by the compiler but is being phased out + //~^ ERROR evaluation of constant value failed }; fn main() { diff --git a/src/test/ui/consts/large_const_alloc.stderr b/src/test/ui/consts/large_const_alloc.stderr index bf05cdb4a1d11..54d56a42061d4 100644 --- a/src/test/ui/consts/large_const_alloc.stderr +++ b/src/test/ui/consts/large_const_alloc.stderr @@ -1,18 +1,9 @@ -error: any use of this value will cause an error +error[E0080]: evaluation of constant value failed --> $DIR/large_const_alloc.rs:6:13 | -LL | / const FOO: () = { -LL | | // 128 TiB, unlikely anyone has that much RAM -LL | | let x = [0_u8; (1 << 47) - 1]; - | | ^^^^^^^^^^^^^^^^^^^^^ tried to allocate more memory than available to compiler -LL | | -LL | | -LL | | }; - | |__- - | - = note: `#[deny(const_err)]` on by default - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #71800 +LL | let x = [0_u8; (1 << 47) - 1]; + | ^^^^^^^^^^^^^^^^^^^^^ tried to allocate more memory than available to compiler error: aborting due to previous error +For more information about this error, try `rustc --explain E0080`. From 43b55cf8935e4f96b143efff1a43bb30c3d55545 Mon Sep 17 00:00:00 2001 From: Smitty Date: Fri, 18 Jun 2021 15:17:13 -0400 Subject: [PATCH 04/18] Simplify allocation creation --- compiler/rustc_middle/src/mir/interpret/allocation.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index 7405a70d39ad0..36c87f335bd26 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -130,9 +130,8 @@ impl Allocation { InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) })?; bytes.resize(size.bytes_usize(), 0); - bytes.fill(0); Ok(Allocation { - bytes: bytes, + bytes, relocations: Relocations::new(), init_mask: InitMask::new(size, false), align, From 3e735a52febb098e1b541e89a6653adbcb134760 Mon Sep 17 00:00:00 2001 From: Smitty Date: Fri, 18 Jun 2021 17:33:33 -0400 Subject: [PATCH 05/18] Unwrap allocated Location at creation --- compiler/rustc_mir/src/const_eval/mod.rs | 6 +----- compiler/rustc_mir/src/interpret/intrinsics.rs | 2 +- .../src/interpret/intrinsics/caller_location.rs | 15 +++++++-------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_mir/src/const_eval/mod.rs b/compiler/rustc_mir/src/const_eval/mod.rs index 8e379f9eb2834..6a514e9f62fce 100644 --- a/compiler/rustc_mir/src/const_eval/mod.rs +++ b/compiler/rustc_mir/src/const_eval/mod.rs @@ -31,11 +31,7 @@ pub(crate) fn const_caller_location( trace!("const_caller_location: {}:{}:{}", file, line, col); let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false); - // This can fail if rustc runs out of memory right here. Trying to emit an error would be - // pointless, since that would require allocating more memory than a Location. - let loc_place = ecx - .alloc_caller_location(file, line, col) - .expect("not enough memory to allocate location?"); + let loc_place = ecx.alloc_caller_location(file, line, col); if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() { bug!("intern_const_alloc_recursive should not error in this case") } diff --git a/compiler/rustc_mir/src/interpret/intrinsics.rs b/compiler/rustc_mir/src/interpret/intrinsics.rs index 92484054e86a3..4e4166dad50e2 100644 --- a/compiler/rustc_mir/src/interpret/intrinsics.rs +++ b/compiler/rustc_mir/src/interpret/intrinsics.rs @@ -137,7 +137,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { match intrinsic_name { sym::caller_location => { let span = self.find_closest_untracked_caller_location(); - let location = self.alloc_caller_location_for_span(span)?; + let location = self.alloc_caller_location_for_span(span); self.write_scalar(location.ptr, dest)?; } diff --git a/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs b/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs index d3e3a565adafb..4a3278030b5d9 100644 --- a/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs +++ b/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs @@ -9,7 +9,7 @@ use rustc_target::abi::LayoutOf; use crate::interpret::{ intrinsics::{InterpCx, Machine}, - InterpResult, MPlaceTy, MemoryKind, Scalar, + MPlaceTy, MemoryKind, Scalar, }; impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { @@ -79,7 +79,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { filename: Symbol, line: u32, col: u32, - ) -> InterpResult<'static, MPlaceTy<'tcx, M::PointerTag>> { + ) -> MPlaceTy<'tcx, M::PointerTag> { let file = self.allocate_str(&filename.as_str(), MemoryKind::CallerLocation, Mutability::Not); let line = Scalar::from_u32(line); @@ -91,7 +91,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { .type_of(self.tcx.require_lang_item(LangItem::PanicLocation, None)) .subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_erased.into()].iter())); let loc_layout = self.layout_of(loc_ty).unwrap(); - let location = self.allocate(loc_layout, MemoryKind::CallerLocation)?; + // This can fail if rustc runs out of memory right here. Trying to emit an error would be + // pointless, since that would require allocating more memory than a Location. + let location = self.allocate(loc_layout, MemoryKind::CallerLocation).unwrap(); // Initialize fields. self.write_immediate(file.to_ref(), &self.mplace_field(&location, 0).unwrap().into()) @@ -101,7 +103,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { self.write_scalar(col, &self.mplace_field(&location, 2).unwrap().into()) .expect("writing to memory we just allocated cannot fail"); - Ok(location) + location } crate fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) { @@ -114,10 +116,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ) } - pub fn alloc_caller_location_for_span( - &mut self, - span: Span, - ) -> InterpResult<'static, MPlaceTy<'tcx, M::PointerTag>> { + pub fn alloc_caller_location_for_span(&mut self, span: Span) -> MPlaceTy<'tcx, M::PointerTag> { let (file, line, column) = self.location_triple_for_span(span); self.alloc_caller_location(file, line, column) } From a59fafeb13aa39991c3b8c704c66289fd8fd9253 Mon Sep 17 00:00:00 2001 From: Smitty Date: Sat, 19 Jun 2021 10:48:44 -0400 Subject: [PATCH 06/18] Test memory exhaustion in const evaluation --- src/test/ui/consts/large_const_alloc.rs | 6 ++++++ src/test/ui/consts/large_const_alloc.stderr | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/test/ui/consts/large_const_alloc.rs b/src/test/ui/consts/large_const_alloc.rs index 2771af92d30d1..54abaab224730 100644 --- a/src/test/ui/consts/large_const_alloc.rs +++ b/src/test/ui/consts/large_const_alloc.rs @@ -7,6 +7,12 @@ const FOO: () = { //~^ ERROR evaluation of constant value failed }; +static FOO2: () = { + let x = [0_u8; (1 << 47) - 1]; + //~^ ERROR could not evaluate static initializer +}; + fn main() { let _ = FOO; + let _ = FOO2; } diff --git a/src/test/ui/consts/large_const_alloc.stderr b/src/test/ui/consts/large_const_alloc.stderr index 54d56a42061d4..25d660f1217f4 100644 --- a/src/test/ui/consts/large_const_alloc.stderr +++ b/src/test/ui/consts/large_const_alloc.stderr @@ -4,6 +4,12 @@ error[E0080]: evaluation of constant value failed LL | let x = [0_u8; (1 << 47) - 1]; | ^^^^^^^^^^^^^^^^^^^^^ tried to allocate more memory than available to compiler -error: aborting due to previous error +error[E0080]: could not evaluate static initializer + --> $DIR/large_const_alloc.rs:11:13 + | +LL | let x = [0_u8; (1 << 47) - 1]; + | ^^^^^^^^^^^^^^^^^^^^^ tried to allocate more memory than available to compiler + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0080`. From ab66c3fbd4ca7d3ec3e677c94a8fc65e633aa7f4 Mon Sep 17 00:00:00 2001 From: Smitty Date: Sat, 19 Jun 2021 10:49:06 -0400 Subject: [PATCH 07/18] Add comment with reasoning for non-determinism --- compiler/rustc_middle/src/mir/interpret/allocation.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index 36c87f335bd26..550eb84fc8770 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -127,6 +127,15 @@ impl Allocation { pub fn uninit(size: Size, align: Align) -> InterpResult<'static, Self> { let mut bytes = Vec::new(); bytes.try_reserve(size.bytes_usize()).map_err(|_| { + // This results in an error that can happen non-deterministically, since the memory + // available to the compiler can change between runs. Normally queries are always + // deterministic. However, we can be non-determinstic here because all uses of const + // evaluation do one of: + // - cause a fatal compiler error when they see this error as the result of const + // evaluation + // - panic on evaluation failure + // - always evaluate very small constants that are practically unlikely to result in + // memory exhaustion InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) })?; bytes.resize(size.bytes_usize(), 0); From c94bafb69b5a09c7d91959f9be7f5c2d7be1b69d Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 29 Jun 2021 19:17:14 -0400 Subject: [PATCH 08/18] fix sess error This passed x.py check locally, not sure why it wasn't rebased right... --- compiler/rustc_codegen_cranelift/src/vtable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_cranelift/src/vtable.rs b/compiler/rustc_codegen_cranelift/src/vtable.rs index 5788aabaadcd2..7ee34b23e46f7 100644 --- a/compiler/rustc_codegen_cranelift/src/vtable.rs +++ b/compiler/rustc_codegen_cranelift/src/vtable.rs @@ -74,7 +74,7 @@ pub(crate) fn get_vtable<'tcx>( } else { let vtable_alloc_id = match fx.tcx.vtable_allocation(ty, trait_ref) { Ok(alloc) => alloc, - Err(_) => fx.tcx.sess().fatal("allocation of constant vtable failed"), + Err(_) => fx.tcx.sess.fatal("allocation of constant vtable failed"), }; let vtable_allocation = fx.tcx.global_alloc(vtable_alloc_id).unwrap_memory(); let vtable_ptr = pointer_for_allocation(fx, vtable_allocation); From d04da1125d8583e7ae85491eb764c19564b2ab19 Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 29 Jun 2021 20:22:32 -0400 Subject: [PATCH 09/18] Properly handle const prop failures --- .../rustc_middle/src/mir/interpret/allocation.rs | 13 ++++++++----- compiler/rustc_middle/src/mir/interpret/error.rs | 6 ++++++ compiler/rustc_middle/src/ty/vtable.rs | 2 +- compiler/rustc_mir/src/transform/const_prop.rs | 13 +++++++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index 550eb84fc8770..9bc41da1b467b 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -131,11 +131,14 @@ impl Allocation { // available to the compiler can change between runs. Normally queries are always // deterministic. However, we can be non-determinstic here because all uses of const // evaluation do one of: - // - cause a fatal compiler error when they see this error as the result of const - // evaluation - // - panic on evaluation failure - // - always evaluate very small constants that are practically unlikely to result in - // memory exhaustion + // - error on failure + // - used for static initalizer evalution + // - used for const value evaluation + // - const prop errors on this since otherwise it would generate different code based + // on available memory + // - panic on failure to allocate very small sizes + // - actually panicking won't happen since there wouldn't be enough memory to panic + // - used for caller location evaluation InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) })?; bytes.resize(size.bytes_usize(), 0); diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index ab9239585c4aa..2e461015b6201 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -535,4 +535,10 @@ impl InterpError<'_> { _ => false, } } + + /// Did the error originate from volatile conditons such as the memory available to the + /// interpreter? + pub fn is_spurious(&self) -> bool { + matches!(self, InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)) + } } diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index 61c146d03a328..0230e05c12e8a 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -1,6 +1,6 @@ use std::convert::TryFrom; -use crate::mir::interpret::{alloc_range, AllocId, Allocation, Pointer, Scalar, InterpResult}; +use crate::mir::interpret::{alloc_range, AllocId, Allocation, InterpResult, Pointer, Scalar}; use crate::ty::fold::TypeFoldable; use crate::ty::{self, DefId, SubstsRef, Ty, TyCtxt}; use rustc_ast::Mutability; diff --git a/compiler/rustc_mir/src/transform/const_prop.rs b/compiler/rustc_mir/src/transform/const_prop.rs index a660b145b3ff0..3264f16eccd5b 100644 --- a/compiler/rustc_mir/src/transform/const_prop.rs +++ b/compiler/rustc_mir/src/transform/const_prop.rs @@ -478,6 +478,19 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { Ok(op) => Some(op), Err(error) => { let tcx = self.ecx.tcx.at(c.span); + if error.kind().is_spurious() { + // Spurious errors can't be ignored since otherwise the amount of available + // memory influences the result of optimization and the build. The error + // doesn't need to be fatal since no code will actually be generated anyways. + self.ecx + .tcx + .tcx + .sess + .struct_err("memory exhausted during optimization") + .help("try increasing the amount of memory available to the compiler") + .emit(); + return None; + } let err = ConstEvalErr::new(&self.ecx, error, Some(c.span)); if let Some(lint_root) = self.lint_root(source_info) { let lint_only = match c.literal { From 55379bb7ea8ddad827bddc341c3df2038fe4a9fd Mon Sep 17 00:00:00 2001 From: Smittyvb Date: Wed, 30 Jun 2021 09:07:47 -0400 Subject: [PATCH 10/18] simplify explanation comment Co-authored-by: Ralf Jung --- compiler/rustc_middle/src/mir/interpret/allocation.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index 9bc41da1b467b..ea2b104b27bdd 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -130,15 +130,8 @@ impl Allocation { // This results in an error that can happen non-deterministically, since the memory // available to the compiler can change between runs. Normally queries are always // deterministic. However, we can be non-determinstic here because all uses of const - // evaluation do one of: - // - error on failure - // - used for static initalizer evalution - // - used for const value evaluation - // - const prop errors on this since otherwise it would generate different code based - // on available memory - // - panic on failure to allocate very small sizes - // - actually panicking won't happen since there wouldn't be enough memory to panic - // - used for caller location evaluation + // evaluation will make compilation fail (via hard error or ICE) upon + // encountering a `MemoryExhausted` error. InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) })?; bytes.resize(size.bytes_usize(), 0); From ba542eebc0f6f08a7275e0e0ed57f110bc3461f2 Mon Sep 17 00:00:00 2001 From: Smitty Date: Wed, 30 Jun 2021 09:27:30 -0400 Subject: [PATCH 11/18] Rename is_spurious -> is_volatile --- compiler/rustc_middle/src/mir/interpret/error.rs | 2 +- compiler/rustc_mir/src/transform/const_prop.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 2e461015b6201..e17017f722551 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -538,7 +538,7 @@ impl InterpError<'_> { /// Did the error originate from volatile conditons such as the memory available to the /// interpreter? - pub fn is_spurious(&self) -> bool { + pub fn is_volatile(&self) -> bool { matches!(self, InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)) } } diff --git a/compiler/rustc_mir/src/transform/const_prop.rs b/compiler/rustc_mir/src/transform/const_prop.rs index 3264f16eccd5b..5ab6b334ed4b5 100644 --- a/compiler/rustc_mir/src/transform/const_prop.rs +++ b/compiler/rustc_mir/src/transform/const_prop.rs @@ -478,8 +478,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { Ok(op) => Some(op), Err(error) => { let tcx = self.ecx.tcx.at(c.span); - if error.kind().is_spurious() { - // Spurious errors can't be ignored since otherwise the amount of available + if error.kind().is_volatile() { + // Volatile errors can't be ignored since otherwise the amount of available // memory influences the result of optimization and the build. The error // doesn't need to be fatal since no code will actually be generated anyways. self.ecx From 9f227945f141deb0ae1540b0439cd8330d4df454 Mon Sep 17 00:00:00 2001 From: Smitty Date: Wed, 30 Jun 2021 11:24:52 -0400 Subject: [PATCH 12/18] Simplify memory failure checking --- .../rustc_middle/src/mir/interpret/error.rs | 6 ---- .../rustc_mir/src/transform/const_prop.rs | 33 +++++++++---------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index e17017f722551..ab9239585c4aa 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -535,10 +535,4 @@ impl InterpError<'_> { _ => false, } } - - /// Did the error originate from volatile conditons such as the memory available to the - /// interpreter? - pub fn is_volatile(&self) -> bool { - matches!(self, InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)) - } } diff --git a/compiler/rustc_mir/src/transform/const_prop.rs b/compiler/rustc_mir/src/transform/const_prop.rs index 5ab6b334ed4b5..e7ab8c1e35cf0 100644 --- a/compiler/rustc_mir/src/transform/const_prop.rs +++ b/compiler/rustc_mir/src/transform/const_prop.rs @@ -31,9 +31,9 @@ use rustc_trait_selection::traits; use crate::const_eval::ConstEvalErr; use crate::interpret::{ self, compile_time_machine, AllocId, Allocation, ConstValue, CtfeValidationMode, Frame, ImmTy, - Immediate, InterpCx, InterpResult, LocalState, LocalValue, MemPlace, Memory, MemoryKind, OpTy, - Operand as InterpOperand, PlaceTy, Pointer, Scalar, ScalarMaybeUninit, StackPopCleanup, - StackPopUnwind, + Immediate, InterpCx, InterpError, InterpResult, LocalState, LocalValue, MemPlace, Memory, + MemoryKind, OpTy, Operand as InterpOperand, PlaceTy, Pointer, ResourceExhaustionInfo, Scalar, + ScalarMaybeUninit, StackPopCleanup, StackPopUnwind, }; use crate::transform::MirPass; @@ -478,19 +478,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { Ok(op) => Some(op), Err(error) => { let tcx = self.ecx.tcx.at(c.span); - if error.kind().is_volatile() { - // Volatile errors can't be ignored since otherwise the amount of available - // memory influences the result of optimization and the build. The error - // doesn't need to be fatal since no code will actually be generated anyways. - self.ecx - .tcx - .tcx - .sess - .struct_err("memory exhausted during optimization") - .help("try increasing the amount of memory available to the compiler") - .emit(); - return None; - } let err = ConstEvalErr::new(&self.ecx, error, Some(c.span)); if let Some(lint_root) = self.lint_root(source_info) { let lint_only = match c.literal { @@ -507,7 +494,19 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { }, ConstantKind::Val(_, ty) => ty.needs_subst(), }; - if lint_only { + // Memory errors can't be ignored since otherwise the amount of available + // memory influences the result of optimization and the build. The error + // doesn't need to be fatal since no code will actually be generated anyways. + // FIXME(#86255): use err.error.is_hard_err(), but beware of backwards + // compatibility and interactions with promoteds + if lint_only + && !matches!( + err.error, + InterpError::ResourceExhaustion( + ResourceExhaustionInfo::MemoryExhausted, + ), + ) + { // Out of backwards compatibility we cannot report hard errors in unused // generic functions using associated constants of the generic parameters. err.report_as_lint(tcx, "erroneous constant used", lint_root, Some(c.span)); From 4c934df45f6daf0076258fae3d5a6e1d3ccae5b6 Mon Sep 17 00:00:00 2001 From: Smitty Date: Wed, 30 Jun 2021 12:38:12 -0400 Subject: [PATCH 13/18] Properly evaluate non-consts in const prop --- .../rustc_mir/src/transform/const_prop.rs | 59 ++++++++++++++++--- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_mir/src/transform/const_prop.rs b/compiler/rustc_mir/src/transform/const_prop.rs index e7ab8c1e35cf0..c6a193ba44631 100644 --- a/compiler/rustc_mir/src/transform/const_prop.rs +++ b/compiler/rustc_mir/src/transform/const_prop.rs @@ -393,8 +393,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { .filter(|ret_layout| { !ret_layout.is_zst() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) }) - // hopefully all types will allocate, since large types have already been removed - .and_then(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack).ok()) + .and_then(|ret_layout| { + let alloc = ecx.allocate(ret_layout, MemoryKind::Stack); + Self::check_interpresult(tcx, &alloc); + alloc.ok() + }) .map(Into::into); ecx.push_stack_frame( @@ -418,11 +421,27 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { } } + /// Some `InterpError`s could be ignored but must not be to ensure that queries are stable. + fn check_interpresult(tcx: TyCtxt<'tcx>, error: &InterpResult<'tcx, T>) { + if let Err(e) = error { + if matches!( + e.kind(), + InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) + ) { + // Memory errors can't be ignored since otherwise the amount of available + // memory influences the result of optimization and the build. The error + // doesn't need to be fatal since no code will actually be generated anyways. + tcx.sess.fatal("memory exhausted during optimization"); + } + } + } + fn get_const(&self, place: Place<'tcx>) -> Option> { let op = match self.ecx.eval_place_to_op(place, None) { Ok(op) => op, Err(e) => { trace!("get_const failed: {}", e); + Self::check_interpresult::<()>(self.tcx, &Err(e)); return None; } }; @@ -524,7 +543,12 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { /// Returns the value, if any, of evaluating `place`. fn eval_place(&mut self, place: Place<'tcx>) -> Option> { trace!("eval_place(place={:?})", place); - self.use_ecx(|this| this.ecx.eval_place_to_op(place, None)) + let tcx = self.tcx; + self.use_ecx(|this| { + let val = this.ecx.eval_place_to_op(place, None); + Self::check_interpresult(tcx, &val); + val + }) } /// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant` @@ -585,8 +609,17 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { right: &Operand<'tcx>, source_info: SourceInfo, ) -> Option<()> { - let r = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(right, None)?)); - let l = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(left, None)?)); + let tcx = self.tcx; + let r = self.use_ecx(|this| { + let val = this.ecx.read_immediate(&this.ecx.eval_operand(right, None)?); + Self::check_interpresult(tcx, &val); + val + }); + let l = self.use_ecx(|this| { + let val = this.ecx.read_immediate(&this.ecx.eval_operand(left, None)?); + Self::check_interpresult(tcx, &val); + val + }); // Check for exceeding shifts *even if* we cannot evaluate the LHS. if op == BinOp::Shr || op == BinOp::Shl { let r = r?; @@ -752,18 +785,24 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { rvalue: &Rvalue<'tcx>, place: Place<'tcx>, ) -> Option<()> { + let tcx = self.tcx; self.use_ecx(|this| { match rvalue { Rvalue::BinaryOp(op, box (left, right)) | Rvalue::CheckedBinaryOp(op, box (left, right)) => { let l = this.ecx.eval_operand(left, None); let r = this.ecx.eval_operand(right, None); + Self::check_interpresult(tcx, &l); + Self::check_interpresult(tcx, &r); let const_arg = match (l, r) { (Ok(ref x), Err(_)) | (Err(_), Ok(ref x)) => this.ecx.read_immediate(x)?, (Err(e), Err(_)) => return Err(e), (Ok(_), Ok(_)) => { - this.ecx.eval_rvalue_into_place(rvalue, place)?; + Self::check_interpresult( + tcx, + &this.ecx.eval_rvalue_into_place(rvalue, place), + ); return Ok(()); } }; @@ -799,12 +838,16 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { } } _ => { - this.ecx.eval_rvalue_into_place(rvalue, place)?; + let res = this.ecx.eval_rvalue_into_place(rvalue, place); + Self::check_interpresult(tcx, &res); + res? } } } _ => { - this.ecx.eval_rvalue_into_place(rvalue, place)?; + let res = this.ecx.eval_rvalue_into_place(rvalue, place); + Self::check_interpresult(tcx, &res); + res? } } From 12a8d106f6a38f45ec01c1b7d3b2c4bfe62d741d Mon Sep 17 00:00:00 2001 From: Smittyvb Date: Wed, 30 Jun 2021 12:42:04 -0400 Subject: [PATCH 14/18] Note that even ConstProp follows the rules Co-authored-by: Ralf Jung --- compiler/rustc_middle/src/mir/interpret/allocation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index ea2b104b27bdd..273609c639c9e 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -130,8 +130,8 @@ impl Allocation { // This results in an error that can happen non-deterministically, since the memory // available to the compiler can change between runs. Normally queries are always // deterministic. However, we can be non-determinstic here because all uses of const - // evaluation will make compilation fail (via hard error or ICE) upon - // encountering a `MemoryExhausted` error. + // evaluation (including ConstProp!) will make compilation fail (via hard error + // or ICE) upon encountering a `MemoryExhausted` error. InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) })?; bytes.resize(size.bytes_usize(), 0); From 3e20129a18b34ba3aa13efaa53ddfa09dfb1fb7b Mon Sep 17 00:00:00 2001 From: Smitty Date: Wed, 30 Jun 2021 15:38:31 -0400 Subject: [PATCH 15/18] Delay ICE on evaluation fail --- .../src/mir/interpret/allocation.rs | 5 ++ .../rustc_mir/src/transform/const_prop.rs | 83 ++++--------------- 2 files changed, 20 insertions(+), 68 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index 273609c639c9e..49e0af9a3a48a 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -8,6 +8,7 @@ use std::ptr; use rustc_ast::Mutability; use rustc_data_structures::sorted_map::SortedMap; +use rustc_span::DUMMY_SP; use rustc_target::abi::{Align, HasDataLayout, Size}; use super::{ @@ -15,6 +16,7 @@ use super::{ ResourceExhaustionInfo, Scalar, ScalarMaybeUninit, UndefinedBehaviorInfo, UninitBytesAccess, UnsupportedOpInfo, }; +use crate::ty; /// This type represents an Allocation in the Miri/CTFE core engine. /// @@ -132,6 +134,9 @@ impl Allocation { // deterministic. However, we can be non-determinstic here because all uses of const // evaluation (including ConstProp!) will make compilation fail (via hard error // or ICE) upon encountering a `MemoryExhausted` error. + ty::tls::with(|tcx| { + tcx.sess.delay_span_bug(DUMMY_SP, "exhausted memory during interpreation") + }); InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) })?; bytes.resize(size.bytes_usize(), 0); diff --git a/compiler/rustc_mir/src/transform/const_prop.rs b/compiler/rustc_mir/src/transform/const_prop.rs index c6a193ba44631..2a6924d354a86 100644 --- a/compiler/rustc_mir/src/transform/const_prop.rs +++ b/compiler/rustc_mir/src/transform/const_prop.rs @@ -31,9 +31,9 @@ use rustc_trait_selection::traits; use crate::const_eval::ConstEvalErr; use crate::interpret::{ self, compile_time_machine, AllocId, Allocation, ConstValue, CtfeValidationMode, Frame, ImmTy, - Immediate, InterpCx, InterpError, InterpResult, LocalState, LocalValue, MemPlace, Memory, - MemoryKind, OpTy, Operand as InterpOperand, PlaceTy, Pointer, ResourceExhaustionInfo, Scalar, - ScalarMaybeUninit, StackPopCleanup, StackPopUnwind, + Immediate, InterpCx, InterpResult, LocalState, LocalValue, MemPlace, Memory, MemoryKind, OpTy, + Operand as InterpOperand, PlaceTy, Pointer, Scalar, ScalarMaybeUninit, StackPopCleanup, + StackPopUnwind, }; use crate::transform::MirPass; @@ -393,12 +393,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { .filter(|ret_layout| { !ret_layout.is_zst() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) }) - .and_then(|ret_layout| { - let alloc = ecx.allocate(ret_layout, MemoryKind::Stack); - Self::check_interpresult(tcx, &alloc); - alloc.ok() - }) - .map(Into::into); + .map(|ret_layout| { + ecx.allocate(ret_layout, MemoryKind::Stack) + .expect("couldn't perform small allocation") + .into() + }); ecx.push_stack_frame( Instance::new(def_id, substs), @@ -421,27 +420,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { } } - /// Some `InterpError`s could be ignored but must not be to ensure that queries are stable. - fn check_interpresult(tcx: TyCtxt<'tcx>, error: &InterpResult<'tcx, T>) { - if let Err(e) = error { - if matches!( - e.kind(), - InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) - ) { - // Memory errors can't be ignored since otherwise the amount of available - // memory influences the result of optimization and the build. The error - // doesn't need to be fatal since no code will actually be generated anyways. - tcx.sess.fatal("memory exhausted during optimization"); - } - } - } - fn get_const(&self, place: Place<'tcx>) -> Option> { let op = match self.ecx.eval_place_to_op(place, None) { Ok(op) => op, Err(e) => { trace!("get_const failed: {}", e); - Self::check_interpresult::<()>(self.tcx, &Err(e)); return None; } }; @@ -513,19 +496,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { }, ConstantKind::Val(_, ty) => ty.needs_subst(), }; - // Memory errors can't be ignored since otherwise the amount of available - // memory influences the result of optimization and the build. The error - // doesn't need to be fatal since no code will actually be generated anyways. - // FIXME(#86255): use err.error.is_hard_err(), but beware of backwards - // compatibility and interactions with promoteds - if lint_only - && !matches!( - err.error, - InterpError::ResourceExhaustion( - ResourceExhaustionInfo::MemoryExhausted, - ), - ) - { + if lint_only { // Out of backwards compatibility we cannot report hard errors in unused // generic functions using associated constants of the generic parameters. err.report_as_lint(tcx, "erroneous constant used", lint_root, Some(c.span)); @@ -543,12 +514,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { /// Returns the value, if any, of evaluating `place`. fn eval_place(&mut self, place: Place<'tcx>) -> Option> { trace!("eval_place(place={:?})", place); - let tcx = self.tcx; - self.use_ecx(|this| { - let val = this.ecx.eval_place_to_op(place, None); - Self::check_interpresult(tcx, &val); - val - }) + self.use_ecx(|this| this.ecx.eval_place_to_op(place, None)) } /// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant` @@ -609,17 +575,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { right: &Operand<'tcx>, source_info: SourceInfo, ) -> Option<()> { - let tcx = self.tcx; - let r = self.use_ecx(|this| { - let val = this.ecx.read_immediate(&this.ecx.eval_operand(right, None)?); - Self::check_interpresult(tcx, &val); - val - }); - let l = self.use_ecx(|this| { - let val = this.ecx.read_immediate(&this.ecx.eval_operand(left, None)?); - Self::check_interpresult(tcx, &val); - val - }); + let r = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(right, None)?)); + let l = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(left, None)?)); // Check for exceeding shifts *even if* we cannot evaluate the LHS. if op == BinOp::Shr || op == BinOp::Shl { let r = r?; @@ -785,24 +742,18 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { rvalue: &Rvalue<'tcx>, place: Place<'tcx>, ) -> Option<()> { - let tcx = self.tcx; self.use_ecx(|this| { match rvalue { Rvalue::BinaryOp(op, box (left, right)) | Rvalue::CheckedBinaryOp(op, box (left, right)) => { let l = this.ecx.eval_operand(left, None); let r = this.ecx.eval_operand(right, None); - Self::check_interpresult(tcx, &l); - Self::check_interpresult(tcx, &r); let const_arg = match (l, r) { (Ok(ref x), Err(_)) | (Err(_), Ok(ref x)) => this.ecx.read_immediate(x)?, (Err(e), Err(_)) => return Err(e), (Ok(_), Ok(_)) => { - Self::check_interpresult( - tcx, - &this.ecx.eval_rvalue_into_place(rvalue, place), - ); + this.ecx.eval_rvalue_into_place(rvalue, place)?; return Ok(()); } }; @@ -838,16 +789,12 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { } } _ => { - let res = this.ecx.eval_rvalue_into_place(rvalue, place); - Self::check_interpresult(tcx, &res); - res? + this.ecx.eval_rvalue_into_place(rvalue, place)?; } } } _ => { - let res = this.ecx.eval_rvalue_into_place(rvalue, place); - Self::check_interpresult(tcx, &res); - res? + this.ecx.eval_rvalue_into_place(rvalue, place)?; } } From e9d69d9f8eb888a6e124c567f804c2e464c7b00a Mon Sep 17 00:00:00 2001 From: Smitty Date: Fri, 2 Jul 2021 16:06:12 -0400 Subject: [PATCH 16/18] Allocation failure in constprop panics right away --- compiler/rustc_middle/src/mir/interpret/allocation.rs | 5 ++++- compiler/rustc_middle/src/ty/vtable.rs | 2 +- compiler/rustc_mir/src/const_eval/machine.rs | 2 ++ compiler/rustc_mir/src/interpret/machine.rs | 3 +++ compiler/rustc_mir/src/interpret/memory.rs | 2 +- compiler/rustc_mir/src/transform/const_prop.rs | 1 + 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index 49e0af9a3a48a..f1b1bb9ab88ab 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -126,7 +126,7 @@ impl Allocation { /// Try to create an Allocation of `size` bytes, failing if there is not enough memory /// available to the compiler to do so. - pub fn uninit(size: Size, align: Align) -> InterpResult<'static, Self> { + pub fn uninit(size: Size, align: Align, panic_on_fail: bool) -> InterpResult<'static, Self> { let mut bytes = Vec::new(); bytes.try_reserve(size.bytes_usize()).map_err(|_| { // This results in an error that can happen non-deterministically, since the memory @@ -134,6 +134,9 @@ impl Allocation { // deterministic. However, we can be non-determinstic here because all uses of const // evaluation (including ConstProp!) will make compilation fail (via hard error // or ICE) upon encountering a `MemoryExhausted` error. + if panic_on_fail { + panic!("Allocation::uninit called with panic_on_fail had allocation failure") + } ty::tls::with(|tcx| { tcx.sess.delay_span_bug(DUMMY_SP, "exhausted memory during interpreation") }); diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index 0230e05c12e8a..ef1c941bee4e4 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -60,7 +60,7 @@ impl<'tcx> TyCtxt<'tcx> { let ptr_align = tcx.data_layout.pointer_align.abi; let vtable_size = ptr_size * u64::try_from(vtable_entries.len()).unwrap(); - let mut vtable = Allocation::uninit(vtable_size, ptr_align)?; + let mut vtable = Allocation::uninit(vtable_size, ptr_align, true)?; // No need to do any alignment checks on the memory accesses below, because we know the // allocation is correctly aligned as we created it above. Also we're only offsetting by diff --git a/compiler/rustc_mir/src/const_eval/machine.rs b/compiler/rustc_mir/src/const_eval/machine.rs index ddc87084e9fd1..992e32e298f8e 100644 --- a/compiler/rustc_mir/src/const_eval/machine.rs +++ b/compiler/rustc_mir/src/const_eval/machine.rs @@ -201,6 +201,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, type MemoryExtra = MemoryExtra; + const PANIC_ON_ALLOC_FAIL: bool = false; // will be raised as a proper error + fn load_mir( ecx: &InterpCx<'mir, 'tcx, Self>, instance: ty::InstanceDef<'tcx>, diff --git a/compiler/rustc_mir/src/interpret/machine.rs b/compiler/rustc_mir/src/interpret/machine.rs index 0d01dc3c219bc..5b8c0788cbc84 100644 --- a/compiler/rustc_mir/src/interpret/machine.rs +++ b/compiler/rustc_mir/src/interpret/machine.rs @@ -122,6 +122,9 @@ pub trait Machine<'mir, 'tcx>: Sized { /// that is added to the memory so that the work is not done twice. const GLOBAL_KIND: Option; + /// Should the machine panic on allocation failures? + const PANIC_ON_ALLOC_FAIL: bool; + /// Whether memory accesses should be alignment-checked. fn enforce_alignment(memory_extra: &Self::MemoryExtra) -> bool; diff --git a/compiler/rustc_mir/src/interpret/memory.rs b/compiler/rustc_mir/src/interpret/memory.rs index 671e3d278f366..cb929c21850cb 100644 --- a/compiler/rustc_mir/src/interpret/memory.rs +++ b/compiler/rustc_mir/src/interpret/memory.rs @@ -208,7 +208,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { align: Align, kind: MemoryKind, ) -> InterpResult<'static, Pointer> { - let alloc = Allocation::uninit(size, align)?; + let alloc = Allocation::uninit(size, align, M::PANIC_ON_ALLOC_FAIL)?; Ok(self.allocate_with(alloc, kind)) } diff --git a/compiler/rustc_mir/src/transform/const_prop.rs b/compiler/rustc_mir/src/transform/const_prop.rs index 2a6924d354a86..743ba95a9afab 100644 --- a/compiler/rustc_mir/src/transform/const_prop.rs +++ b/compiler/rustc_mir/src/transform/const_prop.rs @@ -181,6 +181,7 @@ impl<'mir, 'tcx> ConstPropMachine<'mir, 'tcx> { impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> { compile_time_machine!(<'mir, 'tcx>); + const PANIC_ON_ALLOC_FAIL: bool = true; // all allocations are small type MemoryKind = !; From b201b2f65f64e7595ae1bfa19f6a8b3c354b9f9d Mon Sep 17 00:00:00 2001 From: Smitty Date: Sat, 3 Jul 2021 11:14:19 -0400 Subject: [PATCH 17/18] Make vtable_allocation always succeed --- compiler/rustc_codegen_cranelift/src/vtable.rs | 5 +---- compiler/rustc_codegen_ssa/src/meth.rs | 5 +---- compiler/rustc_middle/src/ty/vtable.rs | 11 ++++++----- compiler/rustc_mir/src/interpret/traits.rs | 2 +- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/vtable.rs b/compiler/rustc_codegen_cranelift/src/vtable.rs index 7ee34b23e46f7..12f7092d935a3 100644 --- a/compiler/rustc_codegen_cranelift/src/vtable.rs +++ b/compiler/rustc_codegen_cranelift/src/vtable.rs @@ -72,10 +72,7 @@ pub(crate) fn get_vtable<'tcx>( let vtable_ptr = if let Some(vtable_ptr) = fx.vtables.get(&(ty, trait_ref)) { *vtable_ptr } else { - let vtable_alloc_id = match fx.tcx.vtable_allocation(ty, trait_ref) { - Ok(alloc) => alloc, - Err(_) => fx.tcx.sess.fatal("allocation of constant vtable failed"), - }; + let vtable_alloc_id = fx.tcx.vtable_allocation(ty, trait_ref); let vtable_allocation = fx.tcx.global_alloc(vtable_alloc_id).unwrap_memory(); let vtable_ptr = pointer_for_allocation(fx, vtable_allocation); diff --git a/compiler/rustc_codegen_ssa/src/meth.rs b/compiler/rustc_codegen_ssa/src/meth.rs index fcaafb94cfd5b..63245a94c8e3d 100644 --- a/compiler/rustc_codegen_ssa/src/meth.rs +++ b/compiler/rustc_codegen_ssa/src/meth.rs @@ -70,10 +70,7 @@ pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>( return val; } - let vtable_alloc_id = match tcx.vtable_allocation(ty, trait_ref) { - Ok(alloc) => alloc, - Err(_) => tcx.sess.fatal("allocation of constant vtable failed"), - }; + let vtable_alloc_id = tcx.vtable_allocation(ty, trait_ref); let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory(); let vtable_const = cx.const_data_from_alloc(vtable_allocation); let align = cx.data_layout().pointer_align.abi; diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index ef1c941bee4e4..0940137a7c33f 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -1,6 +1,6 @@ use std::convert::TryFrom; -use crate::mir::interpret::{alloc_range, AllocId, Allocation, InterpResult, Pointer, Scalar}; +use crate::mir::interpret::{alloc_range, AllocId, Allocation, Pointer, Scalar}; use crate::ty::fold::TypeFoldable; use crate::ty::{self, DefId, SubstsRef, Ty, TyCtxt}; use rustc_ast::Mutability; @@ -28,11 +28,11 @@ impl<'tcx> TyCtxt<'tcx> { self, ty: Ty<'tcx>, poly_trait_ref: Option>, - ) -> InterpResult<'tcx, AllocId> { + ) -> AllocId { let tcx = self; let vtables_cache = tcx.vtables_cache.lock(); if let Some(alloc_id) = vtables_cache.get(&(ty, poly_trait_ref)).cloned() { - return Ok(alloc_id); + return alloc_id; } drop(vtables_cache); @@ -60,7 +60,8 @@ impl<'tcx> TyCtxt<'tcx> { let ptr_align = tcx.data_layout.pointer_align.abi; let vtable_size = ptr_size * u64::try_from(vtable_entries.len()).unwrap(); - let mut vtable = Allocation::uninit(vtable_size, ptr_align, true)?; + let mut vtable = + Allocation::uninit(vtable_size, ptr_align, /* panic_on_fail */ true).unwrap(); // No need to do any alignment checks on the memory accesses below, because we know the // allocation is correctly aligned as we created it above. Also we're only offsetting by @@ -101,6 +102,6 @@ impl<'tcx> TyCtxt<'tcx> { let alloc_id = tcx.create_memory_alloc(tcx.intern_const_alloc(vtable)); let mut vtables_cache = self.vtables_cache.lock(); vtables_cache.insert((ty, poly_trait_ref), alloc_id); - Ok(alloc_id) + alloc_id } } diff --git a/compiler/rustc_mir/src/interpret/traits.rs b/compiler/rustc_mir/src/interpret/traits.rs index 94948948ccfac..5332e615bc8ea 100644 --- a/compiler/rustc_mir/src/interpret/traits.rs +++ b/compiler/rustc_mir/src/interpret/traits.rs @@ -30,7 +30,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ensure_monomorphic_enough(*self.tcx, ty)?; ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?; - let vtable_allocation = self.tcx.vtable_allocation(ty, poly_trait_ref)?; + let vtable_allocation = self.tcx.vtable_allocation(ty, poly_trait_ref); let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_allocation))?; From d83c46ffcc57eab48249476344982b8f5ae1f263 Mon Sep 17 00:00:00 2001 From: Smittyvb Date: Sat, 3 Jul 2021 11:15:14 -0400 Subject: [PATCH 18/18] add note about MAX_ALLOC_LIMIT Co-authored-by: Ralf Jung --- compiler/rustc_mir/src/transform/const_prop.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir/src/transform/const_prop.rs b/compiler/rustc_mir/src/transform/const_prop.rs index 743ba95a9afab..e9b68754bdf80 100644 --- a/compiler/rustc_mir/src/transform/const_prop.rs +++ b/compiler/rustc_mir/src/transform/const_prop.rs @@ -181,7 +181,7 @@ impl<'mir, 'tcx> ConstPropMachine<'mir, 'tcx> { impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> { compile_time_machine!(<'mir, 'tcx>); - const PANIC_ON_ALLOC_FAIL: bool = true; // all allocations are small + const PANIC_ON_ALLOC_FAIL: bool = true; // all allocations are small (see `MAX_ALLOC_LIMIT`) type MemoryKind = !;