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::{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 ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
51 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
52 pub struct ConstEvalErr<'tcx> {
54 pub error: ::mir::interpret::EvalErrorKind<'tcx, u64>,
55 pub stacktrace: Vec<FrameInfo<'tcx>>,
58 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
59 pub struct FrameInfo<'tcx> {
60 pub call_site: Span, // this span is in the caller!
61 pub instance: ty::Instance<'tcx>,
62 pub lint_root: Option<ast::NodeId>,
65 impl<'tcx> fmt::Display for FrameInfo<'tcx> {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 if tcx.def_key(self.instance.def_id()).disambiguated_data.data
69 == DefPathData::ClosureExpr
71 write!(f, "inside call to closure")?;
73 write!(f, "inside call to `{}`", self.instance)?;
75 if !self.call_site.is_dummy() {
76 let lo = tcx.sess.source_map().lookup_char_pos_adj(self.call_site.lo());
77 write!(f, " at {}:{}:{}", lo.filename, lo.line, lo.col.to_usize() + 1)?;
84 impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
85 pub fn struct_error(&self,
86 tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
88 -> Result<DiagnosticBuilder<'tcx>, ErrorHandled>
90 self.struct_generic(tcx, message, None)
93 pub fn report_as_error(&self,
94 tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
97 let err = self.struct_error(tcx, message);
101 ErrorHandled::Reported
107 pub fn report_as_lint(&self,
108 tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
110 lint_root: ast::NodeId,
112 let lint = self.struct_generic(
120 ErrorHandled::Reported
128 tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
130 lint_root: Option<ast::NodeId>,
131 ) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> {
133 EvalErrorKind::Layout(LayoutError::Unknown(_)) |
134 EvalErrorKind::TooGeneric => return Err(ErrorHandled::TooGeneric),
135 EvalErrorKind::Layout(LayoutError::SizeOverflow(_)) |
136 EvalErrorKind::TypeckError => return Err(ErrorHandled::Reported),
139 trace!("reporting const eval failure at {:?}", self.span);
140 let mut err = if let Some(lint_root) = lint_root {
141 let node_id = self.stacktrace
144 .filter_map(|frame| frame.lint_root)
146 .unwrap_or(lint_root);
147 tcx.struct_span_lint_node(
148 ::rustc::lint::builtin::CONST_ERR,
154 struct_error(tcx, message)
156 err.span_label(self.span, self.error.to_string());
157 // Skip the last, which is just the environment of the constant. The stacktrace
158 // is sometimes empty because we create "fake" eval contexts in CTFE to do work
159 // on constant values.
160 if self.stacktrace.len() > 0 {
161 for frame_info in &self.stacktrace[..self.stacktrace.len()-1] {
162 err.span_label(frame_info.call_site, frame_info.to_string());
169 pub fn struct_error<'a, 'gcx, 'tcx>(
170 tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
172 ) -> DiagnosticBuilder<'tcx> {
173 struct_span_err!(tcx.sess, tcx.span, E0080, "{}", msg)
176 #[derive(Debug, Clone)]
177 pub struct EvalError<'tcx> {
178 pub kind: EvalErrorKind<'tcx, u64>,
179 pub backtrace: Option<Box<Backtrace>>,
182 impl<'tcx> EvalError<'tcx> {
183 pub fn print_backtrace(&mut self) {
184 if let Some(ref mut backtrace) = self.backtrace {
185 eprintln!("{}", print_backtrace(&mut *backtrace));
190 fn print_backtrace(backtrace: &mut Backtrace) -> String {
195 let mut trace_text = "\n\nAn error occurred in miri:\n".to_string();
196 write!(trace_text, "backtrace frames: {}\n", backtrace.frames().len()).unwrap();
197 'frames: for (i, frame) in backtrace.frames().iter().enumerate() {
198 if frame.symbols().is_empty() {
199 write!(trace_text, " {}: no symbols\n", i).unwrap();
201 let mut first = true;
202 for symbol in frame.symbols() {
204 write!(trace_text, " {}: ", i).unwrap();
207 let len = i.to_string().len();
208 write!(trace_text, " {} ", " ".repeat(len)).unwrap();
210 if let Some(name) = symbol.name() {
211 write!(trace_text, "{}\n", name).unwrap();
213 write!(trace_text, "<unknown>\n").unwrap();
215 write!(trace_text, " at ").unwrap();
216 if let Some(file_path) = symbol.filename() {
217 write!(trace_text, "{}", file_path.display()).unwrap();
219 write!(trace_text, "<unknown_file>").unwrap();
221 if let Some(line) = symbol.lineno() {
222 write!(trace_text, ":{}\n", line).unwrap();
224 write!(trace_text, "\n").unwrap();
231 impl<'tcx> From<EvalErrorKind<'tcx, u64>> for EvalError<'tcx> {
232 fn from(kind: EvalErrorKind<'tcx, u64>) -> Self {
233 let backtrace = match env::var("RUST_CTFE_BACKTRACE") {
234 // matching RUST_BACKTRACE, we treat "0" the same as "not present".
235 Ok(ref val) if val != "0" => {
236 let mut backtrace = Backtrace::new_unresolved();
238 if val == "immediate" {
240 eprintln!("{}", print_backtrace(&mut backtrace));
243 Some(Box::new(backtrace))
255 pub type AssertMessage<'tcx> = EvalErrorKind<'tcx, mir::Operand<'tcx>>;
257 #[derive(Clone, RustcEncodable, RustcDecodable)]
258 pub enum EvalErrorKind<'tcx, O> {
259 /// This variant is used by machines to signal their own errors that do not
260 /// match an existing variant
261 MachineError(String),
263 FunctionAbiMismatch(Abi, Abi),
264 FunctionArgMismatch(Ty<'tcx>, Ty<'tcx>),
265 FunctionRetMismatch(Ty<'tcx>, Ty<'tcx>),
266 FunctionArgCountMismatch,
268 UnterminatedCString(Pointer),
269 DanglingPointerDeref,
272 InvalidFunctionPointer,
274 InvalidDiscriminant(ScalarMaybeUndef),
277 check: InboundsCheck,
278 allocation_size: Size,
280 InvalidNullPointerUsage,
285 ReadUndefBytes(Size),
287 InvalidBoolOp(mir::BinOp),
288 Unimplemented(String),
289 DerefFunctionPointer,
291 BoundsCheck { len: O, index: O },
292 Overflow(mir::BinOp),
298 StackFrameLimitReached,
301 AbiViolation(String),
302 AlignmentCheckFailed {
306 ValidationFailure(String),
307 CalledClosureAsFunction,
308 VtableForArgumentlessMethod,
309 ModifiedConstantMemory,
312 TypeNotPrimitive(Ty<'tcx>),
313 ReallocatedWrongMemoryKind(String, String),
314 DeallocatedWrongMemoryKind(String, String),
315 ReallocateNonBasePtr,
316 DeallocateNonBasePtr,
317 IncorrectAllocationInformation(Size, Size, Align, Align),
318 Layout(layout::LayoutError<'tcx>),
320 HeapAllocNonPowerOfTwoAlignment(u64),
328 ReadFromReturnPointer,
329 PathNotFound(Vec<String>),
330 UnimplementedTraitSelection,
331 /// Abort in case type errors are reached
333 /// Resolution can fail if we are in a too generic context
335 /// Cannot compute this constant because it depends on another one
336 /// which already produced an error
338 GeneratorResumedAfterReturn,
339 GeneratorResumedAfterPanic,
343 pub type EvalResult<'tcx, T = ()> = Result<T, EvalError<'tcx>>;
345 impl<'tcx, O> EvalErrorKind<'tcx, O> {
346 pub fn description(&self) -> &str {
347 use self::EvalErrorKind::*;
349 MachineError(ref inner) => inner,
350 FunctionAbiMismatch(..) | FunctionArgMismatch(..) | FunctionRetMismatch(..)
351 | FunctionArgCountMismatch =>
352 "tried to call a function through a function pointer of incompatible type",
353 InvalidMemoryAccess =>
354 "tried to access memory through an invalid pointer",
355 DanglingPointerDeref =>
356 "dangling pointer was dereferenced",
358 "tried to deallocate dangling pointer",
359 InvalidFunctionPointer =>
360 "tried to use a function pointer after offsetting it",
362 "invalid boolean value read",
363 InvalidDiscriminant(..) =>
364 "invalid enum discriminant value read",
365 PointerOutOfBounds { .. } =>
366 "pointer offset outside bounds of allocation",
367 InvalidNullPointerUsage =>
368 "invalid use of NULL pointer",
369 ValidationFailure(..) =>
370 "type validation failed",
371 ReadPointerAsBytes =>
372 "a raw memory access tried to access part of a pointer value as raw bytes",
373 ReadBytesAsPointer =>
374 "a memory access tried to interpret some bytes as a pointer",
376 "tried to read from foreign (extern) static",
377 InvalidPointerMath =>
378 "attempted to do invalid arithmetic on pointers that would leak base addresses, \
379 e.g. comparing pointers into different allocations",
381 "attempted to read undefined bytes",
383 "tried to access a dead local variable",
385 "invalid boolean operation",
386 Unimplemented(ref msg) => msg,
387 DerefFunctionPointer =>
388 "tried to dereference a function pointer",
390 "tried to treat a memory pointer as a function pointer",
392 "array index out of bounds",
398 "tried to interpret an invalid 32-bit value as a char",
399 StackFrameLimitReached =>
400 "reached the configured maximum number of stack frames",
402 "reached the maximum number of representable TLS keys",
404 "accessed an invalid (unallocated) TLS key",
405 AbiViolation(ref msg) => msg,
406 AlignmentCheckFailed{..} =>
407 "tried to execute a misaligned read or write",
408 CalledClosureAsFunction =>
409 "tried to call a closure through a function pointer",
410 VtableForArgumentlessMethod =>
411 "tried to call a vtable function without arguments",
412 ModifiedConstantMemory =>
413 "tried to modify constant memory",
415 "`assume` argument was false",
417 "miri does not support inline assembly",
418 TypeNotPrimitive(_) =>
419 "expected primitive type, got nonprimitive",
420 ReallocatedWrongMemoryKind(_, _) =>
421 "tried to reallocate memory from one kind to another",
422 DeallocatedWrongMemoryKind(_, _) =>
423 "tried to deallocate memory of the wrong kind",
424 ReallocateNonBasePtr =>
425 "tried to reallocate with a pointer not to the beginning of an existing object",
426 DeallocateNonBasePtr =>
427 "tried to deallocate with a pointer not to the beginning of an existing object",
428 IncorrectAllocationInformation(..) =>
429 "tried to deallocate or reallocate using incorrect alignment or size",
431 "rustc layout computation failed",
432 UnterminatedCString(_) =>
433 "attempted to get length of a null terminated string, but no null found before end \
435 HeapAllocZeroBytes =>
436 "tried to re-, de- or allocate zero bytes on the heap",
437 HeapAllocNonPowerOfTwoAlignment(_) =>
438 "tried to re-, de-, or allocate heap memory with alignment that is not a power of \
441 "entered unreachable code",
443 "the evaluated program panicked",
444 ReadFromReturnPointer =>
445 "tried to read from the return pointer",
447 "a path could not be resolved, maybe the crate is not loaded",
448 UnimplementedTraitSelection =>
449 "there were unresolved type arguments during trait selection",
451 "encountered constants with type errors, stopping evaluation",
453 "encountered overly generic constant",
454 ReferencedConstant =>
455 "referenced constant has errors",
456 Overflow(mir::BinOp::Add) => "attempt to add with overflow",
457 Overflow(mir::BinOp::Sub) => "attempt to subtract with overflow",
458 Overflow(mir::BinOp::Mul) => "attempt to multiply with overflow",
459 Overflow(mir::BinOp::Div) => "attempt to divide with overflow",
460 Overflow(mir::BinOp::Rem) => "attempt to calculate the remainder with overflow",
461 OverflowNeg => "attempt to negate with overflow",
462 Overflow(mir::BinOp::Shr) => "attempt to shift right with overflow",
463 Overflow(mir::BinOp::Shl) => "attempt to shift left with overflow",
464 Overflow(op) => bug!("{:?} cannot overflow", op),
465 DivisionByZero => "attempt to divide by zero",
466 RemainderByZero => "attempt to calculate the remainder with a divisor of zero",
467 GeneratorResumedAfterReturn => "generator resumed after completion",
468 GeneratorResumedAfterPanic => "generator resumed after panicking",
470 "duplicate interpreter state observed here, const evaluation will never terminate",
475 impl<'tcx> fmt::Display for EvalError<'tcx> {
476 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
477 write!(f, "{}", self.kind)
481 impl<'tcx> fmt::Display for EvalErrorKind<'tcx, u64> {
482 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
483 write!(f, "{:?}", self)
487 impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> {
488 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
489 use self::EvalErrorKind::*;
491 PointerOutOfBounds { ptr, check, allocation_size } => {
492 write!(f, "Pointer must be in-bounds{} at offset {}, but is outside bounds of \
493 allocation {} which has size {}",
495 InboundsCheck::Live => " and live",
496 InboundsCheck::MaybeDead => "",
498 ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
500 ValidationFailure(ref err) => {
501 write!(f, "type validation failed: {}", err)
503 NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
504 FunctionAbiMismatch(caller_abi, callee_abi) =>
505 write!(f, "tried to call a function with ABI {:?} using caller ABI {:?}",
506 callee_abi, caller_abi),
507 FunctionArgMismatch(caller_ty, callee_ty) =>
508 write!(f, "tried to call a function with argument of type {:?} \
509 passing data of type {:?}",
510 callee_ty, caller_ty),
511 FunctionRetMismatch(caller_ty, callee_ty) =>
512 write!(f, "tried to call a function with return type {:?} \
513 passing return place of type {:?}",
514 callee_ty, caller_ty),
515 FunctionArgCountMismatch =>
516 write!(f, "tried to call a function with incorrect number of arguments"),
517 BoundsCheck { ref len, ref index } =>
518 write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index),
519 ReallocatedWrongMemoryKind(ref old, ref new) =>
520 write!(f, "tried to reallocate memory from {} to {}", old, new),
521 DeallocatedWrongMemoryKind(ref old, ref new) =>
522 write!(f, "tried to deallocate {} memory but gave {} as the kind", old, new),
523 Intrinsic(ref err) =>
524 write!(f, "{}", err),
526 write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c),
527 AlignmentCheckFailed { required, has } =>
528 write!(f, "tried to access memory with alignment {}, but alignment {} is required",
529 has.abi(), required.abi()),
530 TypeNotPrimitive(ty) =>
531 write!(f, "expected primitive type, got {}", ty),
533 write!(f, "rustc layout computation failed: {:?}", err),
534 PathNotFound(ref path) =>
535 write!(f, "Cannot find path {:?}", path),
536 MachineError(ref inner) =>
537 write!(f, "{}", inner),
538 IncorrectAllocationInformation(size, size2, align, align2) =>
539 write!(f, "incorrect alloc info: expected size {} and align {}, got size {} and \
540 align {}", size.bytes(), align.abi(), size2.bytes(), align2.abi()),
541 Panic { ref msg, line, col, ref file } =>
542 write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col),
543 InvalidDiscriminant(val) =>
544 write!(f, "encountered invalid enum discriminant {}", val),
545 _ => write!(f, "{}", self.description()),