]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/error.rs
Minor refactoring.
[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 }
268
269 pub type EvalResult<'tcx, T = ()> = Result<T, EvalError<'tcx>>;
270
271 impl<'tcx, O> EvalErrorKind<'tcx, O> {
272     pub fn description(&self) -> &str {
273         use self::EvalErrorKind::*;
274         match *self {
275             MachineError(ref inner) => inner,
276             FunctionPointerTyMismatch(..) =>
277                 "tried to call a function through a function pointer of a different type",
278             InvalidMemoryAccess =>
279                 "tried to access memory through an invalid pointer",
280             DanglingPointerDeref =>
281                 "dangling pointer was dereferenced",
282             DoubleFree =>
283                 "tried to deallocate dangling pointer",
284             InvalidFunctionPointer =>
285                 "tried to use a function pointer after offsetting it",
286             InvalidBool =>
287                 "invalid boolean value read",
288             InvalidDiscriminant =>
289                 "invalid enum discriminant value read",
290             PointerOutOfBounds { .. } =>
291                 "pointer offset outside bounds of allocation",
292             InvalidNullPointerUsage =>
293                 "invalid use of NULL pointer",
294             MemoryLockViolation { .. } =>
295                 "memory access conflicts with lock",
296             MemoryAcquireConflict { .. } =>
297                 "new memory lock conflicts with existing lock",
298             ValidationFailure(..) =>
299                 "type validation failed",
300             InvalidMemoryLockRelease { .. } =>
301                 "invalid attempt to release write lock",
302             DeallocatedLockedMemory { .. } =>
303                 "tried to deallocate memory in conflict with a lock",
304             ReadPointerAsBytes =>
305                 "a raw memory access tried to access part of a pointer value as raw bytes",
306             ReadBytesAsPointer =>
307                 "a memory access tried to interpret some bytes as a pointer",
308             ReadForeignStatic =>
309                 "tried to read from foreign (extern) static",
310             InvalidPointerMath =>
311                 "attempted to do invalid arithmetic on pointers that would leak base addresses, e.g. comparing pointers into different allocations",
312             ReadUndefBytes =>
313                 "attempted to read undefined bytes",
314             DeadLocal =>
315                 "tried to access a dead local variable",
316             InvalidBoolOp(_) =>
317                 "invalid boolean operation",
318             Unimplemented(ref msg) => msg,
319             DerefFunctionPointer =>
320                 "tried to dereference a function pointer",
321             ExecuteMemory =>
322                 "tried to treat a memory pointer as a function pointer",
323             BoundsCheck{..} =>
324                 "array index out of bounds",
325             Intrinsic(..) =>
326                 "intrinsic failed",
327             NoMirFor(..) =>
328                 "mir not found",
329             InvalidChar(..) =>
330                 "tried to interpret an invalid 32-bit value as a char",
331             StackFrameLimitReached =>
332                 "reached the configured maximum number of stack frames",
333             OutOfTls =>
334                 "reached the maximum number of representable TLS keys",
335             TlsOutOfBounds =>
336                 "accessed an invalid (unallocated) TLS key",
337             AbiViolation(ref msg) => msg,
338             AlignmentCheckFailed{..} =>
339                 "tried to execute a misaligned read or write",
340             CalledClosureAsFunction =>
341                 "tried to call a closure through a function pointer",
342             VtableForArgumentlessMethod =>
343                 "tried to call a vtable function without arguments",
344             ModifiedConstantMemory =>
345                 "tried to modify constant memory",
346             AssumptionNotHeld =>
347                 "`assume` argument was false",
348             InlineAsm =>
349                 "miri does not support inline assembly",
350             TypeNotPrimitive(_) =>
351                 "expected primitive type, got nonprimitive",
352             ReallocatedWrongMemoryKind(_, _) =>
353                 "tried to reallocate memory from one kind to another",
354             DeallocatedWrongMemoryKind(_, _) =>
355                 "tried to deallocate memory of the wrong kind",
356             ReallocateNonBasePtr =>
357                 "tried to reallocate with a pointer not to the beginning of an existing object",
358             DeallocateNonBasePtr =>
359                 "tried to deallocate with a pointer not to the beginning of an existing object",
360             IncorrectAllocationInformation(..) =>
361                 "tried to deallocate or reallocate using incorrect alignment or size",
362             Layout(_) =>
363                 "rustc layout computation failed",
364             UnterminatedCString(_) =>
365                 "attempted to get length of a null terminated string, but no null found before end of allocation",
366             HeapAllocZeroBytes =>
367                 "tried to re-, de- or allocate zero bytes on the heap",
368             HeapAllocNonPowerOfTwoAlignment(_) =>
369                 "tried to re-, de-, or allocate heap memory with alignment that is not a power of two",
370             Unreachable =>
371                 "entered unreachable code",
372             Panic =>
373                 "the evaluated program panicked",
374             ReadFromReturnPointer =>
375                 "tried to read from the return pointer",
376             PathNotFound(_) =>
377                 "a path could not be resolved, maybe the crate is not loaded",
378             UnimplementedTraitSelection =>
379                 "there were unresolved type arguments during trait selection",
380             TypeckError =>
381                 "encountered constants with type errors, stopping evaluation",
382             TooGeneric =>
383                 "encountered overly generic constant",
384             CheckMatchError =>
385                 "match checking failed",
386             ReferencedConstant(_) =>
387                 "referenced constant has errors",
388             Overflow(mir::BinOp::Add) => "attempt to add with overflow",
389             Overflow(mir::BinOp::Sub) => "attempt to subtract with overflow",
390             Overflow(mir::BinOp::Mul) => "attempt to multiply with overflow",
391             Overflow(mir::BinOp::Div) => "attempt to divide with overflow",
392             Overflow(mir::BinOp::Rem) => "attempt to calculate the remainder with overflow",
393             OverflowNeg => "attempt to negate with overflow",
394             Overflow(mir::BinOp::Shr) => "attempt to shift right with overflow",
395             Overflow(mir::BinOp::Shl) => "attempt to shift left with overflow",
396             Overflow(op) => bug!("{:?} cannot overflow", op),
397             DivisionByZero => "attempt to divide by zero",
398             RemainderByZero => "attempt to calculate the remainder with a divisor of zero",
399             GeneratorResumedAfterReturn => "generator resumed after completion",
400             GeneratorResumedAfterPanic => "generator resumed after panicking",
401         }
402     }
403 }
404
405 impl<'tcx> fmt::Display for EvalError<'tcx> {
406     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
407         write!(f, "{:?}", self.kind)
408     }
409 }
410
411 impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> {
412     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
413         use self::EvalErrorKind::*;
414         match *self {
415             PointerOutOfBounds { ptr, access, allocation_size } => {
416                 write!(f, "{} at offset {}, outside bounds of allocation {} which has size {}",
417                        if access { "memory access" } else { "pointer computed" },
418                        ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
419             },
420             MemoryLockViolation { ptr, len, frame, access, ref lock } => {
421                 write!(f, "{:?} access by frame {} at {:?}, size {}, is in conflict with lock {:?}",
422                        access, frame, ptr, len, lock)
423             }
424             MemoryAcquireConflict { ptr, len, kind, ref lock } => {
425                 write!(f, "new {:?} lock at {:?}, size {}, is in conflict with lock {:?}",
426                        kind, ptr, len, lock)
427             }
428             InvalidMemoryLockRelease { ptr, len, frame, ref lock } => {
429                 write!(f, "frame {} tried to release memory write lock at {:?}, size {}, but cannot release lock {:?}",
430                        frame, ptr, len, lock)
431             }
432             DeallocatedLockedMemory { ptr, ref lock } => {
433                 write!(f, "tried to deallocate memory at {:?} in conflict with lock {:?}",
434                        ptr, lock)
435             }
436             ValidationFailure(ref err) => {
437                 write!(f, "type validation failed: {}", err)
438             }
439             NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
440             FunctionPointerTyMismatch(sig, got) =>
441                 write!(f, "tried to call a function with sig {} through a function pointer of type {}", sig, got),
442             BoundsCheck { ref len, ref index } =>
443                 write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index),
444             ReallocatedWrongMemoryKind(ref old, ref new) =>
445                 write!(f, "tried to reallocate memory from {} to {}", old, new),
446             DeallocatedWrongMemoryKind(ref old, ref new) =>
447                 write!(f, "tried to deallocate {} memory but gave {} as the kind", old, new),
448             Intrinsic(ref err) =>
449                 write!(f, "{}", err),
450             InvalidChar(c) =>
451                 write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c),
452             AlignmentCheckFailed { required, has } =>
453                write!(f, "tried to access memory with alignment {}, but alignment {} is required",
454                       has.abi(), required.abi()),
455             TypeNotPrimitive(ty) =>
456                 write!(f, "expected primitive type, got {}", ty),
457             Layout(ref err) =>
458                 write!(f, "rustc layout computation failed: {:?}", err),
459             PathNotFound(ref path) =>
460                 write!(f, "Cannot find path {:?}", path),
461             MachineError(ref inner) =>
462                 write!(f, "{}", inner),
463             IncorrectAllocationInformation(size, size2, align, align2) =>
464                 write!(f, "incorrect alloc info: expected size {} and align {}, got size {} and align {}", size.bytes(), align.abi(), size2.bytes(), align2.abi()),
465             _ => write!(f, "{}", self.description()),
466         }
467     }
468 }