]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/error.rs
Auto merge of #55971 - SergioBenitez:skip-non-semantic, r=alexcrichton
[rust.git] / src / librustc / mir / interpret / error.rs
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.
4 //
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.
10
11 use std::{fmt, env};
12
13 use hir::map::definitions::DefPathData;
14 use mir;
15 use ty::{self, Ty, layout};
16 use ty::layout::{Size, Align, LayoutError};
17 use rustc_target::spec::abi::Abi;
18
19 use super::{Pointer, InboundsCheck, ScalarMaybeUndef};
20
21 use backtrace::Backtrace;
22
23 use ty::query::TyCtxtAt;
24 use errors::DiagnosticBuilder;
25
26 use syntax_pos::{Pos, Span};
27 use syntax::ast;
28 use syntax::symbol::Symbol;
29
30 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
31 pub enum ErrorHandled {
32     /// Already reported a lint or an error for this evaluation
33     Reported,
34     /// Don't emit an error, the evaluation failed because the MIR was generic
35     /// and the substs didn't fully monomorphize it.
36     TooGeneric,
37 }
38
39 impl ErrorHandled {
40     pub fn assert_reported(self) {
41         match self {
42             ErrorHandled::Reported => {},
43             ErrorHandled::TooGeneric => bug!("MIR interpretation failed without reporting an error \
44                                               even though it was fully monomorphized"),
45         }
46     }
47 }
48
49 pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
50
51 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
52 pub struct ConstEvalErr<'tcx> {
53     pub span: Span,
54     pub error: ::mir::interpret::EvalErrorKind<'tcx, u64>,
55     pub stacktrace: Vec<FrameInfo<'tcx>>,
56 }
57
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>,
63 }
64
65 impl<'tcx> fmt::Display for FrameInfo<'tcx> {
66     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67         ty::tls::with(|tcx| {
68             if tcx.def_key(self.instance.def_id()).disambiguated_data.data
69                 == DefPathData::ClosureExpr
70             {
71                 write!(f, "inside call to closure")?;
72             } else {
73                 write!(f, "inside call to `{}`", self.instance)?;
74             }
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)?;
78             }
79             Ok(())
80         })
81     }
82 }
83
84 impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
85     pub fn struct_error(&self,
86         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
87         message: &str)
88         -> Result<DiagnosticBuilder<'tcx>, ErrorHandled>
89     {
90         self.struct_generic(tcx, message, None)
91     }
92
93     pub fn report_as_error(&self,
94         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
95         message: &str
96     ) -> ErrorHandled {
97         let err = self.struct_error(tcx, message);
98         match err {
99             Ok(mut err) => {
100                 err.emit();
101                 ErrorHandled::Reported
102             },
103             Err(err) => err,
104         }
105     }
106
107     pub fn report_as_lint(&self,
108         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
109         message: &str,
110         lint_root: ast::NodeId,
111     ) -> ErrorHandled {
112         let lint = self.struct_generic(
113             tcx,
114             message,
115             Some(lint_root),
116         );
117         match lint {
118             Ok(mut lint) => {
119                 lint.emit();
120                 ErrorHandled::Reported
121             },
122             Err(err) => err,
123         }
124     }
125
126     fn struct_generic(
127         &self,
128         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
129         message: &str,
130         lint_root: Option<ast::NodeId>,
131     ) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> {
132         match self.error {
133             EvalErrorKind::Layout(LayoutError::Unknown(_)) |
134             EvalErrorKind::TooGeneric => return Err(ErrorHandled::TooGeneric),
135             EvalErrorKind::Layout(LayoutError::SizeOverflow(_)) |
136             EvalErrorKind::TypeckError => return Err(ErrorHandled::Reported),
137             _ => {},
138         }
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
142                 .iter()
143                 .rev()
144                 .filter_map(|frame| frame.lint_root)
145                 .next()
146                 .unwrap_or(lint_root);
147             tcx.struct_span_lint_node(
148                 ::rustc::lint::builtin::CONST_ERR,
149                 node_id,
150                 tcx.span,
151                 message,
152             )
153         } else {
154             struct_error(tcx, message)
155         };
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());
163             }
164         }
165         Ok(err)
166     }
167 }
168
169 pub fn struct_error<'a, 'gcx, 'tcx>(
170     tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
171     msg: &str,
172 ) -> DiagnosticBuilder<'tcx> {
173     struct_span_err!(tcx.sess, tcx.span, E0080, "{}", msg)
174 }
175
176 #[derive(Debug, Clone)]
177 pub struct EvalError<'tcx> {
178     pub kind: EvalErrorKind<'tcx, u64>,
179     pub backtrace: Option<Box<Backtrace>>,
180 }
181
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));
186         }
187     }
188 }
189
190 fn print_backtrace(backtrace: &mut Backtrace) -> String {
191     use std::fmt::Write;
192
193     backtrace.resolve();
194
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();
200         }
201         let mut first = true;
202         for symbol in frame.symbols() {
203             if first {
204                 write!(trace_text, "  {}: ", i).unwrap();
205                 first = false;
206             } else {
207                 let len = i.to_string().len();
208                 write!(trace_text, "  {}  ", " ".repeat(len)).unwrap();
209             }
210             if let Some(name) = symbol.name() {
211                 write!(trace_text, "{}\n", name).unwrap();
212             } else {
213                 write!(trace_text, "<unknown>\n").unwrap();
214             }
215             write!(trace_text, "           at ").unwrap();
216             if let Some(file_path) = symbol.filename() {
217                 write!(trace_text, "{}", file_path.display()).unwrap();
218             } else {
219                 write!(trace_text, "<unknown_file>").unwrap();
220             }
221             if let Some(line) = symbol.lineno() {
222                 write!(trace_text, ":{}\n", line).unwrap();
223             } else {
224                 write!(trace_text, "\n").unwrap();
225             }
226         }
227     }
228     trace_text
229 }
230
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();
237
238                 if val == "immediate" {
239                     // Print it now
240                     eprintln!("{}", print_backtrace(&mut backtrace));
241                     None
242                 } else {
243                     Some(Box::new(backtrace))
244                 }
245             },
246             _ => None,
247         };
248         EvalError {
249             kind,
250             backtrace,
251         }
252     }
253 }
254
255 pub type AssertMessage<'tcx> = EvalErrorKind<'tcx, mir::Operand<'tcx>>;
256
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),
262
263     FunctionAbiMismatch(Abi, Abi),
264     FunctionArgMismatch(Ty<'tcx>, Ty<'tcx>),
265     FunctionRetMismatch(Ty<'tcx>, Ty<'tcx>),
266     FunctionArgCountMismatch,
267     NoMirFor(String),
268     UnterminatedCString(Pointer),
269     DanglingPointerDeref,
270     DoubleFree,
271     InvalidMemoryAccess,
272     InvalidFunctionPointer,
273     InvalidBool,
274     InvalidDiscriminant(ScalarMaybeUndef),
275     PointerOutOfBounds {
276         ptr: Pointer,
277         check: InboundsCheck,
278         allocation_size: Size,
279     },
280     InvalidNullPointerUsage,
281     ReadPointerAsBytes,
282     ReadBytesAsPointer,
283     ReadForeignStatic,
284     InvalidPointerMath,
285     ReadUndefBytes(Size),
286     DeadLocal,
287     InvalidBoolOp(mir::BinOp),
288     Unimplemented(String),
289     DerefFunctionPointer,
290     ExecuteMemory,
291     BoundsCheck { len: O, index: O },
292     Overflow(mir::BinOp),
293     OverflowNeg,
294     DivisionByZero,
295     RemainderByZero,
296     Intrinsic(String),
297     InvalidChar(u128),
298     StackFrameLimitReached,
299     OutOfTls,
300     TlsOutOfBounds,
301     AbiViolation(String),
302     AlignmentCheckFailed {
303         required: Align,
304         has: Align,
305     },
306     ValidationFailure(String),
307     CalledClosureAsFunction,
308     VtableForArgumentlessMethod,
309     ModifiedConstantMemory,
310     AssumptionNotHeld,
311     InlineAsm,
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>),
319     HeapAllocZeroBytes,
320     HeapAllocNonPowerOfTwoAlignment(u64),
321     Unreachable,
322     Panic {
323         msg: Symbol,
324         line: u32,
325         col: u32,
326         file: Symbol,
327     },
328     ReadFromReturnPointer,
329     PathNotFound(Vec<String>),
330     UnimplementedTraitSelection,
331     /// Abort in case type errors are reached
332     TypeckError,
333     /// Resolution can fail if we are in a too generic context
334     TooGeneric,
335     /// Cannot compute this constant because it depends on another one
336     /// which already produced an error
337     ReferencedConstant,
338     GeneratorResumedAfterReturn,
339     GeneratorResumedAfterPanic,
340     InfiniteLoop,
341 }
342
343 pub type EvalResult<'tcx, T = ()> = Result<T, EvalError<'tcx>>;
344
345 impl<'tcx, O> EvalErrorKind<'tcx, O> {
346     pub fn description(&self) -> &str {
347         use self::EvalErrorKind::*;
348         match *self {
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",
357             DoubleFree =>
358                 "tried to deallocate dangling pointer",
359             InvalidFunctionPointer =>
360                 "tried to use a function pointer after offsetting it",
361             InvalidBool =>
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",
375             ReadForeignStatic =>
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",
380             ReadUndefBytes(_) =>
381                 "attempted to read undefined bytes",
382             DeadLocal =>
383                 "tried to access a dead local variable",
384             InvalidBoolOp(_) =>
385                 "invalid boolean operation",
386             Unimplemented(ref msg) => msg,
387             DerefFunctionPointer =>
388                 "tried to dereference a function pointer",
389             ExecuteMemory =>
390                 "tried to treat a memory pointer as a function pointer",
391             BoundsCheck{..} =>
392                 "array index out of bounds",
393             Intrinsic(..) =>
394                 "intrinsic failed",
395             NoMirFor(..) =>
396                 "mir not found",
397             InvalidChar(..) =>
398                 "tried to interpret an invalid 32-bit value as a char",
399             StackFrameLimitReached =>
400                 "reached the configured maximum number of stack frames",
401             OutOfTls =>
402                 "reached the maximum number of representable TLS keys",
403             TlsOutOfBounds =>
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",
414             AssumptionNotHeld =>
415                 "`assume` argument was false",
416             InlineAsm =>
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",
430             Layout(_) =>
431                 "rustc layout computation failed",
432             UnterminatedCString(_) =>
433                 "attempted to get length of a null terminated string, but no null found before end \
434                 of allocation",
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 \
439                 two",
440             Unreachable =>
441                 "entered unreachable code",
442             Panic { .. } =>
443                 "the evaluated program panicked",
444             ReadFromReturnPointer =>
445                 "tried to read from the return pointer",
446             PathNotFound(_) =>
447                 "a path could not be resolved, maybe the crate is not loaded",
448             UnimplementedTraitSelection =>
449                 "there were unresolved type arguments during trait selection",
450             TypeckError =>
451                 "encountered constants with type errors, stopping evaluation",
452             TooGeneric =>
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",
469             InfiniteLoop =>
470                 "duplicate interpreter state observed here, const evaluation will never terminate",
471         }
472     }
473 }
474
475 impl<'tcx> fmt::Display for EvalError<'tcx> {
476     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
477         write!(f, "{}", self.kind)
478     }
479 }
480
481 impl<'tcx> fmt::Display for EvalErrorKind<'tcx, u64> {
482     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
483         write!(f, "{:?}", self)
484     }
485 }
486
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::*;
490         match *self {
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 {}",
494                        match check {
495                            InboundsCheck::Live => " and live",
496                            InboundsCheck::MaybeDead => "",
497                        },
498                        ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
499             },
500             ValidationFailure(ref err) => {
501                 write!(f, "type validation failed: {}", err)
502             }
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),
525             InvalidChar(c) =>
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),
532             Layout(ref err) =>
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()),
546         }
547     }
548 }