]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/error.rs
Various minor/cosmetic improvements to code
[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::{RawConst, 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 ConstEvalRawResult<'tcx> = Result<RawConst<'tcx>, ErrorHandled>;
50 pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
51
52 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
53 pub struct ConstEvalErr<'tcx> {
54     pub span: Span,
55     pub error: ::mir::interpret::EvalErrorKind<'tcx, u64>,
56     pub stacktrace: Vec<FrameInfo<'tcx>>,
57 }
58
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>,
64 }
65
66 impl<'tcx> fmt::Display for FrameInfo<'tcx> {
67     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68         ty::tls::with(|tcx| {
69             if tcx.def_key(self.instance.def_id()).disambiguated_data.data
70                 == DefPathData::ClosureExpr
71             {
72                 write!(f, "inside call to closure")?;
73             } else {
74                 write!(f, "inside call to `{}`", self.instance)?;
75             }
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)?;
79             }
80             Ok(())
81         })
82     }
83 }
84
85 impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
86     pub fn struct_error(&self,
87         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
88         message: &str)
89         -> Result<DiagnosticBuilder<'tcx>, ErrorHandled>
90     {
91         self.struct_generic(tcx, message, None)
92     }
93
94     pub fn report_as_error(&self,
95         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
96         message: &str
97     ) -> ErrorHandled {
98         let err = self.struct_error(tcx, message);
99         match err {
100             Ok(mut err) => {
101                 err.emit();
102                 ErrorHandled::Reported
103             },
104             Err(err) => err,
105         }
106     }
107
108     pub fn report_as_lint(&self,
109         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
110         message: &str,
111         lint_root: ast::NodeId,
112     ) -> ErrorHandled {
113         let lint = self.struct_generic(
114             tcx,
115             message,
116             Some(lint_root),
117         );
118         match lint {
119             Ok(mut lint) => {
120                 lint.emit();
121                 ErrorHandled::Reported
122             },
123             Err(err) => err,
124         }
125     }
126
127     fn struct_generic(
128         &self,
129         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
130         message: &str,
131         lint_root: Option<ast::NodeId>,
132     ) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> {
133         match self.error {
134             EvalErrorKind::Layout(LayoutError::Unknown(_)) |
135             EvalErrorKind::TooGeneric => return Err(ErrorHandled::TooGeneric),
136             EvalErrorKind::Layout(LayoutError::SizeOverflow(_)) |
137             EvalErrorKind::TypeckError => return Err(ErrorHandled::Reported),
138             _ => {},
139         }
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
143                 .iter()
144                 .rev()
145                 .filter_map(|frame| frame.lint_root)
146                 .next()
147                 .unwrap_or(lint_root);
148             tcx.struct_span_lint_node(
149                 ::rustc::lint::builtin::CONST_ERR,
150                 node_id,
151                 tcx.span,
152                 message,
153             )
154         } else {
155             struct_error(tcx, message)
156         };
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());
164             }
165         }
166         Ok(err)
167     }
168 }
169
170 pub fn struct_error<'a, 'gcx, 'tcx>(
171     tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
172     msg: &str,
173 ) -> DiagnosticBuilder<'tcx> {
174     struct_span_err!(tcx.sess, tcx.span, E0080, "{}", msg)
175 }
176
177 #[derive(Debug, Clone)]
178 pub struct EvalError<'tcx> {
179     pub kind: EvalErrorKind<'tcx, u64>,
180     pub backtrace: Option<Box<Backtrace>>,
181 }
182
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));
187         }
188     }
189 }
190
191 fn print_backtrace(backtrace: &mut Backtrace) -> String {
192     use std::fmt::Write;
193
194     backtrace.resolve();
195
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();
201         }
202         let mut first = true;
203         for symbol in frame.symbols() {
204             if first {
205                 write!(trace_text, "  {}: ", i).unwrap();
206                 first = false;
207             } else {
208                 let len = i.to_string().len();
209                 write!(trace_text, "  {}  ", " ".repeat(len)).unwrap();
210             }
211             if let Some(name) = symbol.name() {
212                 write!(trace_text, "{}\n", name).unwrap();
213             } else {
214                 write!(trace_text, "<unknown>\n").unwrap();
215             }
216             write!(trace_text, "           at ").unwrap();
217             if let Some(file_path) = symbol.filename() {
218                 write!(trace_text, "{}", file_path.display()).unwrap();
219             } else {
220                 write!(trace_text, "<unknown_file>").unwrap();
221             }
222             if let Some(line) = symbol.lineno() {
223                 write!(trace_text, ":{}\n", line).unwrap();
224             } else {
225                 write!(trace_text, "\n").unwrap();
226             }
227         }
228     }
229     trace_text
230 }
231
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();
238
239                 if val == "immediate" {
240                     // Print it now
241                     eprintln!("{}", print_backtrace(&mut backtrace));
242                     None
243                 } else {
244                     Some(Box::new(backtrace))
245                 }
246             },
247             _ => None,
248         };
249         EvalError {
250             kind,
251             backtrace,
252         }
253     }
254 }
255
256 pub type AssertMessage<'tcx> = EvalErrorKind<'tcx, mir::Operand<'tcx>>;
257
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),
263
264     FunctionAbiMismatch(Abi, Abi),
265     FunctionArgMismatch(Ty<'tcx>, Ty<'tcx>),
266     FunctionRetMismatch(Ty<'tcx>, Ty<'tcx>),
267     FunctionArgCountMismatch,
268     NoMirFor(String),
269     UnterminatedCString(Pointer),
270     DanglingPointerDeref,
271     DoubleFree,
272     InvalidMemoryAccess,
273     InvalidFunctionPointer,
274     InvalidBool,
275     InvalidDiscriminant(ScalarMaybeUndef),
276     PointerOutOfBounds {
277         ptr: Pointer,
278         check: InboundsCheck,
279         allocation_size: Size,
280     },
281     InvalidNullPointerUsage,
282     ReadPointerAsBytes,
283     ReadBytesAsPointer,
284     ReadForeignStatic,
285     InvalidPointerMath,
286     ReadUndefBytes(Size),
287     DeadLocal,
288     InvalidBoolOp(mir::BinOp),
289     Unimplemented(String),
290     DerefFunctionPointer,
291     ExecuteMemory,
292     BoundsCheck { len: O, index: O },
293     Overflow(mir::BinOp),
294     OverflowNeg,
295     DivisionByZero,
296     RemainderByZero,
297     Intrinsic(String),
298     InvalidChar(u128),
299     StackFrameLimitReached,
300     OutOfTls,
301     TlsOutOfBounds,
302     AbiViolation(String),
303     AlignmentCheckFailed {
304         required: Align,
305         has: Align,
306     },
307     ValidationFailure(String),
308     CalledClosureAsFunction,
309     VtableForArgumentlessMethod,
310     ModifiedConstantMemory,
311     ModifiedStatic,
312     AssumptionNotHeld,
313     InlineAsm,
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>),
321     HeapAllocZeroBytes,
322     HeapAllocNonPowerOfTwoAlignment(u64),
323     Unreachable,
324     Panic {
325         msg: Symbol,
326         line: u32,
327         col: u32,
328         file: Symbol,
329     },
330     ReadFromReturnPointer,
331     PathNotFound(Vec<String>),
332     UnimplementedTraitSelection,
333     /// Abort in case type errors are reached
334     TypeckError,
335     /// Resolution can fail if we are in a too generic context
336     TooGeneric,
337     /// Cannot compute this constant because it depends on another one
338     /// which already produced an error
339     ReferencedConstant,
340     GeneratorResumedAfterReturn,
341     GeneratorResumedAfterPanic,
342     InfiniteLoop,
343 }
344
345 pub type EvalResult<'tcx, T = ()> = Result<T, EvalError<'tcx>>;
346
347 impl<'tcx, O> EvalErrorKind<'tcx, O> {
348     pub fn description(&self) -> &str {
349         use self::EvalErrorKind::*;
350         match *self {
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",
359             DoubleFree =>
360                 "tried to deallocate dangling pointer",
361             InvalidFunctionPointer =>
362                 "tried to use a function pointer after offsetting it",
363             InvalidBool =>
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",
377             ReadForeignStatic =>
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",
382             ReadUndefBytes(_) =>
383                 "attempted to read undefined bytes",
384             DeadLocal =>
385                 "tried to access a dead local variable",
386             InvalidBoolOp(_) =>
387                 "invalid boolean operation",
388             Unimplemented(ref msg) => msg,
389             DerefFunctionPointer =>
390                 "tried to dereference a function pointer",
391             ExecuteMemory =>
392                 "tried to treat a memory pointer as a function pointer",
393             BoundsCheck{..} =>
394                 "array index out of bounds",
395             Intrinsic(..) =>
396                 "intrinsic failed",
397             NoMirFor(..) =>
398                 "mir not found",
399             InvalidChar(..) =>
400                 "tried to interpret an invalid 32-bit value as a char",
401             StackFrameLimitReached =>
402                 "reached the configured maximum number of stack frames",
403             OutOfTls =>
404                 "reached the maximum number of representable TLS keys",
405             TlsOutOfBounds =>
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",
416             ModifiedStatic =>
417                 "tried to modify a static's initial value from another static's initializer",
418             AssumptionNotHeld =>
419                 "`assume` argument was false",
420             InlineAsm =>
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",
434             Layout(_) =>
435                 "rustc layout computation failed",
436             UnterminatedCString(_) =>
437                 "attempted to get length of a null terminated string, but no null found before end \
438                 of allocation",
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 \
443                 two",
444             Unreachable =>
445                 "entered unreachable code",
446             Panic { .. } =>
447                 "the evaluated program panicked",
448             ReadFromReturnPointer =>
449                 "tried to read from the return pointer",
450             PathNotFound(_) =>
451                 "a path could not be resolved, maybe the crate is not loaded",
452             UnimplementedTraitSelection =>
453                 "there were unresolved type arguments during trait selection",
454             TypeckError =>
455                 "encountered constants with type errors, stopping evaluation",
456             TooGeneric =>
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",
473             InfiniteLoop =>
474                 "duplicate interpreter state observed here, const evaluation will never terminate",
475         }
476     }
477 }
478
479 impl<'tcx> fmt::Display for EvalError<'tcx> {
480     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
481         write!(f, "{}", self.kind)
482     }
483 }
484
485 impl<'tcx> fmt::Display for EvalErrorKind<'tcx, u64> {
486     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
487         write!(f, "{:?}", self)
488     }
489 }
490
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::*;
494         match *self {
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 {}",
498                        match check {
499                            InboundsCheck::Live => " and live",
500                            InboundsCheck::MaybeDead => "",
501                        },
502                        ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
503             },
504             ValidationFailure(ref err) => {
505                 write!(f, "type validation failed: {}", err)
506             }
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),
529             InvalidChar(c) =>
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),
536             Layout(ref err) =>
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()),
551         }
552     }
553 }