Skip to content

Commit 524e575

Browse files
committed
Support allocation failures when interperting MIR
Note that this breaks Miri. Closes #79601
1 parent 6e0b554 commit 524e575

File tree

19 files changed

+103
-39
lines changed

19 files changed

+103
-39
lines changed

compiler/rustc_codegen_cranelift/src/vtable.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ pub(crate) fn get_vtable<'tcx>(
7272
let vtable_ptr = if let Some(vtable_ptr) = fx.vtables.get(&(ty, trait_ref)) {
7373
*vtable_ptr
7474
} else {
75-
let vtable_alloc_id = fx.tcx.vtable_allocation(ty, trait_ref);
75+
let vtable_alloc_id = match fx.tcx.vtable_allocation(ty, trait_ref) {
76+
Ok(alloc) => alloc,
77+
Err(_) => fx.tcx.sess().fatal("allocation of constant vtable failed"),
78+
};
7679
let vtable_allocation = fx.tcx.global_alloc(vtable_alloc_id).unwrap_memory();
7780
let vtable_ptr = pointer_for_allocation(fx, vtable_allocation);
7881

compiler/rustc_codegen_ssa/src/meth.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
7070
return val;
7171
}
7272

73-
let vtable_alloc_id = tcx.vtable_allocation(ty, trait_ref);
73+
let vtable_alloc_id = match tcx.vtable_allocation(ty, trait_ref) {
74+
Ok(alloc) => alloc,
75+
Err(_) => tcx.sess.fatal("allocation of constant vtable failed"),
76+
};
7477
let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory();
7578
let vtable_const = cx.const_data_from_alloc(vtable_allocation);
7679
let align = cx.data_layout().pointer_align.abi;

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#![feature(associated_type_defaults)]
4949
#![feature(iter_zip)]
5050
#![feature(thread_local_const_init)]
51+
#![feature(try_reserve)]
5152
#![recursion_limit = "512"]
5253

5354
#[macro_use]

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use rustc_data_structures::sorted_map::SortedMap;
1111
use rustc_target::abi::{Align, HasDataLayout, Size};
1212

1313
use super::{
14-
read_target_uint, write_target_uint, AllocId, InterpError, Pointer, Scalar, ScalarMaybeUninit,
15-
UndefinedBehaviorInfo, UninitBytesAccess, UnsupportedOpInfo,
14+
read_target_uint, write_target_uint, AllocId, InterpError, InterpResult, Pointer,
15+
ResourceExhaustionInfo, Scalar, ScalarMaybeUninit, UndefinedBehaviorInfo, UninitBytesAccess,
16+
UnsupportedOpInfo,
1617
};
1718

1819
/// This type represents an Allocation in the Miri/CTFE core engine.
@@ -121,15 +122,23 @@ impl<Tag> Allocation<Tag> {
121122
Allocation::from_bytes(slice, Align::ONE, Mutability::Not)
122123
}
123124

124-
pub fn uninit(size: Size, align: Align) -> Self {
125-
Allocation {
126-
bytes: vec![0; size.bytes_usize()],
125+
/// Try to create an Allocation of `size` bytes, failing if there is not enough memory
126+
/// available to the compiler to do so.
127+
pub fn uninit(size: Size, align: Align) -> InterpResult<'static, Self> {
128+
let mut bytes = Vec::new();
129+
bytes.try_reserve(size.bytes_usize()).map_err(|_| {
130+
InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)
131+
})?;
132+
bytes.resize(size.bytes_usize(), 0);
133+
bytes.fill(0);
134+
Ok(Allocation {
135+
bytes: bytes,
127136
relocations: Relocations::new(),
128137
init_mask: InitMask::new(size, false),
129138
align,
130139
mutability: Mutability::Mut,
131140
extra: (),
132-
}
141+
})
133142
}
134143
}
135144

compiler/rustc_middle/src/mir/interpret/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ pub enum ResourceExhaustionInfo {
423423
///
424424
/// The exact limit is set by the `const_eval_limit` attribute.
425425
StepLimitReached,
426+
/// There is not enough memory to perform an allocation.
427+
MemoryExhausted,
426428
}
427429

