]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/error.rs
code review fixes
[rust.git] / src / librustc / mir / interpret / error.rs
1 use std::{fmt, env};
2
3 use crate::hir;
4 use crate::hir::map::definitions::DefPathData;
5 use crate::mir;
6 use crate::ty::{self, Ty, layout};
7 use crate::ty::layout::{Size, Align, LayoutError};
8 use rustc_target::spec::abi::Abi;
9 use rustc_macros::HashStable;
10
11 use super::{RawConst, Pointer, CheckInAllocMsg, ScalarMaybeUndef};
12
13 use backtrace::Backtrace;
14
15 use crate::ty::query::TyCtxtAt;
16 use errors::DiagnosticBuilder;
17
18 use syntax_pos::{Pos, Span};
19 use syntax::symbol::Symbol;
20
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.
24     Reported,
25     /// Don't emit an error, the evaluation failed because the MIR was generic
26     /// and the substs didn't fully monomorphize it.
27     TooGeneric,
28 }
29
30 impl ErrorHandled {
31     pub fn assert_reported(self) {
32         match self {
33             ErrorHandled::Reported => {},
34             ErrorHandled::TooGeneric => bug!("MIR interpretation failed without reporting an error \
35                                               even though it was fully monomorphized"),
36         }
37     }
38 }
39
40 CloneTypeFoldableImpls! {
41     ErrorHandled,
42 }
43
44 pub type ConstEvalRawResult<'tcx> = Result<RawConst<'tcx>, ErrorHandled>;
45 pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
46
47 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
48 pub struct ConstEvalErr<'tcx> {
49     pub span: Span,
50     pub error: crate::mir::interpret::InterpError<'tcx>,
51     pub stacktrace: Vec<FrameInfo<'tcx>>,
52 }
53
54 #[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
55 pub struct FrameInfo<'tcx> {
56     /// This span is in the caller.
57     pub call_site: Span,
58     pub instance: ty::Instance<'tcx>,
59     pub lint_root: Option<hir::HirId>,
60 }
61
62 impl<'tcx> fmt::Display for FrameInfo<'tcx> {
63     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64         ty::tls::with(|tcx| {
65             if tcx.def_key(self.instance.def_id()).disambiguated_data.data
66                 == DefPathData::ClosureExpr
67             {
68                 write!(f, "inside call to closure")?;
69             } else {
70                 write!(f, "inside call to `{}`", self.instance)?;
71             }
72             if !self.call_site.is_dummy() {
73                 let lo = tcx.sess.source_map().lookup_char_pos(self.call_site.lo());
74                 write!(f, " at {}:{}:{}", lo.file.name, lo.line, lo.col.to_usize() + 1)?;
75             }
76             Ok(())
77         })
78     }
79 }
80
81 impl<'tcx> ConstEvalErr<'tcx> {
82     pub fn struct_error(
83         &self,
84         tcx: TyCtxtAt<'tcx>,
85         message: &str,
86     ) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> {
87         self.struct_generic(tcx, message, None)
88     }
89
90     pub fn report_as_error(&self, tcx: TyCtxtAt<'tcx>, message: &str) -> ErrorHandled {
91         let err = self.struct_error(tcx, message);
92         match err {
93             Ok(mut err) => {
94                 err.emit();
95                 ErrorHandled::Reported
96             },
97             Err(err) => err,
98         }
99     }
100
101     pub fn report_as_lint(
102         &self,
103         tcx: TyCtxtAt<'tcx>,
104         message: &str,
105         lint_root: hir::HirId,
106         span: Option<Span>,
107     ) -> ErrorHandled {
108         let lint = self.struct_generic(
109             tcx,
110             message,
111             Some(lint_root),
112         );
113         match lint {
114             Ok(mut lint) => {
115                 if let Some(span) = span {
116                     let primary_spans = lint.span.primary_spans().to_vec();
117                     // point at the actual error as the primary span
118                     lint.replace_span_with(span);
119                     // point to the `const` statement as a secondary span
120                     // they don't have any label
121                     for sp in primary_spans {
122                         if sp != span {
123                             lint.span_label(sp, "");
124                         }
125                     }
126                 }
127                 lint.emit();
128                 ErrorHandled::Reported
129             },
130             Err(err) => err,
131         }
132     }
133
134     fn struct_generic(
135         &self,
136         tcx: TyCtxtAt<'tcx>,
137         message: &str,
138         lint_root: Option<hir::HirId>,
139     ) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> {
140         match self.error {
141             err_inval!(Layout(LayoutError::Unknown(_))) |
142             err_inval!(TooGeneric) =>
143                 return Err(ErrorHandled::TooGeneric),
144             err_inval!(Layout(LayoutError::SizeOverflow(_))) |
145             err_inval!(TypeckError) =>
146                 return Err(ErrorHandled::Reported),
147             _ => {},
148         }
149         trace!("reporting const eval failure at {:?}", self.span);
150         let mut err = if let Some(lint_root) = lint_root {
151             let hir_id = self.stacktrace
152                 .iter()
153                 .rev()
154                 .filter_map(|frame| frame.lint_root)
155                 .next()
156                 .unwrap_or(lint_root);
157             tcx.struct_span_lint_hir(
158                 crate::rustc::lint::builtin::CONST_ERR,
159                 hir_id,
160                 tcx.span,
161                 message,
162             )
163         } else {
164             struct_error(tcx, message)
165         };
166         err.span_label(self.span, self.error.to_string());
167         // Skip the last, which is just the environment of the constant.  The stacktrace
168         // is sometimes empty because we create "fake" eval contexts in CTFE to do work
169         // on constant values.
170         if self.stacktrace.len() > 0 {
171             for frame_info in &self.stacktrace[..self.stacktrace.len()-1] {
172                 err.span_label(frame_info.call_site, frame_info.to_string());
173             }
174         }
175         Ok(err)
176     }
177 }
178
179 pub fn struct_error<'tcx>(tcx: TyCtxtAt<'tcx>, msg: &str) -> DiagnosticBuilder<'tcx> {
180     struct_span_err!(tcx.sess, tcx.span, E0080, "{}", msg)
181 }
182
183 /// Packages the kind of error we got from the const code interpreter
184 /// up with a Rust-level backtrace of where the error occured.
185 /// Thsese should always be constructed by calling `.into()` on
186 /// a `InterpError`. In `librustc_mir::interpret`, we have `throw_err_*`
187 /// macros for this.
188 #[derive(Debug, Clone)]
189 pub struct InterpErrorInfo<'tcx> {
190     pub kind: InterpError<'tcx>,
191     backtrace: Option<Box<Backtrace>>,
192 }
193
194
195 impl fmt::Display for InterpErrorInfo<'_> {
196     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197         write!(f, "{}", self.kind)
198     }
199 }
200
201 impl InterpErrorInfo<'_> {
202     pub fn print_backtrace(&mut self) {
203         if let Some(ref mut backtrace) = self.backtrace {
204             print_backtrace(&mut *backtrace);
205         }
206     }
207 }
208
209 fn print_backtrace(backtrace: &mut Backtrace) {
210     backtrace.resolve();
211     eprintln!("\n\nAn error occurred in miri:\n{:?}", backtrace);
212 }
213
214 impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> {
215     fn from(kind: InterpError<'tcx>) -> Self {
216         let backtrace = match env::var("RUST_CTFE_BACKTRACE") {
217             // Matching `RUST_BACKTRACE` -- we treat "0" the same as "not present".
218             Ok(ref val) if val != "0" => {
219                 let mut backtrace = Backtrace::new_unresolved();
220
221                 if val == "immediate" {
222                     // Print it now.
223                     print_backtrace(&mut backtrace);
224                     None
225                 } else {
226                     Some(Box::new(backtrace))
227                 }
228             },
229             _ => None,
230         };
231         InterpErrorInfo {
232             kind,
233             backtrace,
234         }
235     }
236 }
237
238 #[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
239 pub enum PanicInfo<O> {
240     Panic {
241         msg: Symbol,
242         line: u32,
243         col: u32,
244         file: Symbol,
245     },
246     BoundsCheck {
247         len: O,
248         index: O,
249     },
250     Overflow(mir::BinOp),
251     OverflowNeg,
252     DivisionByZero,
253     RemainderByZero,
254     GeneratorResumedAfterReturn,
255     GeneratorResumedAfterPanic,
256 }
257
258 /// Type for MIR `Assert` terminator error messages.
259 pub type AssertMessage<'tcx> = PanicInfo<mir::Operand<'tcx>>;
260
261 impl<O> PanicInfo<O> {
262     /// Getting a description does not require `O` to be printable, and does not
263     /// require allocation.
264     /// The caller is expected to handle `Panic` and `BoundsCheck` separately.
265     pub fn description(&self) -> &'static str {
266         use PanicInfo::*;
267         match self {
268             Overflow(mir::BinOp::Add) =>
269                 "attempt to add with overflow",
270             Overflow(mir::BinOp::Sub) =>
271                 "attempt to subtract with overflow",
272             Overflow(mir::BinOp::Mul) =>
273                 "attempt to multiply with overflow",
274             Overflow(mir::BinOp::Div) =>
275                 "attempt to divide with overflow",
276             Overflow(mir::BinOp::Rem) =>
277                 "attempt to calculate the remainder with overflow",
278             OverflowNeg =>
279                 "attempt to negate with overflow",
280             Overflow(mir::BinOp::Shr) =>
281                 "attempt to shift right with overflow",
282             Overflow(mir::BinOp::Shl) =>
283                 "attempt to shift left with overflow",
284             Overflow(op) =>
285                 bug!("{:?} cannot overflow", op),
286             DivisionByZero =>
287                 "attempt to divide by zero",
288             RemainderByZero =>
289                 "attempt to calculate the remainder with a divisor of zero",
290             GeneratorResumedAfterReturn =>
291                 "generator resumed after completion",
292             GeneratorResumedAfterPanic =>
293                 "generator resumed after panicking",
294             Panic { .. } | BoundsCheck { .. } =>
295                 bug!("Unexpected PanicInfo"),
296         }
297     }
298 }
299
300 impl<O: fmt::Debug> fmt::Debug for PanicInfo<O> {
301     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
302         use PanicInfo::*;
303         match self {
304             Panic { ref msg, line, col, ref file } =>
305                 write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col),
306             BoundsCheck { ref len, ref index } =>
307                 write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index),
308             _ =>
309                 write!(f, "{}", self.description()),
310         }
311     }
312 }
313
314 #[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
315 pub enum InvalidProgramInfo<'tcx> {
316     /// Resolution can fail if we are in a too generic context.
317     TooGeneric,
318     /// Cannot compute this constant because it depends on another one
319     /// which already produced an error.
320     ReferencedConstant,
321     /// Abort in case type errors are reached.
322     TypeckError,
323     /// An error occurred during layout computation.
324     Layout(layout::LayoutError<'tcx>),
325 }
326
327 impl fmt::Debug for InvalidProgramInfo<'tcx> {
328     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
329         use InvalidProgramInfo::*;
330         match self {
331             TooGeneric =>
332                 write!(f, "encountered overly generic constant"),
333             ReferencedConstant =>
334                 write!(f, "referenced constant has errors"),
335             TypeckError =>
336                 write!(f, "encountered constants with type errors, stopping evaluation"),
337             Layout(ref err) =>
338                 write!(f, "rustc layout computation failed: {:?}", err),
339         }
340     }
341 }
342
343 #[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
344 pub enum UndefinedBehaviourInfo {
345     /// Handle cases which for which we do not have a fixed variant.
346     Ub(String),
347     /// Unreachable code was executed.
348     Unreachable,
349 }
350
351 impl fmt::Debug for UndefinedBehaviourInfo {
352     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
353         use UndefinedBehaviourInfo::*;
354         match self {
355             Ub(ref msg) =>
356                 write!(f, "{}", msg),
357             Unreachable =>
358                 write!(f, "entered unreachable code"),
359         }
360     }
361 }
362
363 #[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
364 pub enum UnsupportedOpInfo<'tcx> {
365     /// Handle cases which for which we do not have a fixed variant.
366     Unimplemented(String),
367
368     // -- Everything below is not classified yet --
369     FunctionAbiMismatch(Abi, Abi),
370     FunctionArgMismatch(Ty<'tcx>, Ty<'tcx>),
371     FunctionRetMismatch(Ty<'tcx>, Ty<'tcx>),
372     FunctionArgCountMismatch,
373     UnterminatedCString(Pointer),
374     DanglingPointerDeref,
375     DoubleFree,
376     InvalidMemoryAccess,
377     InvalidFunctionPointer,
378     InvalidBool,
379     InvalidDiscriminant(ScalarMaybeUndef),
380     PointerOutOfBounds {
381         ptr: Pointer,
382         msg: CheckInAllocMsg,
383         allocation_size: Size,
384     },
385     InvalidNullPointerUsage,
386     ReadPointerAsBytes,
387     ReadBytesAsPointer,
388     ReadForeignStatic,
389     InvalidPointerMath,
390     ReadUndefBytes(Size),
391     DeadLocal,
392     InvalidBoolOp(mir::BinOp),
393     InlineAsm,
394     UnimplementedTraitSelection,
395     CalledClosureAsFunction,
396     NoMirFor(String),
397     /// This variant is used by machines to signal their own errors that do not
398     /// match an existing variant.
399     MachineError(String),
400     DerefFunctionPointer,
401     ExecuteMemory,
402     Intrinsic(String),
403     InvalidChar(u128),
404     OutOfTls,
405     TlsOutOfBounds,
406     AbiViolation(String),
407     AlignmentCheckFailed {
408         required: Align,
409         has: Align,
410     },
411     ValidationFailure(String),
412     VtableForArgumentlessMethod,
413     ModifiedConstantMemory,
414     ModifiedStatic,
415     AssumptionNotHeld,
416     TypeNotPrimitive(Ty<'tcx>),
417     ReallocatedWrongMemoryKind(String, String),
418     DeallocatedWrongMemoryKind(String, String),
419     ReallocateNonBasePtr,
420     DeallocateNonBasePtr,
421     IncorrectAllocationInformation(Size, Size, Align, Align),
422     HeapAllocZeroBytes,
423     HeapAllocNonPowerOfTwoAlignment(u64),
424     ReadFromReturnPointer,
425     PathNotFound(Vec<String>),
426 }
427
428 impl fmt::Debug for UnsupportedOpInfo<'tcx> {
429     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
430         use UnsupportedOpInfo::*;
431         match self {
432             PointerOutOfBounds { ptr, msg, allocation_size } => {
433                 write!(f, "{} failed: pointer must be in-bounds at offset {}, \
434                           but is outside bounds of allocation {} which has size {}",
435                     msg, ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
436             },
437             ValidationFailure(ref err) => {
438                 write!(f, "type validation failed: {}", err)
439             }
440             NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
441             FunctionAbiMismatch(caller_abi, callee_abi) =>
442                 write!(f, "tried to call a function with ABI {:?} using caller ABI {:?}",
443                     callee_abi, caller_abi),
444             FunctionArgMismatch(caller_ty, callee_ty) =>
445                 write!(f, "tried to call a function with argument of type {:?} \
446                            passing data of type {:?}",
447                     callee_ty, caller_ty),
448             FunctionRetMismatch(caller_ty, callee_ty) =>
449                 write!(f, "tried to call a function with return type {:?} \
450                            passing return place of type {:?}",
451                     callee_ty, caller_ty),
452             FunctionArgCountMismatch =>
453                 write!(f, "tried to call a function with incorrect number of arguments"),
454             ReallocatedWrongMemoryKind(ref old, ref new) =>
455                 write!(f, "tried to reallocate memory from {} to {}", old, new),
456             DeallocatedWrongMemoryKind(ref old, ref new) =>
457                 write!(f, "tried to deallocate {} memory but gave {} as the kind", old, new),
458             InvalidChar(c) =>
459                 write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c),
460             AlignmentCheckFailed { required, has } =>
461                write!(f, "tried to access memory with alignment {}, but alignment {} is required",
462                       has.bytes(), required.bytes()),
463             TypeNotPrimitive(ty) =>
464                 write!(f, "expected primitive type, got {}", ty),
465             PathNotFound(ref path) =>
466                 write!(f, "Cannot find path {:?}", path),
467             IncorrectAllocationInformation(size, size2, align, align2) =>
468                 write!(f, "incorrect alloc info: expected size {} and align {}, \
469                            got size {} and align {}",
470                     size.bytes(), align.bytes(), size2.bytes(), align2.bytes()),
471             InvalidDiscriminant(val) =>
472                 write!(f, "encountered invalid enum discriminant {}", val),
473             InvalidMemoryAccess =>
474                 write!(f, "tried to access memory through an invalid pointer"),
475             DanglingPointerDeref =>
476                 write!(f, "dangling pointer was dereferenced"),
477             DoubleFree =>
478                 write!(f, "tried to deallocate dangling pointer"),
479             InvalidFunctionPointer =>
480                 write!(f, "tried to use a function pointer after offsetting it"),
481             InvalidBool =>
482                 write!(f, "invalid boolean value read"),
483             InvalidNullPointerUsage =>
484                 write!(f, "invalid use of NULL pointer"),
485             ReadPointerAsBytes =>
486                 write!(f, "a raw memory access tried to access part of a pointer value as raw \
487                     bytes"),
488             ReadBytesAsPointer =>
489                 write!(f, "a memory access tried to interpret some bytes as a pointer"),
490             ReadForeignStatic =>
491                 write!(f, "tried to read from foreign (extern) static"),
492             InvalidPointerMath =>
493                 write!(f, "attempted to do invalid arithmetic on pointers that would leak base \
494                     addresses, e.g., comparing pointers into different allocations"),
495             DeadLocal =>
496                 write!(f, "tried to access a dead local variable"),
497             DerefFunctionPointer =>
498                 write!(f, "tried to dereference a function pointer"),
499             ExecuteMemory =>
500                 write!(f, "tried to treat a memory pointer as a function pointer"),
501             OutOfTls =>
502                 write!(f, "reached the maximum number of representable TLS keys"),
503             TlsOutOfBounds =>
504                 write!(f, "accessed an invalid (unallocated) TLS key"),
505             CalledClosureAsFunction =>
506                 write!(f, "tried to call a closure through a function pointer"),
507             VtableForArgumentlessMethod =>
508                 write!(f, "tried to call a vtable function without arguments"),
509             ModifiedConstantMemory =>
510                 write!(f, "tried to modify constant memory"),
511             ModifiedStatic =>
512                 write!(f, "tried to modify a static's initial value from another static's \
513                     initializer"),
514             AssumptionNotHeld =>
515                 write!(f, "`assume` argument was false"),
516             InlineAsm =>
517                 write!(f, "miri does not support inline assembly"),
518             ReallocateNonBasePtr =>
519                 write!(f, "tried to reallocate with a pointer not to the beginning of an \
520                     existing object"),
521             DeallocateNonBasePtr =>
522                 write!(f, "tried to deallocate with a pointer not to the beginning of an \
523                     existing object"),
524             HeapAllocZeroBytes =>
525                 write!(f, "tried to re-, de- or allocate zero bytes on the heap"),
526             ReadFromReturnPointer =>
527                 write!(f, "tried to read from the return pointer"),
528             UnimplementedTraitSelection =>
529                 write!(f, "there were unresolved type arguments during trait selection"),
530             InvalidBoolOp(_) =>
531                 write!(f, "invalid boolean operation"),
532             UnterminatedCString(_) =>
533                 write!(f, "attempted to get length of a null terminated string, but no null \
534                     found before end of allocation"),
535             ReadUndefBytes(_) =>
536                 write!(f, "attempted to read undefined bytes"),
537             HeapAllocNonPowerOfTwoAlignment(_) =>
538                 write!(f, "tried to re-, de-, or allocate heap memory with alignment that is \
539                     not a power of two"),
540             MachineError(ref msg) |
541             Unimplemented(ref msg) |
542             AbiViolation(ref msg) |
543             Intrinsic(ref msg) =>
544                 write!(f, "{}", msg),
545         }
546     }
547 }
548
549 #[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
550 pub enum ResourceExhaustionInfo {
551     /// The stack grew too big.
552     StackFrameLimitReached,
553     /// The program ran into an infinite loop.
554     InfiniteLoop,
555 }
556
557 impl fmt::Debug for ResourceExhaustionInfo {
558     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
559         use ResourceExhaustionInfo::*;
560         match self {
561             StackFrameLimitReached =>
562                 write!(f, "reached the configured maximum number of stack frames"),
563             InfiniteLoop =>
564                 write!(f, "duplicate interpreter state observed here, const evaluation will never \
565                     terminate"),
566         }
567     }
568 }
569
570 #[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
571 pub enum InterpError<'tcx> {
572     /// The program panicked.
573     Panic(PanicInfo<u64>),
574     /// The program caused undefined behavior.
575     UndefinedBehaviour(UndefinedBehaviourInfo),
576     /// The program did something the interpreter does not support (some of these *might* be UB
577     /// but the interpreter is not sure).
578     Unsupported(UnsupportedOpInfo<'tcx>),
579     /// The program was invalid (ill-typed, not sufficiently monomorphized, ...).
580     InvalidProgram(InvalidProgramInfo<'tcx>),
581     /// The program exhausted the interpreter's resources (stack/heap too big,
582     /// execution takes too long, ..).
583     ResourceExhaustion(ResourceExhaustionInfo),
584     /// Not actually an interpreter error -- used to signal that execution has exited
585     /// with the given status code.  Used by Miri, but not by CTFE.
586     Exit(i32),
587 }
588
589 pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
590
591 impl fmt::Display for InterpError<'_> {
592     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
593         // Forward `Display` to `Debug`
594         write!(f, "{:?}", self)
595     }
596 }
597
598 impl fmt::Debug for InterpError<'_> {
599     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
600         use InterpError::*;
601         match *self {
602             Unsupported(ref msg) =>
603                 write!(f, "{:?}", msg),
604             InvalidProgram(ref msg) =>
605                 write!(f, "{:?}", msg),
606             UndefinedBehaviour(ref msg) =>
607                 write!(f, "{:?}", msg),
608             ResourceExhaustion(ref msg) =>
609                 write!(f, "{:?}", msg),
610             Panic(ref msg) =>
611                 write!(f, "{:?}", msg),
612             Exit(code) =>
613                 write!(f, "exited with status code {}", code),
614         }
615     }
616 }