Skip to content

Commit 524e575

Browse files
committed
Support allocation failures when interperting MIR
Note that this breaks Miri. Closes rust-lang#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);

0 commit comments

Comments
 (0)