Skip to content

Commit 75611d2

Browse files
wesleywiserMark-Simulacrum
authored andcommitted
[ConstProp] Avoid OOM crashes by not evaluating large Places
Fix spurious CI filures due to OOM Fixes #66397
1 parent 0f177c3 commit 75611d2

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/librustc_mir/transform/const_prop.rs

+9-1
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::interpret::{
@@ -34,6 +34,9 @@ use crate::interpret::{
3434
use crate::const_eval::error_to_const_error;
3535
use crate::transform::{MirPass, MirSource};
3636

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

3942
impl<'tcx> MirPass<'tcx> for ConstProp {
@@ -434,6 +437,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
434437
) -> Option<()> {
435438
let span = source_info.span;
436439

440+
// #66397: Don't try to eval into large places as that can cause an OOM
441+
if place_layout.size >= Size::from_bytes(MAX_ALLOC_LIMIT) {
442+
return None;
443+
}
444+
437445
let overflow_check = self.tcx.sess.overflow_checks();
438446

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

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

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-pass
2+
// only-x86_64
3+
4+
// Checks that the compiler does not actually try to allocate 4 TB during compilation and OOM crash.
5+
6+
fn foo() -> [u8; 4 * 1024 * 1024 * 1024 * 1024] {
7+
unimplemented!()
8+
}
9+
10+
fn main() {
11+
foo();
12+
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// check-pass
2+
// only-x86_64
3+
4+
// Checks that the compiler does not actually try to allocate 4 TB during compilation and OOM crash.
5+
6+
fn main() {
7+
[0; 4 * 1024 * 1024 * 1024 * 1024];
8+
}

0 commit comments

Comments
 (0)