Skip to content

Commit 4081586

Browse files
authored
Rollup merge of rust-lang#66394 - wesleywiser:fix_oom, r=oli-obk
Fix two OOM issues related to `ConstProp` Fixes rust-lang#66342 Fixes rust-lang#66397 r? @oli-obk
2 parents b13272f + 3bd5226 commit 4081586

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/librustc_mir/transform/const_prop.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc::ty::subst::InternalSubsts;
2222
use rustc_data_structures::fx::FxHashMap;
2323
use rustc_index::vec::IndexVec;
2424
use rustc::ty::layout::{
25-
LayoutOf, TyLayout, LayoutError, HasTyCtxt, TargetDataLayout, HasDataLayout,
25+
LayoutOf, TyLayout, LayoutError, HasTyCtxt, TargetDataLayout, HasDataLayout, Size,
2626
};
2727

2828
use crate::rustc::ty::subst::Subst;
@@ -35,6 +35,9 @@ use crate::interpret::{
3535
use crate::const_eval::error_to_const_error;
3636
use crate::transform::{MirPass, MirSource};
3737

38+
/// The maximum number of bytes that we'll allocate space for a return value.
39+
const MAX_ALLOC_LIMIT: u64 = 1024;
40+
3841
pub struct ConstProp;
3942

4043
impl<'tcx> MirPass<'tcx> for ConstProp {
@@ -313,8 +316,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
313316
ecx
314317
.layout_of(body.return_ty().subst(tcx, substs))
315318
.ok()
316-
// Don't bother allocating memory for ZST types which have no values.
317-
.filter(|ret_layout| !ret_layout.is_zst())
319+
// Don't bother allocating memory for ZST types which have no values
320+
// or for large values.
321+
.filter(|ret_layout| !ret_layout.is_zst() &&
322+
ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT))
318323
.map(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack));
319324

320325
ecx.push_stack_frame(
@@ -453,6 +458,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
453458
) -> Option<()> {
454459
let span = source_info.span;
455460

461+
// #66397: Don't try to eval into large places as that can cause an OOM
462+
if place_layout.size >= Size::from_bytes(MAX_ALLOC_LIMIT) {
463+
return None;
464+
}
465+
456466
let overflow_check = self.tcx.sess.overflow_checks();
457467

458468
// Perform any special handling for specific Rvalue types.

src/test/ui/consts/issue-66342.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
3+
// Checks that the compiler does not actually try to allocate 4 TB during compilation and OOM crash.
4+
5+
#[cfg(target_pointer_width = "64")]
6+
const SIZE: usize = 4 * 1024 * 1024 * 1024 * 1024;
7+
#[cfg(target_pointer_width = "32")]
8+
const SIZE: usize = 2 * 1024 * 1024 * 1024;
9+
10+
fn foo() -> [u8; SIZE] {
11+
unimplemented!()
12+
}
13+
14+
fn main() {
15+
foo();
16+
}

src/test/ui/consts/issue-66397.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// build-pass
2+
3+
// Checks that the compiler does not actually try to allocate 4 TB during compilation and OOM crash.
4+
5+
#[cfg(target_pointer_width = "64")]
6+
const SIZE: usize = 4 * 1024 * 1024 * 1024 * 1024;
7+
#[cfg(target_pointer_width = "32")]
8+
const SIZE: usize = 2 * 1024 * 1024 * 1024;
9+
10+
fn main() {
11+
[0; SIZE];
12+
}

0 commit comments

Comments
 (0)