]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/error.rs
Auto merge of #51702 - ecstatic-morse:infinite-loop-detection, r=oli-obk
[rust.git] / src / librustc / mir / interpret / error.rs
1 use std::{fmt, env};
2
3 use mir;
4 use ty::{FnSig, Ty, layout};
5 use ty::layout::{Size, Align};
6 use rustc_data_structures::sync::Lrc;
7
8 use super::{
9     Pointer, Lock, AccessKind
10 };
11
12 use backtrace::Backtrace;
13
14 use ty;
15 use ty::query::TyCtxtAt;
16 use errors::DiagnosticBuilder;
17
18 use syntax_pos::Span;
19 use syntax::ast;
20
21 pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, Lrc<ConstEvalErr<'tcx>>>;
22
23 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
24 pub struct ConstEvalErr<'tcx> {
25     pub span: Span,
26     pub error: ::mir::interpret::EvalError<'tcx>,
27     pub stacktrace: Vec<FrameInfo>,
28 }
29
30 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
31 pub struct FrameInfo {
32     pub span: Span,
33     pub location: String,
34     pub lint_root: Option<ast::NodeId>,
35 }
36
37 impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
38     pub fn struct_error(&self,
39         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
40         message: &str)
41         -> Option<DiagnosticBuilder<'tcx>>
42     {
43         self.struct_generic(tcx, message, None)
44     }
45
46     pub fn report_as_error(&self,
47         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
48         message: &str
49     ) {
50         let err = self.struct_generic(tcx, message, None);
51         if let Some(mut err) = err {
52             err.emit();
53         }
54     }
55
56     pub fn report_as_lint(&self,
57         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
58         message: &str,
59         lint_root: ast::NodeId,
60     ) {
61         let lint = self.struct_generic(
62             tcx,
63             message,
64             Some(lint_root),
65         );
66         if let Some(mut lint) = lint {
67             lint.emit();
68         }
69     }
70
71     fn struct_generic(
72         &self,
73         tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
74         message: &str,
75         lint_root: Option<ast::NodeId>,
76     ) -> Option<DiagnosticBuilder<'tcx>> {
77         match self.error.kind {
78             ::mir::interpret::EvalErrorKind::TypeckError |
79             ::mir::interpret::EvalErrorKind::TooGeneric |
80             ::mir::interpret::EvalErrorKind::CheckMatchError |
81             ::mir::interpret::EvalErrorKind::Layout(_) => return None,
82             ::mir::interpret::EvalErrorKind::ReferencedConstant(ref inner) => {
83                 inner.struct_generic(tcx, "referenced constant has errors", lint_root)?.emit();
84             },
85             _ => {},
86         }
87         trace!("reporting const eval failure at {:?}", self.span);
88         let mut err = if let Some(lint_root) = lint_root {
89             let node_id = self.stacktrace
90                 .iter()
91                 .rev()
92                 .filter_map(|frame| frame.lint_root)
93                 .next()
94                 .unwrap_or(lint_root);
95             tcx.struct_span_lint_node(
96                 ::rustc::lint::builtin::CONST_ERR,
97                 node_id,
98                 tcx.span,
99                 message,
100             )
101         } else {
102             struct_error(tcx, message)
103         };
104         err.span_label(self.span, self.error.to_string());
105         for FrameInfo { span, location, .. } in &self.stacktrace {
106             err.span_label(*span, format!("inside call to `{}`", location));
107         }
108         Some(err)
109     }
110 }
111
112 pub fn struct_error<'a, 'gcx, 'tcx>(
113     tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
114     msg: &str,
115 ) -> DiagnosticBuilder<'tcx> {
116     struct_span_err!(tcx.sess, tcx.span, E0080, "{}", msg)
117 }
118
119 #[derive(Debug, Clone, RustcEncodable, RustcDecodable)]
120 pub struct EvalError<'tcx> {
121     pub kind: EvalErrorKind<'tcx, u64>,
122 }
123
124 impl<'tcx> From<EvalErrorKind<'tcx, u64>> for EvalError<'tcx> {
125     fn from(kind: EvalErrorKind<'tcx, u64>) -> Self {
126         match env::var("MIRI_BACKTRACE") {
127             Ok(ref val) if !val.is_empty() => {
128                 let backtrace = Backtrace::new();
129
130                 use std::fmt::Write;
131                 let mut trace_text = "\n\nAn error occurred in miri:\n".to_string();
132                 write!(trace_text, "backtrace frames: {}\n", backtrace.frames().len()).unwrap();
133                 'frames: for (i, frame) in backtrace.frames().iter().enumerate() {
134                     if frame.symbols().is_empty() {
135                         write!(trace_text, "{}: no symbols\n", i).unwrap();
136                     }
137                     for symbol in frame.symbols() {
138                         write!(trace_text, "{}: ", i).unwrap();
139                         if let Some(name) = symbol.name() {
140                             write!(trace_text, "{}\n", name).unwrap();
141                         } else {
142                             write!(trace_text, "<unknown>\n").unwrap();
143                         }
144                         write!(trace_text, "\tat ").unwrap();
145                         if let Some(file_path) = symbol.filename() {
146                             write!(trace_text, "{}", file_path.display()).unwrap();
147                         } else {
148                             write!(trace_text, "<unknown_file>").unwrap();
149                         }
150                         if let Some(line) = symbol.lineno() {
151                             write!(trace_text, ":{}\n", line).unwrap();
152                         } else {
153                             write!(trace_text, "\n").unwrap();
154                         }
155                     }
156                 }
157                 error!("{}", trace_text);
158             },
159             _ => {},
160         }
161         EvalError {
162             kind,
163         }
164     }
165 }
166
167 pub type AssertMessage<'tcx> = EvalErrorKind<'tcx, mir::Operand<'tcx>>;
168
169 #[derive(Clone, RustcEncodable, RustcDecodable)]
170 pub enum EvalErrorKind<'tcx, O> {
171     /// This variant is used by machines to signal their own errors that do not
172     /// match an existing variant
173     MachineError(String),
174     FunctionPointerTyMismatch(FnSig<'tcx>, FnSig<'tcx>),
175     NoMirFor(String),
176     UnterminatedCString(Pointer),
177     DanglingPointerDeref,
178     DoubleFree,
179     InvalidMemoryAccess,
180     InvalidFunctionPointer,
181     InvalidBool,
182     InvalidDiscriminant,
183     PointerOutOfBounds {
184         ptr: Pointer,
185         access: bool,
186         allocation_size: Size,
187     },
188     InvalidNullPointerUsage,
189     ReadPointerAsBytes,
190     ReadBytesAsPointer,
191     ReadForeignStatic,
192     InvalidPointerMath,
193     ReadUndefBytes,
194     DeadLocal,
195     InvalidBoolOp(mir::BinOp),
196     Unimplemented(String),
197     DerefFunctionPointer,
198     ExecuteMemory,
199     BoundsCheck { len: O, index: O },
200     Overflow(mir::BinOp),
201     OverflowNeg,
202     DivisionByZero,
203     RemainderByZero,
204     Intrinsic(String),
205     InvalidChar(u128),
206     StackFrameLimitReached,
207     OutOfTls,
208     TlsOutOfBounds,
209     AbiViolation(String),
210     AlignmentCheckFailed {
211         required: Align,
212         has: Align,
213     },
214     MemoryLockViolation {
215         ptr: Pointer,
216         len: u64,
217         frame: usize,
218         access: AccessKind,
219         lock: Lock,
220     },
221     MemoryAcquireConflict {
222         ptr: Pointer,
223         len: u64,
224         kind: AccessKind,
225         lock: Lock,
226     },
227     InvalidMemoryLockRelease {
228         ptr: Pointer,
229         len: u64,
230         frame: usize,
231         lock: Lock,
232     },
233     DeallocatedLockedMemory {
234         ptr: Pointer,
235         lock: Lock,
236     },
237     ValidationFailure(String),
238     CalledClosureAsFunction,
239     VtableForArgumentlessMethod,
240     ModifiedConstantMemory,
241     AssumptionNotHeld,
242     InlineAsm,
243     TypeNotPrimitive(Ty<'tcx>),
244     ReallocatedWrongMemoryKind(String, String),
245     DeallocatedWrongMemoryKind(String, String),
246     ReallocateNonBasePtr,
247     DeallocateNonBasePtr,
248     IncorrectAllocationInformation(Size, Size, Align, Align),
249     Layout(layout::LayoutError<'tcx>),
250     HeapAllocZeroBytes,
251     HeapAllocNonPowerOfTwoAlignment(u64),
252     Unreachable,
253     Panic,
254     ReadFromReturnPointer,
255     PathNotFound(Vec<String>),
256     UnimplementedTraitSelection,
257     /// Abort in case type errors are reached
258     TypeckError,
259     /// Resolution can fail if we are in a too generic context
260     TooGeneric,
261     CheckMatchError,
262     /// Cannot compute this constant because it depends on another one
263     /// which already produced an error
264     ReferencedConstant(Lrc<ConstEvalErr<'tcx>>),
265     GeneratorResumedAfterReturn,
266     GeneratorResumedAfterPanic,
267     InfiniteLoop,
268 }
269
270 pub type EvalResult<'tcx, T = ()> = Result<T, EvalError<'tcx>>;
271
272 impl<'tcx, O> EvalErrorKind<'tcx, O> {
273     pub fn description(&self) -> &str {
274         use self::EvalErrorKind::*;
275         match *self {
276             MachineError(ref inner) => inner,
277             FunctionPointerTyMismatch(..) =>
278                 "tried to call a function through a function pointer of a different type",
279             InvalidMemoryAccess =>
280                 "tried to access memory through an invalid pointer",
281             DanglingPointerDeref =>
282                 "dangling pointer was dereferenced",
283             DoubleFree =>
284                 "tried to deallocate dangling pointer",
285             InvalidFunctionPointer =>
286                 "tried to use a function pointer after offsetting it",
287             InvalidBool =>
288                 "invalid boolean value read",
289             InvalidDiscriminant =>
290                 "invalid enum discriminant value read",
291             PointerOutOfBounds { .. } =>
292                 "pointer offset outside bounds of allocation",
293             InvalidNullPointerUsage =>
294                 "invalid use of NULL pointer",
295             MemoryLockViolation { .. } =>
296                 "memory access conflicts with lock",
297             MemoryAcquireConflict { .. } =>
298                 "new memory lock conflicts with existing lock",
299             ValidationFailure(..) =>
300                 "type validation failed",
301             InvalidMemoryLockRelease { .. } =>
302                 "invalid attempt to release write lock",
303             DeallocatedLockedMemory { .. } =>
304                 "tried to deallocate memory in conflict with a lock",
305             ReadPointerAsBytes =>
306                 "a raw memory access tried to access part of a pointer value as raw bytes",
307             ReadBytesAsPointer =>
308                 "a memory access tried to interpret some bytes as a pointer",
309             ReadForeignStatic =>
310                 "tried to read from foreign (extern) static",
311             InvalidPointerMath =>
312                 "attempted to do invalid arithmetic on pointers that would leak base addresses, e.g. comparing pointers into different allocations",
313             ReadUndefBytes =>
314                 "attempted to read undefined bytes",
315             DeadLocal =>
316                 "tried to access a dead local variable",
317             InvalidBoolOp(_) =>
318                 "invalid boolean operation",
319             Unimplemented(ref msg) => msg,
320             DerefFunctionPointer =>
321                 "tried to dereference a function pointer",
322             ExecuteMemory =>
323                 "tried to treat a memory pointer as a function pointer",
324             BoundsCheck{..} =>
325                 "array index out of bounds",
326             Intrinsic(..) =>
327                 "intrinsic failed",
328             NoMirFor(..) =>
329                 "mir not found",
330             InvalidChar(..) =>
331                 "tried to interpret an invalid 32-bit value as a char",
332             StackFrameLimitReached =>
333                 "reached the configured maximum number of stack frames",
334             OutOfTls =>
335                 "reached the maximum number of representable TLS keys",
336             TlsOutOfBounds =>
337                 "accessed an invalid (unallocated) TLS key",
338             AbiViolation(ref msg) => msg,
339             AlignmentCheckFailed{..} =>
340                 "tried to execute a misaligned read or write",
341             CalledClosureAsFunction =>
342                 "tried to call a closure through a function pointer",
343             VtableForArgumentlessMethod =>
344                 "tried to call a vtable function without arguments",
345             ModifiedConstantMemory =>
346                 "tried to modify constant memory",
347             AssumptionNotHeld =>
348                 "`assume` argument was false",
349             InlineAsm =>
350                 "miri does not support inline assembly",
351             TypeNotPrimitive(_) =>
352                 "expected primitive type, got nonprimitive",
353             ReallocatedWrongMemoryKind(_, _) =>
354                 "tried to reallocate memory from one kind to another",
355             DeallocatedWrongMemoryKind(_, _) =>
356                 "tried to deallocate memory of the wrong kind",
357             ReallocateNonBasePtr =>
358                 "tried to reallocate with a pointer not to the beginning of an existing object",
359             DeallocateNonBasePtr =>
360                 "tried to deallocate with a pointer not to the beginning of an existing object",
361             IncorrectAllocationInformation(..) =>
362                 "tried to deallocate or reallocate using incorrect alignment or size",
363             Layout(_) =>
364                 "rustc layout computation failed",
365             UnterminatedCString(_) =>
366                 "attempted to get length of a null terminated string, but no null found before end of allocation",
367             HeapAllocZeroBytes =>
368                 "tried to re-, de- or allocate zero bytes on the heap",
369             HeapAllocNonPowerOfTwoAlignment(_) =>
370                 "tried to re-, de-, or allocate heap memory with alignment that is not a power of two",
371             Unreachable =>
372                 "entered unreachable code",
373             Panic =>
374                 "the evaluated program panicked",
375             ReadFromReturnPointer =>
376                 "tried to read from the return pointer",
377             PathNotFound(_) =>
378                 "a path could not be resolved, maybe the crate is not loaded",
379             UnimplementedTraitSelection =>
380                 "there were unresolved type arguments during trait selection",
381             TypeckError =>
382                 "encountered constants with type errors, stopping evaluation",
383             TooGeneric =>
384                 "encountered overly generic constant",
385             CheckMatchError =>
386                 "match checking failed",
387             ReferencedConstant(_) =>
388                 "referenced constant has errors",
389             Overflow(mir::BinOp::Add) => "attempt to add with overflow",
390             Overflow(mir::BinOp::Sub) => "attempt to subtract with overflow",
391             Overflow(mir::BinOp::Mul) => "attempt to multiply with overflow",
392             Overflow(mir::BinOp::Div) => "attempt to divide with overflow",
393             Overflow(mir::BinOp::Rem) => "attempt to calculate the remainder with overflow",
394             OverflowNeg => "attempt to negate with overflow",
395             Overflow(mir::BinOp::Shr) => "attempt to shift right with overflow",
396             Overflow(mir::BinOp::Shl) => "attempt to shift left with overflow",
397             Overflow(op) => bug!("{:?} cannot overflow", op),
398             DivisionByZero => "attempt to divide by zero",
399             RemainderByZero => "attempt to calculate the remainder with a divisor of zero",
400             GeneratorResumedAfterReturn => "generator resumed after completion",
401             GeneratorResumedAfterPanic => "generator resumed after panicking",
402             InfiniteLoop =>
403                 "duplicate interpreter state observed here, const evaluation will never terminate",
404         }
405     }
406 }
407
408 impl<'tcx> fmt::Display for EvalError<'tcx> {
409     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
410         write!(f, "{:?}", self.kind)
411     }
412 }
413
414 impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> {
415     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
416         use self::EvalErrorKind::*;
417         match *self {
418             PointerOutOfBounds { ptr, access, allocation_size } => {
419                 write!(f, "{} at offset {}, outside bounds of allocation {} which has size {}",
420                        if access { "memory access" } else { "pointer computed" },
421                        ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
422             },
423             MemoryLockViolation { ptr, len, frame, access, ref lock } => {
424                 write!(f, "{:?} access by frame {} at {:?}, size {}, is in conflict with lock {:?}",
425                        access, frame, ptr, len, lock)
426             }
427             MemoryAcquireConflict { ptr, len, kind, ref lock } => {
428                 write!(f, "new {:?} lock at {:?}, size {}, is in conflict with lock {:?}",
429                        kind, ptr, len, lock)
430             }
431             InvalidMemoryLockRelease { ptr, len, frame, ref lock } => {
432                 write!(f, "frame {} tried to release memory write lock at {:?}, size {}, but cannot release lock {:?}",
433                        frame, ptr, len, lock)
434             }
435             DeallocatedLockedMemory { ptr, ref lock } => {
436                 write!(f, "tried to deallocate memory at {:?} in conflict with lock {:?}",
437                        ptr, lock)
438             }
439             ValidationFailure(ref err) => {
440                 write!(f, "type validation failed: {}", err)
441             }
442             NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
443             FunctionPointerTyMismatch(sig, got) =>
444                 write!(f, "tried to call a function with sig {} through a function pointer of type {}", sig, got),
445             BoundsCheck { ref len, ref index } =>
446                 write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index),
447             ReallocatedWrongMemoryKind(ref old, ref new) =>
448                 write!(f, "tried to reallocate memory from {} to {}", old, new),
449             DeallocatedWrongMemoryKind(ref old, ref new) =>
450                 write!(f, "tried to deallocate {} memory but gave {} as the kind", old, new),
451             Intrinsic(ref err) =>
452                 write!(f, "{}", err),
453             InvalidChar(c) =>
454                 write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c),
455             AlignmentCheckFailed { required, has } =>
456                write!(f, "tried to access memory with alignment {}, but alignment {} is required",
457                       has.abi(), required.abi()),
458             TypeNotPrimitive(ty) =>
459                 write!(f, "expected primitive type, got {}", ty),
460             Layout(ref err) =>
461                 write!(f, "rustc layout computation failed: {:?}", err),
462             PathNotFound(ref path) =>
463                 write!(f, "Cannot find path {:?}", path),
464             MachineError(ref inner) =>
465                 write!(f, "{}", inner),
466             IncorrectAllocationInformation(size, size2, align, align2) =>
467                 write!(f, "incorrect alloc info: expected size {} and align {}, got size {} and align {}", size.bytes(), align.abi(), size2.bytes(), align2.abi()),
468             _ => write!(f, "{}", self.description()),
469         }
470     }
471 }