1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use hir::map::definitions::DefPathData;
15 use ty::{self, Ty, layout};
16 use ty::layout::{Size, Align, LayoutError};
17 use rustc_target::spec::abi::Abi;
19 use super::{RawConst, Pointer, InboundsCheck, ScalarMaybeUndef};
21 use backtrace::Backtrace;
23 use ty::query::TyCtxtAt;
24 use errors::DiagnosticBuilder;
26 use syntax_pos::{Pos, Span};
28 use syntax::symbol::Symbol;
30 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
31 pub enum ErrorHandled {
32 /// Already reported a lint or an error for this evaluation
34 /// Don't emit an error, the evaluation failed because the MIR was generic
35 /// and the substs didn't fully monomorphize it.
40 pub fn assert_reported(self) {
42 ErrorHandled::Reported => {},
43 ErrorHandled::TooGeneric => bug!("MIR interpretation failed without reporting an error \
44 even though it was fully monomorphized"),
49 pub type ConstEvalRawResult<'tcx> = Result<RawConst<'tcx>, ErrorHandled>;
50 pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
52 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
53 pub struct ConstEvalErr<'tcx> {
55 pub error: ::mir::interpret::EvalErrorKind<'tcx, u64>,
56 pub stacktrace: Vec<FrameInfo<'tcx>>,
59 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
60 pub struct FrameInfo<'tcx> {
61 pub call_site: Span, // this span is in the caller!
62 pub instance: ty::Instance<'tcx>,
63 pub lint_root: Option<ast::NodeId>,
66 impl<'tcx> fmt::Display for FrameInfo<'tcx> {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 if tcx.def_key(self.instance.def_id()).disambiguated_data.data
70 == DefPathData::ClosureExpr
72 write!(f, "inside call to closure")?;
74 write!(f, "inside call to `{}`", self.instance)?;
76 if !self.call_site.is_dummy() {
77 let lo = tcx.sess.source_map().lookup_char_pos_adj(self.call_site.lo());
78 write!(f, " at {}:{}:{}", lo.filename, lo.line, lo.col.to_usize() + 1)?;
85 impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
86 pub fn struct_error(&self,
87 tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
89 -> Result<DiagnosticBuilder<'tcx>, ErrorHandled>
91 self.struct_generic(tcx, message, None)
94 pub fn report_as_error(&self,
95 tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
98 let err = self.struct_error(tcx, message);
102 ErrorHandled::Reported
108 pub fn report_as_lint(&self,
109 tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
111 lint_root: ast::NodeId,
113 let lint = self.struct_generic(
121 ErrorHandled::Reported
129 tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
131 lint_root: Option<ast::NodeId>,
132 ) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> {
134 EvalErrorKind::Layout(LayoutError::Unknown(_)) |
135 EvalErrorKind::TooGeneric => return Err(ErrorHandled::TooGeneric),
136 EvalErrorKind::Layout(LayoutError::SizeOverflow(_)) |
137 EvalErrorKind::TypeckError => return Err(ErrorHandled::Reported),
140 trace!("reporting const eval failure at {:?}", self.span);
141 let mut err = if let Some(lint_root) = lint_root {
142 let node_id = self.stacktrace
145 .filter_map(|frame| frame.lint_root)
147 .unwrap_or(lint_root);
148 tcx.struct_span_lint_node(
149 ::rustc::lint::builtin::CONST_ERR,
155 struct_error(tcx, message)
157 err.span_label(self.span, self.error.to_string());
158 // Skip the last, which is just the environment of the constant. The stacktrace
159 // is sometimes empty because we create "fake" eval contexts in CTFE to do work
160 // on constant values.
161 if self.stacktrace.len() > 0 {
162 for frame_info in &self.stacktrace[..self.stacktrace.len()-1] {
163 err.span_label(frame_info.call_site, frame_info.to_string());
170 pub fn struct_error<'a, 'gcx, 'tcx>(
171 tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
173 ) -> DiagnosticBuilder<'tcx> {
174 struct_span_err!(tcx.sess, tcx.span, E0080, "{}", msg)
177 #[derive(Debug, Clone)]
178 pub struct EvalError<'tcx> {
179 pub kind: EvalErrorKind<'tcx, u64>,
180 pub backtrace: Option<Box<Backtrace>>,
183 impl<'tcx> EvalError<'tcx> {
184 pub fn print_backtrace(&mut self) {
185 if let Some(ref mut backtrace) = self.backtrace {
186 eprintln!("{}", print_backtrace(&mut *backtrace));
191 fn print_backtrace(backtrace: &mut Backtrace) -> String {
196 let mut trace_text = "\n\nAn error occurred in miri:\n".to_string();
197 write!(trace_text, "backtrace frames: {}\n", backtrace.frames().len()).unwrap();
198 'frames: for (i, frame) in backtrace.frames().iter().enumerate() {
199 if frame.symbols().is_empty() {
200 write!(trace_text, " {}: no symbols\n", i).unwrap();
202 let mut first = true;
203 for symbol in frame.symbols() {
205 write!(trace_text, " {}: ", i).unwrap();
208 let len = i.to_string().len();
209 write!(trace_text, " {} ", " ".repeat(len)).unwrap();
211 if let Some(name) = symbol.name() {
212 write!(trace_text, "{}\n", name).unwrap();
214 write!(trace_text, "<unknown>\n").unwrap();
216 write!(trace_text, " at ").unwrap();
217 if let Some(file_path) = symbol.filename() {
218 write!(trace_text, "{}", file_path.display()).unwrap();
220 write!(trace_text, "<unknown_file>").unwrap();
222 if let Some(line) = symbol.lineno() {
223 write!(trace_text, ":{}\n", line).unwrap();
225 write!(trace_text, "\n").unwrap();
232 impl<'tcx> From<EvalErrorKind<'tcx, u64>> for EvalError<'tcx> {
233 fn from(kind: EvalErrorKind<'tcx, u64>) -> Self {
234 let backtrace = match env::var("RUST_CTFE_BACKTRACE") {
235 // matching RUST_BACKTRACE, we treat "0" the same as "not present".
236 Ok(ref val) if val != "0" => {
237 let mut backtrace = Backtrace::new_unresolved();
239 if val == "immediate" {
241 eprintln!("{}", print_backtrace(&mut backtrace));
244 Some(Box::new(backtrace))
256 pub type AssertMessage<'tcx> = EvalErrorKind<'tcx, mir::Operand<'tcx>>;
258 #[derive(Clone, RustcEncodable, RustcDecodable)]
259 pub enum EvalErrorKind<'tcx, O> {
260 /// This variant is used by machines to signal their own errors that do not
261 /// match an existing variant
262 MachineError(String),
264 FunctionAbiMismatch(Abi, Abi),
265 FunctionArgMismatch(Ty<'tcx>, Ty<'tcx>),
266 FunctionRetMismatch(Ty<'tcx>, Ty<'tcx>),
267 FunctionArgCountMismatch,
269 UnterminatedCString(Pointer),
270 DanglingPointerDeref,
273 InvalidFunctionPointer,
275 InvalidDiscriminant(ScalarMaybeUndef),
278 check: InboundsCheck,
279 allocation_size: Size,
281 InvalidNullPointerUsage,
286 ReadUndefBytes(Size),
288 InvalidBoolOp(mir::BinOp),
289 Unimplemented(String),
290 DerefFunctionPointer,
292 BoundsCheck { len: O, index: O },
293 Overflow(mir::BinOp),
299 StackFrameLimitReached,
302 AbiViolation(String),
303 AlignmentCheckFailed {
307 ValidationFailure(String),
308 CalledClosureAsFunction,
309 VtableForArgumentlessMethod,
310 ModifiedConstantMemory,
314 TypeNotPrimitive(Ty<'tcx>),
315 ReallocatedWrongMemoryKind(String, String),
316 DeallocatedWrongMemoryKind(String, String),
317 ReallocateNonBasePtr,
318 DeallocateNonBasePtr,
319 IncorrectAllocationInformation(Size, Size, Align, Align),
320 Layout(layout::LayoutError<'tcx>),
322 HeapAllocNonPowerOfTwoAlignment(u64),
330 ReadFromReturnPointer,
331 PathNotFound(Vec<String>),
332 UnimplementedTraitSelection,
333 /// Abort in case type errors are reached
335 /// Resolution can fail if we are in a too generic context
337 /// Cannot compute this constant because it depends on another one
338 /// which already produced an error
340 GeneratorResumedAfterReturn,
341 GeneratorResumedAfterPanic,
345 pub type EvalResult<'tcx, T = ()> = Result<T, EvalError<'tcx>>;
347 impl<'tcx, O> EvalErrorKind<'tcx, O> {
348 pub fn description(&self) -> &str {
349 use self::EvalErrorKind::*;
351 MachineError(ref inner) => inner,
352 FunctionAbiMismatch(..) | FunctionArgMismatch(..) | FunctionRetMismatch(..)
353 | FunctionArgCountMismatch =>
354 "tried to call a function through a function pointer of incompatible type",
355 InvalidMemoryAccess =>
356 "tried to access memory through an invalid pointer",
357 DanglingPointerDeref =>
358 "dangling pointer was dereferenced",
360 "tried to deallocate dangling pointer",
361 InvalidFunctionPointer =>
362 "tried to use a function pointer after offsetting it",
364 "invalid boolean value read",
365 InvalidDiscriminant(..) =>
366 "invalid enum discriminant value read",
367 PointerOutOfBounds { .. } =>
368 "pointer offset outside bounds of allocation",
369 InvalidNullPointerUsage =>
370 "invalid use of NULL pointer",
371 ValidationFailure(..) =>
372 "type validation failed",
373 ReadPointerAsBytes =>
374 "a raw memory access tried to access part of a pointer value as raw bytes",
375 ReadBytesAsPointer =>
376 "a memory access tried to interpret some bytes as a pointer",
378 "tried to read from foreign (extern) static",
379 InvalidPointerMath =>
380 "attempted to do invalid arithmetic on pointers that would leak base addresses, \
381 e.g., comparing pointers into different allocations",
383 "attempted to read undefined bytes",
385 "tried to access a dead local variable",
387 "invalid boolean operation",
388 Unimplemented(ref msg) => msg,
389 DerefFunctionPointer =>
390 "tried to dereference a function pointer",
392 "tried to treat a memory pointer as a function pointer",
394 "array index out of bounds",
400 "tried to interpret an invalid 32-bit value as a char",
401 StackFrameLimitReached =>
402 "reached the configured maximum number of stack frames",
404 "reached the maximum number of representable TLS keys",
406 "accessed an invalid (unallocated) TLS key",
407 AbiViolation(ref msg) => msg,
408 AlignmentCheckFailed{..} =>
409 "tried to execute a misaligned read or write",
410 CalledClosureAsFunction =>
411 "tried to call a closure through a function pointer",
412 VtableForArgumentlessMethod =>
413 "tried to call a vtable function without arguments",
414 ModifiedConstantMemory =>
415 "tried to modify constant memory",
417 "tried to modify a static's initial value from another static's initializer",
419 "`assume` argument was false",
421 "miri does not support inline assembly",
422 TypeNotPrimitive(_) =>
423 "expected primitive type, got nonprimitive",
424 ReallocatedWrongMemoryKind(_, _) =>
425 "tried to reallocate memory from one kind to another",
426 DeallocatedWrongMemoryKind(_, _) =>
427 "tried to deallocate memory of the wrong kind",
428 ReallocateNonBasePtr =>
429 "tried to reallocate with a pointer not to the beginning of an existing object",
430 DeallocateNonBasePtr =>
431 "tried to deallocate with a pointer not to the beginning of an existing object",
432 IncorrectAllocationInformation(..) =>
433 "tried to deallocate or reallocate using incorrect alignment or size",
435 "rustc layout computation failed",
436 UnterminatedCString(_) =>
437 "attempted to get length of a null terminated string, but no null found before end \
439 HeapAllocZeroBytes =>
440 "tried to re-, de- or allocate zero bytes on the heap",
441 HeapAllocNonPowerOfTwoAlignment(_) =>
442 "tried to re-, de-, or allocate heap memory with alignment that is not a power of \
445 "entered unreachable code",
447 "the evaluated program panicked",
448 ReadFromReturnPointer =>
449 "tried to read from the return pointer",
451 "a path could not be resolved, maybe the crate is not loaded",
452 UnimplementedTraitSelection =>
453 "there were unresolved type arguments during trait selection",
455 "encountered constants with type errors, stopping evaluation",
457 "encountered overly generic constant",
458 ReferencedConstant =>
459 "referenced constant has errors",
460 Overflow(mir::BinOp::Add) => "attempt to add with overflow",
461 Overflow(mir::BinOp::Sub) => "attempt to subtract with overflow",
462 Overflow(mir::BinOp::Mul) => "attempt to multiply with overflow",
463 Overflow(mir::BinOp::Div) => "attempt to divide with overflow",
464 Overflow(mir::BinOp::Rem) => "attempt to calculate the remainder with overflow",
465 OverflowNeg => "attempt to negate with overflow",
466 Overflow(mir::BinOp::Shr) => "attempt to shift right with overflow",
467 Overflow(mir::BinOp::Shl) => "attempt to shift left with overflow",
468 Overflow(op) => bug!("{:?} cannot overflow", op),
469 DivisionByZero => "attempt to divide by zero",
470 RemainderByZero => "attempt to calculate the remainder with a divisor of zero",
471 GeneratorResumedAfterReturn => "generator resumed after completion",
472 GeneratorResumedAfterPanic => "generator resumed after panicking",
474 "duplicate interpreter state observed here, const evaluation will never terminate",
479 impl<'tcx> fmt::Display for EvalError<'tcx> {
480 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
481 write!(f, "{}", self.kind)
485 impl<'tcx> fmt::Display for EvalErrorKind<'tcx, u64> {
486 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
487 write!(f, "{:?}", self)
491 impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> {
492 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
493 use self::EvalErrorKind::*;
495 PointerOutOfBounds { ptr, check, allocation_size } => {
496 write!(f, "Pointer must be in-bounds{} at offset {}, but is outside bounds of \
497 allocation {} which has size {}",
499 InboundsCheck::Live => " and live",
500 InboundsCheck::MaybeDead => "",
502 ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
504 ValidationFailure(ref err) => {
505 write!(f, "type validation failed: {}", err)
507 NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
508 FunctionAbiMismatch(caller_abi, callee_abi) =>
509 write!(f, "tried to call a function with ABI {:?} using caller ABI {:?}",
510 callee_abi, caller_abi),
511 FunctionArgMismatch(caller_ty, callee_ty) =>
512 write!(f, "tried to call a function with argument of type {:?} \
513 passing data of type {:?}",
514 callee_ty, caller_ty),
515 FunctionRetMismatch(caller_ty, callee_ty) =>
516 write!(f, "tried to call a function with return type {:?} \
517 passing return place of type {:?}",
518 callee_ty, caller_ty),
519 FunctionArgCountMismatch =>
520 write!(f, "tried to call a function with incorrect number of arguments"),
521 BoundsCheck { ref len, ref index } =>
522 write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index),
523 ReallocatedWrongMemoryKind(ref old, ref new) =>
524 write!(f, "tried to reallocate memory from {} to {}", old, new),
525 DeallocatedWrongMemoryKind(ref old, ref new) =>
526 write!(f, "tried to deallocate {} memory but gave {} as the kind", old, new),
527 Intrinsic(ref err) =>
528 write!(f, "{}", err),
530 write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c),
531 AlignmentCheckFailed { required, has } =>
532 write!(f, "tried to access memory with alignment {}, but alignment {} is required",
533 has.bytes(), required.bytes()),
534 TypeNotPrimitive(ty) =>
535 write!(f, "expected primitive type, got {}", ty),
537 write!(f, "rustc layout computation failed: {:?}", err),
538 PathNotFound(ref path) =>
539 write!(f, "Cannot find path {:?}", path),
540 MachineError(ref inner) =>
541 write!(f, "{}", inner),
542 IncorrectAllocationInformation(size, size2, align, align2) =>
543 write!(f, "incorrect alloc info: expected size {} and align {}, \
544 got size {} and align {}",
545 size.bytes(), align.bytes(), size2.bytes(), align2.bytes()),
546 Panic { ref msg, line, col, ref file } =>
547 write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col),
548 InvalidDiscriminant(val) =>
549 write!(f, "encountered invalid enum discriminant {}", val),
550 _ => write!(f, "{}", self.description()),