Auto merge of #63152 - estebank:big-array, r=oli-obk

Always error on `SizeOverflow` during mir evaluation

Fix #55878, fix #25116.

r? @oli-obk
This commit is contained in:
bors 2019-08-07 04:28:08 +00:00
commit 5421d94960
29 changed files with 172 additions and 74 deletions

View File

@ -137,17 +137,17 @@ impl<'tcx> ConstEvalErr<'tcx> {
message: &str, message: &str,
lint_root: Option<hir::HirId>, lint_root: Option<hir::HirId>,
) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> { ) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> {
match self.error { let must_error = match self.error {
err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(Layout(LayoutError::Unknown(_))) |
err_inval!(TooGeneric) => err_inval!(TooGeneric) =>
return Err(ErrorHandled::TooGeneric), return Err(ErrorHandled::TooGeneric),
err_inval!(Layout(LayoutError::SizeOverflow(_))) |
err_inval!(TypeckError) => err_inval!(TypeckError) =>
return Err(ErrorHandled::Reported), return Err(ErrorHandled::Reported),
_ => {}, err_inval!(Layout(LayoutError::SizeOverflow(_))) => true,
} _ => false,
};
trace!("reporting const eval failure at {:?}", self.span); trace!("reporting const eval failure at {:?}", self.span);
let mut err = if let Some(lint_root) = lint_root { let mut err = if let (Some(lint_root), false) = (lint_root, must_error) {
let hir_id = self.stacktrace let hir_id = self.stacktrace
.iter() .iter()
.rev() .rev()
@ -160,10 +160,14 @@ impl<'tcx> ConstEvalErr<'tcx> {
tcx.span, tcx.span,
message, message,
) )
} else if must_error {
struct_error(tcx, &self.error.to_string())
} else { } else {
struct_error(tcx, message) struct_error(tcx, message)
}; };
err.span_label(self.span, self.error.to_string()); if !must_error {
err.span_label(self.span, self.error.to_string());
}
// Skip the last, which is just the environment of the constant. The stacktrace // Skip the last, which is just the environment of the constant. The stacktrace
// is sometimes empty because we create "fake" eval contexts in CTFE to do work // is sometimes empty because we create "fake" eval contexts in CTFE to do work
// on constant values. // on constant values.
@ -335,7 +339,7 @@ impl fmt::Debug for InvalidProgramInfo<'tcx> {
TypeckError => TypeckError =>
write!(f, "encountered constants with type errors, stopping evaluation"), write!(f, "encountered constants with type errors, stopping evaluation"),
Layout(ref err) => Layout(ref err) =>
write!(f, "rustc layout computation failed: {:?}", err), write!(f, "{}", err),
} }
} }
} }

View File

@ -30,6 +30,7 @@ use std::iter;
use std::str; use std::str;
use std::sync::Arc; use std::sync::Arc;
use syntax::symbol::LocalInternedString; use syntax::symbol::LocalInternedString;
use syntax::source_map::{DUMMY_SP, Span};
use crate::abi::Abi; use crate::abi::Abi;
/// There is one `CodegenCx` per compilation unit. Each one has its own LLVM /// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
@ -860,9 +861,13 @@ impl LayoutOf for CodegenCx<'ll, 'tcx> {
type TyLayout = TyLayout<'tcx>; type TyLayout = TyLayout<'tcx>;
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout { fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
self.spanned_layout_of(ty, DUMMY_SP)
}
fn spanned_layout_of(&self, ty: Ty<'tcx>, span: Span) -> Self::TyLayout {
self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)) self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty))
.unwrap_or_else(|e| if let LayoutError::SizeOverflow(_) = e { .unwrap_or_else(|e| if let LayoutError::SizeOverflow(_) = e {
self.sess().fatal(&e.to_string()) self.sess().span_fatal(span, &e.to_string())
} else { } else {
bug!("failed to get layout for `{}`: {}", ty, e) bug!("failed to get layout for `{}`: {}", ty, e)
}) })

View File