428430
impl fmt::Display for ResourceExhaustionInfo {
@@ -435,6 +437,9 @@ impl fmt::Display for ResourceExhaustionInfo {
435437
StepLimitReached => {
436438
write!(f, "exceeded interpreter step limit (see `#[const_eval_limit]`)")
437439
}
440+
MemoryExhausted => {
441+
write!(f, "tried to allocate more memory than available to compiler")
442+
}
438443
}
439444
}
440445
}

compiler/rustc_middle/src/ty/vtable.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::convert::TryFrom;
22

3-
use crate::mir::interpret::{alloc_range, AllocId, Allocation, Pointer, Scalar};
3+
use crate::mir::interpret::{alloc_range, AllocId, Allocation, Pointer, Scalar, InterpResult};
44
use crate::ty::fold::TypeFoldable;
55
use crate::ty::{self, DefId, SubstsRef, Ty, TyCtxt};
66
use rustc_ast::Mutability;
@@ -28,11 +28,11 @@ impl<'tcx> TyCtxt<'tcx> {
2828
self,
2929
ty: Ty<'tcx>,
3030
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
31-
) -> AllocId {
31+
) -> InterpResult<'tcx, AllocId> {
3232
let tcx = self;
3333
let vtables_cache = tcx.vtables_cache.lock();
3434
if let Some(alloc_id) = vtables_cache.get(&(ty, poly_trait_ref)).cloned() {
35-
return alloc_id;
35+
return Ok(alloc_id);
3636
}
3737
drop(vtables_cache);
3838

@@ -60,7 +60,7 @@ impl<'tcx> TyCtxt<'tcx> {
6060
let ptr_align = tcx.data_layout.pointer_align.abi;
6161

6262
let vtable_size = ptr_size * u64::try_from(vtable_entries.len()).unwrap();
63-
let mut vtable = Allocation::uninit(vtable_size, ptr_align);
63+
let mut vtable = Allocation::uninit(vtable_size, ptr_align)?;
6464

6565
// No need to do any alignment checks on the memory accesses below, because we know the
6666
// allocation is correctly aligned as we created it above. Also we're only offsetting by
@@ -101,6 +101,6 @@ impl<'tcx> TyCtxt<'tcx> {
101101
let alloc_id = tcx.create_memory_alloc(tcx.intern_const_alloc(vtable));
102102
let mut vtables_cache = self.vtables_cache.lock();
103103
vtables_cache.insert((ty, poly_trait_ref), alloc_id);
104-
alloc_id
104+
Ok(alloc_id)
105105
}
106106
}

compiler/rustc_mir/src/const_eval/eval_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
4848
);
4949
let layout = ecx.layout_of(body.return_ty().subst(tcx, cid.instance.substs))?;
5050
assert!(!layout.is_unsized());
51-
let ret = ecx.allocate(layout, MemoryKind::Stack);
51+
let ret = ecx.allocate(layout, MemoryKind::Stack)?;
5252

5353
let name =
5454
with_no_trimmed_paths(|| ty::tls::with(|tcx| tcx.def_path_str(cid.instance.def_id())));

compiler/rustc_mir/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
306306
Size::from_bytes(size as u64),
307307
align,
308308
interpret::MemoryKind::Machine(MemoryKind::Heap),
309-
);
309+
)?;
310310
ecx.write_scalar(Scalar::Ptr(ptr), dest)?;
311311
}
312312
_ => {

compiler/rustc_mir/src/const_eval/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ pub(crate) fn const_caller_location(
3131
trace!("const_caller_location: {}:{}:{}", file, line, col);
3232
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false);
3333

34-
let loc_place = ecx.alloc_caller_location(file, line, col);
34+
// This can fail if rustc runs out of memory right here. Trying to emit an error would be
35+
// pointless, since that would require allocating more memory than a Location.
36+
let loc_place = ecx
37+
.alloc_caller_location(file, line, col)
38+
.expect("not enough memory to allocate location?");
3539
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
3640
bug!("intern_const_alloc_recursive should not error in this case")
3741
}

