Skip to content

Commit

Permalink
[ConstProp] Avoid OOM crashes by not evaluating large Places
Browse files Browse the repository at this point in the history
Fix spurious CI filures due to OOM

Fixes rust-lang#66397
  • Loading branch information
wesleywiser committed Dec 6, 2019
1 parent 1463fed commit ea9878b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc::ty::subst::InternalSubsts;
use rustc_data_structures::fx::FxHashMap;
use rustc_index::vec::IndexVec;
use rustc::ty::layout::{
LayoutOf, TyLayout, LayoutError, HasTyCtxt, TargetDataLayout, HasDataLayout,
LayoutOf, TyLayout, LayoutError, HasTyCtxt, TargetDataLayout, HasDataLayout, Size,
};

use crate::interpret::{
Expand All @@ -34,6 +34,9 @@ use crate::interpret::{
use crate::const_eval::error_to_const_error;
use crate::transform::{MirPass, MirSource};

/// The maximum number of bytes that we'll allocate space for a return value.
const MAX_ALLOC_LIMIT: u64 = 1024;

pub struct ConstProp;

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

// #66397: Don't try to eval into large places as that can cause an OOM
if place_layout.size >= Size::from_bytes(MAX_ALLOC_LIMIT) {
return None;
}

let overflow_check = self.tcx.sess.overflow_checks();

// Perform any special handling for specific Rvalue types.
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/consts/issue-66342.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// check-pass
// only-x86_64

// Checks that the compiler does not actually try to allocate 4 TB during compilation and OOM crash.

fn foo() -> [u8; 4 * 1024 * 1024 * 1024 * 1024] {
unimplemented!()
}

fn main() {
foo();
}
8 changes: 8 additions & 0 deletions src/test/ui/consts/issue-66397.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-pass
// only-x86_64

// Checks that the compiler does not actually try to allocate 4 TB during compilation and OOM crash.

fn main() {
[0; 4 * 1024 * 1024 * 1024 * 1024];
}

0 comments on commit ea9878b

Please sign in to comment.