@ -9,6 +9,7 @@ use rustc::mir::visit::{Visitor, PlaceContext, MutatingUseContext, NonMutatingUs
use rustc::mir::traversal; use rustc::mir::traversal;
use rustc::ty; use rustc::ty;
use rustc::ty::layout::{LayoutOf, HasTyCtxt}; use rustc::ty::layout::{LayoutOf, HasTyCtxt};
use syntax_pos::DUMMY_SP;
use super::FunctionCx; use super::FunctionCx;
use crate::traits::*; use crate::traits::*;
@ -20,10 +21,13 @@ pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
analyzer.visit_body(mir); analyzer.visit_body(mir);
for (index, ty) in mir.local_decls.iter().map(|l| l.ty).enumerate() { for (index, (ty, span)) in mir.local_decls.iter()
.map(|l| (l.ty, l.source_info.span))
.enumerate()
{
let ty = fx.monomorphize(&ty); let ty = fx.monomorphize(&ty);
debug!("local {} has type {:?}", index, ty); debug!("local {} has type {:?}", index, ty);
let layout = fx.cx.layout_of(ty); let layout = fx.cx.spanned_layout_of(ty, span);
if fx.cx.is_backend_immediate(layout) { if fx.cx.is_backend_immediate(layout) {
// These sorts of types are immediates that we can store // These sorts of types are immediates that we can store
// in an Value without an alloca. // in an Value without an alloca.
@ -93,10 +97,12 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
} }
} }
fn process_place(&mut self, fn process_place(
place_ref: &mir::PlaceRef<'_, 'tcx>, &mut self,
context: PlaceContext, place_ref: &mir::PlaceRef<'_, 'tcx>,
location: Location) { context: PlaceContext,
location: Location,
) {
let cx = self.fx.cx; let cx = self.fx.cx;
if let Some(proj) = place_ref.projection { if let Some(proj) = place_ref.projection {
@ -116,12 +122,17 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
.projection_ty(cx.tcx(), &proj.elem) .projection_ty(cx.tcx(), &proj.elem)
.ty; .ty;
let elem_ty = self.fx.monomorphize(&elem_ty); let elem_ty = self.fx.monomorphize(&elem_ty);
if cx.layout_of(elem_ty).is_zst() { let span = if let mir::PlaceBase::Local(index) = place_ref.base {
self.fx.mir.local_decls[*index].source_info.span
} else {
DUMMY_SP
};
if cx.spanned_layout_of(elem_ty, span).is_zst() {
return; return;
} }
if let mir::ProjectionElem::Field(..) = proj.elem { if let mir::ProjectionElem::Field(..) = proj.elem {
let layout = cx.layout_of(base_ty.ty); let layout = cx.spanned_layout_of(base_ty.ty, span);
if cx.is_backend_immediate(layout) || cx.is_backend_scalar_pair(layout) { if cx.is_backend_immediate(layout) || cx.is_backend_scalar_pair(layout) {
// Recurse with the same context, instead of `Projection`, // Recurse with the same context, instead of `Projection`,
// potentially stopping at non-operand projections, // potentially stopping at non-operand projections,
@ -188,7 +199,8 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
projection: None, projection: None,
} = *place { } = *place {
self.assign(index, location); self.assign(index, location);
if !self.fx.rvalue_creates_operand(rvalue) { let decl_span = self.fx.mir.local_decls[index].source_info.span;
if !self.fx.rvalue_creates_operand(rvalue, decl_span) {
self.not_ssa(index); self.not_ssa(index);
} }
} else { } else {

View File

@ -6,6 +6,7 @@ use rustc::middle::lang_items::ExchangeMallocFnLangItem;
use rustc_apfloat::{ieee, Float, Status, Round}; use rustc_apfloat::{ieee, Float, Status, Round};
use std::{u128, i128}; use std::{u128, i128};
use syntax::symbol::sym; use syntax::symbol::sym;
use syntax::source_map::{DUMMY_SP, Span};
use crate::base; use crate::base;
use crate::MemFlags; use crate::MemFlags;
@ -136,7 +137,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
} }
_ => { _ => {
assert!(self.rvalue_creates_operand(rvalue)); assert!(self.rvalue_creates_operand(rvalue, DUMMY_SP));
let (mut bx, temp) = self.codegen_rvalue_operand(bx, rvalue); let (mut bx, temp) = self.codegen_rvalue_operand(bx, rvalue);
temp.val.store(&mut bx, dest); temp.val.store(&mut bx, dest);
bx bx
@ -169,7 +170,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
mut bx: Bx, mut bx: Bx,
rvalue: &mir::Rvalue<'tcx> rvalue: &mir::Rvalue<'tcx>
) -> (Bx, OperandRef<'tcx, Bx::Value>) { ) -> (Bx, OperandRef<'tcx, Bx::Value>) {
assert!(self.rvalue_creates_operand(rvalue), "cannot codegen {:?} to operand", rvalue); assert!(
self.rvalue_creates_operand(rvalue, DUMMY_SP),
"cannot codegen {:?} to operand",
rvalue,
);
match *rvalue { match *rvalue {
mir::Rvalue::Cast(ref kind, ref source, mir_cast_ty) => { mir::Rvalue::Cast(ref kind, ref source, mir_cast_ty) => {
@ -691,7 +696,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
} }
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
pub fn rvalue_creates_operand(&self, rvalue: &mir::Rvalue<'tcx>) -> bool { pub fn rvalue_creates_operand(&self, rvalue: &mir::Rvalue<'tcx>, span: Span) -> bool {
match *rvalue { match *rvalue {
mir::Rvalue::Ref(..) | mir::Rvalue::Ref(..) |
mir::Rvalue::Len(..) | mir::Rvalue::Len(..) |
@ -707,7 +712,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
mir::Rvalue::Aggregate(..) => { mir::Rvalue::Aggregate(..) => {
let ty = rvalue.ty(self.mir, self.cx.tcx()); let ty = rvalue.ty(self.mir, self.cx.tcx());
let ty = self.monomorphize(&ty); let ty = self.monomorphize(&ty);
self.cx.layout_of(ty).is_zst() self.cx.spanned_layout_of(ty, span).is_zst()
} }
} }

View File

@ -506,7 +506,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
pub fn push_stack_frame( pub fn push_stack_frame(
&mut self, &mut self,
instance: ty::Instance<'tcx>, instance: ty::Instance<'tcx>,
span: source_map::Span, span: Span,
body: &'mir mir::Body<'tcx>, body: &'mir mir::Body<'tcx>,
return_place: Option<PlaceTy<'tcx, M::PointerTag>>, return_place: Option<PlaceTy<'tcx, M::PointerTag>>,
return_to_block: StackPopCleanup, return_to_block: StackPopCleanup,

View File

@ -9,6 +9,7 @@ use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive};
use rustc_data_structures::newtype_index; use rustc_data_structures::newtype_index;
use rustc_data_structures::indexed_vec::{Idx, IndexVec}; use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use syntax_pos::symbol::{sym, Symbol}; use syntax_pos::symbol::{sym, Symbol};
use syntax_pos::Span;
pub mod call; pub mod call;
@ -1012,6 +1013,9 @@ pub trait LayoutOf {
type TyLayout; type TyLayout;
fn layout_of(&self, ty: Self::Ty) -> Self::TyLayout; fn layout_of(&self, ty: Self::Ty) -> Self::TyLayout;
fn spanned_layout_of(&self, ty: Self::Ty, _span: Span) -> Self::TyLayout {
self.layout_of(ty)
}
} }
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]

View File

@ -0,0 +1,7 @@
// normalize-stderr-64bit "18446744073709551615" -> "SIZE"
// normalize-stderr-32bit "4294967295" -> "SIZE"
// error-pattern: is too big for the current architecture
fn main() {
println!("Size: {}", std::mem::size_of::<[u8; std::u64::MAX as usize]>());
}

View File

@ -0,0 +1,11 @@
// ignore-64bit
// FIXME https://github.com/rust-lang/rust/issues/59774
// normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> ""
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
#![allow(exceeding_bitshifts)]
fn main() {
let _fat: [u8; (1<<31)+(1<<15)] = //~ ERROR too big for the current architecture
[0; (1u32<<31) as usize +(1u32<<15) as usize];
}

View File

@ -0,0 +1,8 @@
error: the type `[u8; 2147516416]` is too big for the current architecture
--> $DIR/huge-array-simple-32.rs:9:9
|
LL | let _fat: [u8; (1<<31)+(1<<15)] =
| ^^^^
error: aborting due to previous error

View File

@ -0,0 +1,11 @@
// ignore-32bit
// FIXME https://github.com/rust-lang/rust/issues/59774
// normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> ""
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
#![allow(exceeding_bitshifts)]
fn main() {
let _fat: [u8; (1<<61)+(1<<31)] = //~ ERROR too big for the current architecture
[0; (1u64<<61) as usize +(1u64<<31) as usize];
}

View File

@ -0,0 +1,8 @@
error: the type `[u8; 2305843011361177600]` is too big for the current architecture
--> $DIR/huge-array-simple-64.rs:9:9
|
LL | let _fat: [u8; (1<<61)+(1<<31)] =
| ^^^^
error: aborting due to previous error

View File

@ -1,20 +0,0 @@
// error-pattern: too big for the current architecture
// normalize-stderr-test "; \d+]" -> "; N]"
// FIXME https://github.com/rust-lang/rust/issues/59774
// normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> ""
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
#![allow(exceeding_bitshifts)]
#[cfg(target_pointer_width = "64")]
fn main() {
let _fat : [u8; (1<<61)+(1<<31)] =
[0; (1u64<<61) as usize +(1u64<<31) as usize];
}
#[cfg(target_pointer_width = "32")]
fn main() {
let _fat : [u8; (1<<31)+(1<<15)] =
[0; (1u32<<31) as usize +(1u32<<15) as usize];
}

View File

@ -1,4 +0,0 @@
error: the type `[u8; N]` is too big for the current architecture
error: aborting due to previous error

View File

@ -1,11 +1,10 @@
// error-pattern:; 1518600000
// FIXME https://github.com/rust-lang/rust/issues/59774 // FIXME https://github.com/rust-lang/rust/issues/59774
// normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> "" // normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> ""
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> "" // normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
fn generic<T: Copy>(t: T) { fn generic<T: Copy>(t: T) {
let s: [T; 1518600000] = [t; 1518600000]; let s: [T; 1518600000] = [t; 1518600000];
//~^ ERROR the type `[[u8; 1518599999]; 1518600000]` is too big for the current architecture
} }
fn main() { fn main() {

View File

@ -1,4 +1,8 @@
error: the type `[[u8; 1518599999]; 1518600000]` is too big for the current architecture error: the type `[[u8; 1518599999]; 1518600000]` is too big for the current architecture
--> $DIR/huge-array.rs:6:9
|
LL | let s: [T; 1518600000] = [t; 1518600000];
| ^
error: aborting due to previous error error: aborting due to previous error

View File

@ -6,11 +6,12 @@
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> "" // normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
#[cfg(target_pointer_width = "32")] #[cfg(target_pointer_width = "32")]
fn main() { type BIG = Option<[u32; (1<<29)-1]>;
let big: Option<[u32; (1<<29)-1]> = None;
}
#[cfg(target_pointer_width = "64")] #[cfg(target_pointer_width = "64")]
type BIG = Option<[u32; (1<<45)-1]>;
fn main() { fn main() {
let big: Option<[u32; (1<<45)-1]> = None; let big: BIG = None;
//~^ ERROR is too big for the current architecture
} }

View File

@ -1,4 +1,8 @@
error: the type `TYPE` is too big for the current architecture error: the type `TYPE` is too big for the current architecture
--> $DIR/huge-enum.rs:15:9
|
LL | let big: BIG = None;
| ^^^
error: aborting due to previous error error: aborting due to previous error

View File

@ -47,4 +47,6 @@ struct S1M<T> { val: S1k<S1k<T>> }
fn main() { fn main() {
let fat: Option<S1M<S1M<S1M<u32>>>> = None; let fat: Option<S1M<S1M<S1M<u32>>>> = None;
//~^ ERROR the type `S32<S1M<S1M<u32>>>` is too big for the current architecture
} }

View File

@ -1,4 +1,8 @@
error: the type `SXX<SXX<SXX<u32>>>` is too big for the current architecture error: the type `SXX<SXX<SXX<u32>>>` is too big for the current architecture
--> $DIR/huge-struct.rs:49:9
|
LL | let fat: Option<SXX<SXX<SXX<u32>>>> = None;
| ^^^
error: aborting due to previous error error: aborting due to previous error

View File

@ -0,0 +1,12 @@
// ignore-64bit
// FIXME https://github.com/rust-lang/rust/issues/59774
// normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> ""
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
fn main() {
let x = [0usize; 0xffff_ffff]; //~ ERROR too big
}
// This and the -64 version of this test need to have different literals, as we can't rely on
// conditional compilation for them while retaining the same spans/lines.

View File

@ -0,0 +1,8 @@
error: the type `[usize; 4294967295]` is too big for the current architecture
--> $DIR/issue-15919-32.rs:8:9
|
LL | let x = [0usize; 0xffff_ffff];
| ^
error: aborting due to previous error

View File

@ -0,0 +1,12 @@
// ignore-32bit
// FIXME https://github.com/rust-lang/rust/issues/59774
// normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> ""
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
fn main() {
let x = [0usize; 0xffff_ffff_ffff_ffff]; //~ ERROR too big
}
// This and the -32 version of this test need to have different literals, as we can't rely on
// conditional compilation for them while retaining the same spans/lines.

View File

@ -0,0 +1,8 @@
error: the type `[usize; 18446744073709551615]` is too big for the current architecture
--> $DIR/issue-15919-64.rs:8:9
|
LL | let x = [0usize; 0xffff_ffff_ffff_ffff];
| ^
error: aborting due to previous error

View File

@ -1,16 +0,0 @@
// error-pattern: too big for the current architecture
// normalize-stderr-test "\[usize; \d+\]" -> "[usize; N]"
// FIXME https://github.com/rust-lang/rust/issues/59774
// normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> ""
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
#[cfg(target_pointer_width = "32")]
fn main() {
let x = [0usize; 0xffff_ffff];
}
#[cfg(target_pointer_width = "64")]
fn main() {
let x = [0usize; 0xffff_ffff_ffff_ffff];
}

View File

@ -1,4 +0,0 @@
error: the type `[usize; N]` is too big for the current architecture
error: aborting due to previous error

View File

@ -17,6 +17,8 @@ impl TooBigArray {
} }
static MY_TOO_BIG_ARRAY_1: TooBigArray = TooBigArray::new(); static MY_TOO_BIG_ARRAY_1: TooBigArray = TooBigArray::new();
//~^ ERROR the type `[u8; 2305843009213693951]` is too big for the current architecture
static MY_TOO_BIG_ARRAY_2: [u8; HUGE_SIZE] = [0x00; HUGE_SIZE]; static MY_TOO_BIG_ARRAY_2: [u8; HUGE_SIZE] = [0x00; HUGE_SIZE];
//~^ ERROR the type `[u8; 2305843009213693951]` is too big for the current architecture
fn main() { } fn main() { }

View File

@ -1,4 +1,15 @@
error: the type `[u8; 2305843009213693951]` is too big for the current architecture error[E0080]: the type `[u8; 2305843009213693951]` is too big for the current architecture
--> $DIR/issue-56762.rs:19:1
|
LL | static MY_TOO_BIG_ARRAY_1: TooBigArray = TooBigArray::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error error[E0080]: the type `[u8; 2305843009213693951]` is too big for the current architecture
--> $DIR/issue-56762.rs:21:1
|
LL | static MY_TOO_BIG_ARRAY_2: [u8; HUGE_SIZE] = [0x00; HUGE_SIZE];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0080`.