compiler/rustc_mir/src/interpret/intern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>>
428428
&MPlaceTy<'tcx, M::PointerTag>,
429429
) -> InterpResult<'tcx, ()>,
430430
) -> InterpResult<'tcx, &'tcx Allocation> {
431-
let dest = self.allocate(layout, MemoryKind::Stack);
431+
let dest = self.allocate(layout, MemoryKind::Stack)?;
432432
f(self, &dest)?;
433433
let ptr = dest.ptr.assert_ptr();
434434
assert_eq!(ptr.offset, Size::ZERO);

compiler/rustc_mir/src/interpret/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
137137
match intrinsic_name {
138138
sym::caller_location => {
139139
let span = self.find_closest_untracked_caller_location();
140-
let location = self.alloc_caller_location_for_span(span);
140+
let location = self.alloc_caller_location_for_span(span)?;
141141
self.write_scalar(location.ptr, dest)?;
142142
}
143143

compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_target::abi::LayoutOf;
99

1010
use crate::interpret::{
1111
intrinsics::{InterpCx, Machine},
12-
MPlaceTy, MemoryKind, Scalar,
12+
InterpResult, MPlaceTy, MemoryKind, Scalar,
1313
};
1414

1515
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> {
7979
filename: Symbol,
8080
line: u32,
8181
col: u32,
82-
) -> MPlaceTy<'tcx, M::PointerTag> {
82+
) -> InterpResult<'static, MPlaceTy<'tcx, M::PointerTag>> {
8383
let file =
8484
self.allocate_str(&filename.as_str(), MemoryKind::CallerLocation, Mutability::Not);
8585
let line = Scalar::from_u32(line);
@@ -91,7 +91,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
9191
.type_of(self.tcx.require_lang_item(LangItem::PanicLocation, None))
9292
.subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_erased.into()].iter()));
9393
let loc_layout = self.layout_of(loc_ty).unwrap();
94-
let location = self.allocate(loc_layout, MemoryKind::CallerLocation);
94+
let location = self.allocate(loc_layout, MemoryKind::CallerLocation)?;
9595

9696
// Initialize fields.
9797
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> {
101101
self.write_scalar(col, &self.mplace_field(&location, 2).unwrap().into())
102102
.expect("writing to memory we just allocated cannot fail");
103103

104-
location
104+
Ok(location)
105105
}
106106

107107
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> {
114114
)
115115
}
116116

117-
pub fn alloc_caller_location_for_span(&mut self, span: Span) -> MPlaceTy<'tcx, M::PointerTag> {
117+
pub fn alloc_caller_location_for_span(
118+
&mut self,
119+
span: Span,
120+
) -> InterpResult<'static, MPlaceTy<'tcx, M::PointerTag>> {
118121
let (file, line, column) = self.location_triple_for_span(span);
119122
self.alloc_caller_location(file, line, column)
120123
}

compiler/rustc_mir/src/interpret/memory.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
207207
size: Size,
208208
align: Align,
209209
kind: MemoryKind<M::MemoryKind>,
210-
) -> Pointer<M::PointerTag> {
211-
let alloc = Allocation::uninit(size, align);
212-
self.allocate_with(alloc, kind)
210+
) -> InterpResult<'static, Pointer<M::PointerTag>> {
211+
let alloc = Allocation::uninit(size, align)?;
212+
Ok(self.allocate_with(alloc, kind))
213213
}
214214

