1 use super::{CheckInAllocMsg, Pointer, RawConst, ScalarMaybeUndef};
3 use crate::hir::map::definitions::DefPathData;
5 use crate::ty::layout::{Align, LayoutError, Size};
6 use crate::ty::query::TyCtxtAt;
7 use crate::ty::{self, layout, Ty};
9 use backtrace::Backtrace;
10 use errors::{struct_span_err, DiagnosticBuilder};
11 use hir::GeneratorKind;
13 use rustc_macros::HashStable;
14 use rustc_span::symbol::Symbol;
15 use rustc_span::{Pos, Span};
16 use rustc_target::spec::abi::Abi;
17 use std::{any::Any, env, fmt};
19 use rustc_error_codes::*;
21 #[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, RustcEncodable, RustcDecodable)]
22 pub enum ErrorHandled {
23 /// Already reported a lint or an error for this evaluation.
25 /// Don't emit an error, the evaluation failed because the MIR was generic
26 /// and the substs didn't fully monomorphize it.
31 pub fn assert_reported(self) {
33 ErrorHandled::Reported => {}
34 ErrorHandled::TooGeneric => bug!(
35 "MIR interpretation failed without reporting an error \
36 even though it was fully monomorphized"
42 CloneTypeFoldableImpls! {
46 pub type ConstEvalRawResult<'tcx> = Result<RawConst<'tcx>, ErrorHandled>;
47 pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
50 pub struct ConstEvalErr<'tcx> {
52 pub error: crate::mir::interpret::InterpError<'tcx>,
53 pub stacktrace: Vec<FrameInfo<'tcx>>,
57 pub struct FrameInfo<'tcx> {
58 /// This span is in the caller.
60 pub instance: ty::Instance<'tcx>,
61 pub lint_root: Option<hir::HirId>,
64 impl<'tcx> fmt::Display for FrameInfo<'tcx> {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 if tcx.def_key(self.instance.def_id()).disambiguated_data.data
68 == DefPathData::ClosureExpr
70 write!(f, "inside call to closure")?;
72 write!(f, "inside call to `{}`", self.instance)?;
74 if !self.call_site.is_dummy() {
75 let lo = tcx.sess.source_map().lookup_char_pos(self.call_site.lo());
76 write!(f, " at {}:{}:{}", lo.file.name, lo.line, lo.col.to_usize() + 1)?;
83 impl<'tcx> ConstEvalErr<'tcx> {
88 ) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> {
89 self.struct_generic(tcx, message, None)
92 pub fn report_as_error(&self, tcx: TyCtxtAt<'tcx>, message: &str) -> ErrorHandled {
93 let err = self.struct_error(tcx, message);
97 ErrorHandled::Reported
103 pub fn report_as_lint(
107 lint_root: hir::HirId,
110 let lint = self.struct_generic(tcx, message, Some(lint_root));
113 if let Some(span) = span {
114 let primary_spans = lint.span.primary_spans().to_vec();
115 // point at the actual error as the primary span
116 lint.replace_span_with(span);
117 // point to the `const` statement as a secondary span
118 // they don't have any label
119 for sp in primary_spans {
121 lint.span_label(sp, "");
126 ErrorHandled::Reported
136 lint_root: Option<hir::HirId>,
137 ) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> {
138 let must_error = match self.error {
139 InterpError::MachineStop(_) => bug!("CTFE does not stop"),
140 err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => {
141 return Err(ErrorHandled::TooGeneric);
143 err_inval!(TypeckError) => return Err(ErrorHandled::Reported),
144 err_inval!(Layout(LayoutError::SizeOverflow(_))) => true,
147 trace!("reporting const eval failure at {:?}", self.span);
148 let mut err = if let (Some(lint_root), false) = (lint_root, must_error) {
153 .filter_map(|frame| frame.lint_root)
155 .unwrap_or(lint_root);
156 tcx.struct_span_lint_hir(
157 crate::rustc::lint::builtin::CONST_ERR,
162 } else if must_error {
163 struct_error(tcx, &self.error.to_string())
165 struct_error(tcx, message)
168 err.span_label(self.span, self.error.to_string());
170 // Skip the last, which is just the environment of the constant. The stacktrace
171 // is sometimes empty because we create "fake" eval contexts in CTFE to do work
172 // on constant values.
173 if self.stacktrace.len() > 0 {
174 for frame_info in &self.stacktrace[..self.stacktrace.len() - 1] {
175 err.span_label(frame_info.call_site, frame_info.to_string());
182 pub fn struct_error<'tcx>(tcx: TyCtxtAt<'tcx>, msg: &str) -> DiagnosticBuilder<'tcx> {
183 struct_span_err!(tcx.sess, tcx.span, E0080, "{}", msg)
186 /// Packages the kind of error we got from the const code interpreter
187 /// up with a Rust-level backtrace of where the error occurred.
188 /// Thsese should always be constructed by calling `.into()` on
189 /// a `InterpError`. In `librustc_mir::interpret`, we have `throw_err_*`
192 pub struct InterpErrorInfo<'tcx> {
193 pub kind: InterpError<'tcx>,
194 backtrace: Option<Box<Backtrace>>,
197 impl fmt::Display for InterpErrorInfo<'_> {
198 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199 write!(f, "{}", self.kind)
203 impl InterpErrorInfo<'_> {
204 pub fn print_backtrace(&mut self) {
205 if let Some(ref mut backtrace) = self.backtrace {
206 print_backtrace(&mut *backtrace);
211 fn print_backtrace(backtrace: &mut Backtrace) {
213 eprintln!("\n\nAn error occurred in miri:\n{:?}", backtrace);
216 impl From<ErrorHandled> for InterpErrorInfo<'tcx> {
217 fn from(err: ErrorHandled) -> Self {
219 ErrorHandled::Reported => err_inval!(ReferencedConstant),
220 ErrorHandled::TooGeneric => err_inval!(TooGeneric),
226 impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> {
227 fn from(kind: InterpError<'tcx>) -> Self {
228 let backtrace = match env::var("RUSTC_CTFE_BACKTRACE") {
229 // Matching `RUST_BACKTRACE` -- we treat "0" the same as "not present".
230 Ok(ref val) if val != "0" => {
231 let mut backtrace = Backtrace::new_unresolved();
233 if val == "immediate" {
235 print_backtrace(&mut backtrace);
238 Some(Box::new(backtrace))
243 InterpErrorInfo { kind, backtrace }
247 #[derive(Clone, RustcEncodable, RustcDecodable, HashStable, PartialEq)]
248 pub enum PanicInfo<O> {
249 Panic { msg: Symbol, line: u32, col: u32, file: Symbol },
250 BoundsCheck { len: O, index: O },
251 Overflow(mir::BinOp),
255 ResumedAfterReturn(GeneratorKind),
256 ResumedAfterPanic(GeneratorKind),
259 /// Type for MIR `Assert` terminator error messages.
260 pub type AssertMessage<'tcx> = PanicInfo<mir::Operand<'tcx>>;
262 impl<O> PanicInfo<O> {
263 /// Getting a description does not require `O` to be printable, and does not
264 /// require allocation.
265 /// The caller is expected to handle `Panic` and `BoundsCheck` separately.
266 pub fn description(&self) -> &'static str {
269 Overflow(mir::BinOp::Add) => "attempt to add with overflow",
270 Overflow(mir::BinOp::Sub) => "attempt to subtract with overflow",
271 Overflow(mir::BinOp::Mul) => "attempt to multiply with overflow",
272 Overflow(mir::BinOp::Div) => "attempt to divide with overflow",
273 Overflow(mir::BinOp::Rem) => "attempt to calculate the remainder with overflow",
274 OverflowNeg => "attempt to negate with overflow",
275 Overflow(mir::BinOp::Shr) => "attempt to shift right with overflow",
276 Overflow(mir::BinOp::Shl) => "attempt to shift left with overflow",
277 Overflow(op) => bug!("{:?} cannot overflow", op),
278 DivisionByZero => "attempt to divide by zero",
279 RemainderByZero => "attempt to calculate the remainder with a divisor of zero",
280 ResumedAfterReturn(GeneratorKind::Gen) => "generator resumed after completion",
281 ResumedAfterReturn(GeneratorKind::Async(_)) => "`async fn` resumed after completion",
282 ResumedAfterPanic(GeneratorKind::Gen) => "generator resumed after panicking",
283 ResumedAfterPanic(GeneratorKind::Async(_)) => "`async fn` resumed after panicking",
284 Panic { .. } | BoundsCheck { .. } => bug!("Unexpected PanicInfo"),
289 impl<O: fmt::Debug> fmt::Debug for PanicInfo<O> {
290 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
293 Panic { ref msg, line, col, ref file } => {
294 write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col)
296 BoundsCheck { ref len, ref index } => {
297 write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index)
299 _ => write!(f, "{}", self.description()),
304 /// Error information for when the program we executed turned out not to actually be a valid
305 /// program. This cannot happen in stand-alone Miri, but it can happen during CTFE/ConstProp
306 /// where we work on generic code or execution does not have all information available.
307 pub enum InvalidProgramInfo<'tcx> {
308 /// Resolution can fail if we are in a too generic context.
310 /// Cannot compute this constant because it depends on another one
311 /// which already produced an error.
313 /// Abort in case type errors are reached.
315 /// An error occurred during layout computation.
316 Layout(layout::LayoutError<'tcx>),
319 impl fmt::Debug for InvalidProgramInfo<'tcx> {
320 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
321 use InvalidProgramInfo::*;
323 TooGeneric => write!(f, "encountered overly generic constant"),
324 ReferencedConstant => write!(f, "referenced constant has errors"),
325 TypeckError => write!(f, "encountered constants with type errors, stopping evaluation"),
326 Layout(ref err) => write!(f, "{}", err),
331 /// Error information for when the program caused Undefined Behavior.
332 pub enum UndefinedBehaviorInfo {
333 /// Free-form case. Only for errors that are never caught!
335 /// Free-form case for experimental UB. Only for errors that are never caught!
336 UbExperimental(String),
337 /// Unreachable code was executed.
339 /// An enum discriminant was set to a value which was outside the range of valid values.
340 InvalidDiscriminant(ScalarMaybeUndef),
341 /// A slice/array index projection went out-of-bounds.
342 BoundsCheckFailed { len: u64, index: u64 },
343 /// Something was divided by 0 (x / 0).
345 /// Something was "remainded" by 0 (x % 0).
347 /// Overflowing inbounds pointer arithmetic.
348 PointerArithOverflow,
351 impl fmt::Debug for UndefinedBehaviorInfo {
352 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
353 use UndefinedBehaviorInfo::*;
355 Ub(msg) | UbExperimental(msg) => write!(f, "{}", msg),
356 Unreachable => write!(f, "entering unreachable code"),
357 InvalidDiscriminant(val) => write!(f, "encountering invalid enum discriminant {}", val),
358 BoundsCheckFailed { ref len, ref index } => write!(
360 "indexing out of bounds: the len is {:?} but the index is {:?}",
363 DivisionByZero => write!(f, "dividing by zero"),
364 RemainderByZero => write!(f, "calculating the remainder with a divisor of zero"),
365 PointerArithOverflow => write!(f, "overflowing in-bounds pointer arithmetic"),
370 /// Error information for when the program did something that might (or might not) be correct
371 /// to do according to the Rust spec, but due to limitations in the interpreter, the
372 /// operation could not be carried out. These limitations can differ between CTFE and the
373 /// Miri engine, e.g., CTFE does not support casting pointers to "real" integers.
375 /// Currently, we also use this as fall-back error kind for errors that have not been
377 pub enum UnsupportedOpInfo<'tcx> {
378 /// Free-form case. Only for errors that are never caught!
381 /// When const-prop encounters a situation it does not support, it raises this error.
382 /// This must not allocate for performance reasons.
383 ConstPropUnsupported(&'tcx str),
385 // -- Everything below is not categorized yet --
386 FunctionAbiMismatch(Abi, Abi),
387 FunctionArgMismatch(Ty<'tcx>, Ty<'tcx>),
388 FunctionRetMismatch(Ty<'tcx>, Ty<'tcx>),
389 FunctionArgCountMismatch,
390 UnterminatedCString(Pointer),
391 DanglingPointerDeref,
394 InvalidFunctionPointer,
398 msg: CheckInAllocMsg,
399 allocation_size: Size,
401 InvalidNullPointerUsage,
406 ReadUndefBytes(Size),
408 InvalidBoolOp(mir::BinOp),
409 UnimplementedTraitSelection,
410 CalledClosureAsFunction,
412 DerefFunctionPointer,
417 AlignmentCheckFailed {
421 ValidationFailure(String),
422 VtableForArgumentlessMethod,
423 ModifiedConstantMemory,
425 TypeNotPrimitive(Ty<'tcx>),
426 ReallocatedWrongMemoryKind(String, String),
427 DeallocatedWrongMemoryKind(String, String),
428 ReallocateNonBasePtr,
429 DeallocateNonBasePtr,
430 IncorrectAllocationInformation(Size, Size, Align, Align),
432 HeapAllocNonPowerOfTwoAlignment(u64),
433 ReadFromReturnPointer,
434 PathNotFound(Vec<String>),
435 TransmuteSizeDiff(Ty<'tcx>, Ty<'tcx>),
438 impl fmt::Debug for UnsupportedOpInfo<'tcx> {
439 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
440 use UnsupportedOpInfo::*;
442 PointerOutOfBounds { ptr, msg, allocation_size } => write!(
444 "{} failed: pointer must be in-bounds at offset {}, \
445 but is outside bounds of allocation {} which has size {}",
449 allocation_size.bytes()
451 ValidationFailure(ref err) => write!(f, "type validation failed: {}", err),
452 NoMirFor(ref func) => write!(f, "no MIR for `{}`", func),
453 FunctionAbiMismatch(caller_abi, callee_abi) => write!(
455 "tried to call a function with ABI {:?} using caller ABI {:?}",
456 callee_abi, caller_abi
458 FunctionArgMismatch(caller_ty, callee_ty) => write!(
460 "tried to call a function with argument of type {:?} \
461 passing data of type {:?}",
464 TransmuteSizeDiff(from_ty, to_ty) => write!(
466 "tried to transmute from {:?} to {:?}, but their sizes differed",
469 FunctionRetMismatch(caller_ty, callee_ty) => write!(
471 "tried to call a function with return type {:?} \
472 passing return place of type {:?}",
475 FunctionArgCountMismatch => {
476 write!(f, "tried to call a function with incorrect number of arguments")
478 ReallocatedWrongMemoryKind(ref old, ref new) => {
479 write!(f, "tried to reallocate memory from `{}` to `{}`", old, new)
481 DeallocatedWrongMemoryKind(ref old, ref new) => {
482 write!(f, "tried to deallocate `{}` memory but gave `{}` as the kind", old, new)
485 write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c)
487 AlignmentCheckFailed { required, has } => write!(
489 "tried to access memory with alignment {}, but alignment {} is required",
493 TypeNotPrimitive(ty) => write!(f, "expected primitive type, got {}", ty),
494 PathNotFound(ref path) => write!(f, "cannot find path {:?}", path),
495 IncorrectAllocationInformation(size, size2, align, align2) => write!(
497 "incorrect alloc info: expected size {} and align {}, \
498 got size {} and align {}",
504 InvalidMemoryAccess => write!(f, "tried to access memory through an invalid pointer"),
505 DanglingPointerDeref => write!(f, "dangling pointer was dereferenced"),
506 DoubleFree => write!(f, "tried to deallocate dangling pointer"),
507 InvalidFunctionPointer => {
508 write!(f, "tried to use a function pointer after offsetting it")
510 InvalidBool => write!(f, "invalid boolean value read"),
511 InvalidNullPointerUsage => write!(f, "invalid use of NULL pointer"),
512 ReadPointerAsBytes => write!(
514 "a raw memory access tried to access part of a pointer value as raw \
517 ReadBytesAsPointer => {
518 write!(f, "a memory access tried to interpret some bytes as a pointer")
520 ReadForeignStatic => write!(f, "tried to read from foreign (extern) static"),
521 InvalidPointerMath => write!(
523 "attempted to do invalid arithmetic on pointers that would leak base \
524 addresses, e.g., comparing pointers into different allocations"
526 DeadLocal => write!(f, "tried to access a dead local variable"),
527 DerefFunctionPointer => write!(f, "tried to dereference a function pointer"),
528 ExecuteMemory => write!(f, "tried to treat a memory pointer as a function pointer"),
529 OutOfTls => write!(f, "reached the maximum number of representable TLS keys"),
530 TlsOutOfBounds => write!(f, "accessed an invalid (unallocated) TLS key"),
531 CalledClosureAsFunction => {
532 write!(f, "tried to call a closure through a function pointer")
534 VtableForArgumentlessMethod => {
535 write!(f, "tried to call a vtable function without arguments")
537 ModifiedConstantMemory => write!(f, "tried to modify constant memory"),
538 ModifiedStatic => write!(
540 "tried to modify a static's initial value from another static's \
543 ReallocateNonBasePtr => write!(
545 "tried to reallocate with a pointer not to the beginning of an \
548 DeallocateNonBasePtr => write!(
550 "tried to deallocate with a pointer not to the beginning of an \
553 HeapAllocZeroBytes => write!(f, "tried to re-, de- or allocate zero bytes on the heap"),
554 ReadFromReturnPointer => write!(f, "tried to read from the return pointer"),
555 UnimplementedTraitSelection => {
556 write!(f, "there were unresolved type arguments during trait selection")
558 InvalidBoolOp(_) => write!(f, "invalid boolean operation"),
559 UnterminatedCString(_) => write!(
561 "attempted to get length of a null-terminated string, but no null \
562 found before end of allocation"
564 ReadUndefBytes(_) => write!(f, "attempted to read undefined bytes"),
565 HeapAllocNonPowerOfTwoAlignment(_) => write!(
567 "tried to re-, de-, or allocate heap memory with alignment that is \
570 Unsupported(ref msg) => write!(f, "{}", msg),
571 ConstPropUnsupported(ref msg) => {
572 write!(f, "Constant propagation encountered an unsupported situation: {}", msg)
578 /// Error information for when the program exhausted the resources granted to it
579 /// by the interpreter.
580 pub enum ResourceExhaustionInfo {
581 /// The stack grew too big.
582 StackFrameLimitReached,
583 /// The program ran into an infinite loop.
587 impl fmt::Debug for ResourceExhaustionInfo {
588 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
589 use ResourceExhaustionInfo::*;
591 StackFrameLimitReached => {
592 write!(f, "reached the configured maximum number of stack frames")
594 InfiniteLoop => write!(
596 "duplicate interpreter state observed here, const evaluation will never \
603 pub enum InterpError<'tcx> {
604 /// The program panicked.
605 Panic(PanicInfo<u64>),
606 /// The program caused undefined behavior.
607 UndefinedBehavior(UndefinedBehaviorInfo),
608 /// The program did something the interpreter does not support (some of these *might* be UB
609 /// but the interpreter is not sure).
610 Unsupported(UnsupportedOpInfo<'tcx>),
611 /// The program was invalid (ill-typed, bad MIR, not sufficiently monomorphized, ...).
612 InvalidProgram(InvalidProgramInfo<'tcx>),
613 /// The program exhausted the interpreter's resources (stack/heap too big,
614 /// execution takes too long, ...).
615 ResourceExhaustion(ResourceExhaustionInfo),
616 /// Stop execution for a machine-controlled reason. This is never raised by
617 /// the core engine itself.
618 MachineStop(Box<dyn Any + Send>),
621 pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
623 impl fmt::Display for InterpError<'_> {
624 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
625 // Forward `Display` to `Debug`.
626 write!(f, "{:?}", self)
630 impl fmt::Debug for InterpError<'_> {
631 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
634 Unsupported(ref msg) => write!(f, "{:?}", msg),
635 InvalidProgram(ref msg) => write!(f, "{:?}", msg),
636 UndefinedBehavior(ref msg) => write!(f, "{:?}", msg),
637 ResourceExhaustion(ref msg) => write!(f, "{:?}", msg),
638 Panic(ref msg) => write!(f, "{:?}", msg),
639 MachineStop(_) => write!(f, "machine caused execution to stop"),