215215
pub fn allocate_bytes(
@@ -257,7 +257,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
257257

258258
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
259259
// This happens so rarely, the perf advantage is outweighed by the maintenance cost.
260-
let new_ptr = self.allocate(new_size, new_align, kind);
260+
let new_ptr = self.allocate(new_size, new_align, kind)?;
261261
let old_size = match old_size_and_align {
262262
Some((size, _align)) => size,
263263
None => self.get_raw(ptr.alloc_id)?.size(),

compiler/rustc_mir/src/interpret/place.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ where
982982
let (size, align) = self
983983
.size_and_align_of(&meta, &local_layout)?
984984
.expect("Cannot allocate for non-dyn-sized type");
985-
let ptr = self.memory.allocate(size, align, MemoryKind::Stack);
985+
let ptr = self.memory.allocate(size, align, MemoryKind::Stack)?;
986986
let mplace = MemPlace { ptr: ptr.into(), align, meta };
987987
if let LocalValue::Live(Operand::Immediate(value)) = local_val {
988988
// Preserve old value.
@@ -1018,9 +1018,9 @@ where
10181018
&mut self,
10191019
layout: TyAndLayout<'tcx>,
10201020
kind: MemoryKind<M::MemoryKind>,
1021-
) -> MPlaceTy<'tcx, M::PointerTag> {
1022-
let ptr = self.memory.allocate(layout.size, layout.align.abi, kind);
1023-
MPlaceTy::from_aligned_ptr(ptr, layout)
1021+
) -> InterpResult<'static, MPlaceTy<'tcx, M::PointerTag>> {
1022+
let ptr = self.memory.allocate(layout.size, layout.align.abi, kind)?;
1023+
Ok(MPlaceTy::from_aligned_ptr(ptr, layout))
10241024
}
10251025

10261026
/// Returns a wide MPlace of type `&'static [mut] str` to a new 1-aligned allocation.

compiler/rustc_mir/src/interpret/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
3030
ensure_monomorphic_enough(*self.tcx, ty)?;
3131
ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?;
3232

33-
let vtable_allocation = self.tcx.vtable_allocation(ty, poly_trait_ref);
33+
let vtable_allocation = self.tcx.vtable_allocation(ty, poly_trait_ref)?;
3434

3535
let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_allocation))?;
3636

compiler/rustc_mir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Rust MIR: a lowered representation of Rust.
2929
#![feature(option_get_or_insert_default)]
3030
#![feature(once_cell)]
3131
#![feature(control_flow_enum)]
32+
#![feature(try_reserve)]
3233
#![recursion_limit = "256"]
3334

3435
#[macro_use]

compiler/rustc_mir/src/transform/const_prop.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,19 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
385385
(),
386386
);
387387

388-
let ret = ecx
389-
.layout_of(body.return_ty().subst(tcx, substs))
390-
.ok()
388+
let ret = if let Ok(layout) = ecx.layout_of(body.return_ty().subst(tcx, substs)) {
391389
// Don't bother allocating memory for ZST types which have no values
392390
// or for large values.
393-
.filter(|ret_layout| {
394-
!ret_layout.is_zst() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT)
395-
})
396-
.map(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack).into());
391+
if !layout.is_zst() && layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) {
392+
// hopefully all types will allocate, since large types have already been removed,
393+
// but check anyways
394+
ecx.allocate(layout, MemoryKind::Stack).ok().map(Into::into)
395+
} else {
396+
None
397+
}
398+
} else {
399+
None
400+
};
397401

398402
ecx.push_stack_frame(
399403
Instance::new(def_id, substs),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// only-64bit
2+
// on 32bit and 16bit platforms it is plausible that the maximum allocation size will succeed
3+
4+
const FOO: () = {
5+
// 128 TiB, unlikely anyone has that much RAM
6+
let x = [0_u8; (1 << 47) - 1];
7+
//~^ ERROR any use of this value will cause an error
8+
//~| WARNING this was previously accepted by the compiler but is being phased out
9+
};
10+
11+
fn main() {
12+
let _ = FOO;
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/large_const_alloc.rs:6:13
3+
|
4+
LL | / const FOO: () = {
5+
LL | | // 128 TiB, unlikely anyone has that much RAM
6+
LL | | let x = [0_u8; (1 << 47) - 1];
7+
| | ^^^^^^^^^^^^^^^^^^^^^ tried to allocate more memory than available to compiler
8+
LL | |
9+
LL | |
10+
LL | | };
11+
| |__-
12+
|
13+
= note: `#[deny(const_err)]` on by default
14+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
15+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
16+
17+
error: aborting due to previous error
18+

0 commit comments

Comments
 (0)