1 use std::cell::RefCell;
6 use rustc_span::DUMMY_SP;
10 /// Details of premature program termination.
11 pub enum TerminationInfo {
13 Abort(Option<String>),
14 UnsupportedInIsolation(String),
15 ExperimentalUb { msg: String, url: String }
18 impl fmt::Debug for TerminationInfo {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 use TerminationInfo::*;
23 write!(f, "the evaluated program completed with exit code {}", code),
25 write!(f, "the evaluated program aborted execution"),
27 write!(f, "the evaluated program aborted execution: {}", msg),
28 UnsupportedInIsolation(msg) =>
30 ExperimentalUb { msg, .. } =>
36 impl MachineStopType for TerminationInfo {}
38 /// Miri specific diagnostics
39 pub enum NonHaltingDiagnostic {
40 PoppedTrackedPointerTag(Item),
41 CreatedAlloc(AllocId),
44 /// Emit a custom diagnostic without going through the miri-engine machinery
45 pub fn report_error<'tcx, 'mir>(
46 ecx: &InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
47 mut e: InterpErrorInfo<'tcx>,
51 let (title, helps) = match e.kind {
52 MachineStop(ref info) => {
53 let info = info.downcast_ref::<TerminationInfo>().expect("invalid MachineStop payload");
54 use TerminationInfo::*;
55 let title = match info {
56 Exit(code) => return Some(*code),
58 "abnormal termination",
59 UnsupportedInIsolation(_) =>
60 "unsupported operation",
61 ExperimentalUb { .. } =>
64 let helps = match info {
65 UnsupportedInIsolation(_) =>
66 vec![format!("pass the flag `-Zmiri-disable-isolation` to disable isolation")],
67 ExperimentalUb { url, .. } =>
69 format!("this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental"),
70 format!("see {} for further information", url),
77 let title = match e.kind {
79 "unsupported operation",
80 UndefinedBehavior(_) =>
82 ResourceExhaustion(_) =>
83 "resource exhaustion",
85 bug!("This error should be impossible in Miri: {}", e),
87 let helps = match e.kind {
88 Unsupported(UnsupportedOpInfo::NoMirFor(..)) =>
89 vec![format!("make sure to use a Miri sysroot, which you can prepare with `cargo miri setup`")],
91 vec![format!("this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support")],
92 UndefinedBehavior(_) =>
94 format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior"),
95 format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information"),
104 let msg = e.to_string();
105 report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true)
108 /// Report an error or note (depending on the `error` argument) at the current frame's current statement.
109 /// Also emits a full stacktrace of the interpreter stack.
110 fn report_msg<'tcx, 'mir>(
111 ecx: &InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
114 mut helps: Vec<String>,
117 let span = if let Some(frame) = ecx.stack().last() {
118 frame.current_source_info().unwrap().span
122 let mut err = if error {
123 ecx.tcx.sess.struct_span_err(span, title)
125 ecx.tcx.sess.diagnostic().span_note_diag(span, title)
127 err.span_label(span, span_msg);
128 if !helps.is_empty() {
129 // Add visual separator before backtrace.
130 helps.last_mut().unwrap().push_str("\n");
136 let frames = ecx.generate_stacktrace();
137 for (idx, frame_info) in frames.iter().enumerate() {
138 let is_local = frame_info.instance.def_id().is_local();
139 // No span for non-local frames and the first frame (which is the error site).
140 if is_local && idx > 0 {
141 err.span_note(frame_info.span, &frame_info.to_string());
143 err.note(&frame_info.to_string());
149 for (i, frame) in ecx.stack().iter().enumerate() {
150 trace!("-------------------");
151 trace!("Frame {}", i);
152 trace!(" return: {:?}", frame.return_place.map(|p| *p));
153 for (i, local) in frame.locals.iter().enumerate() {
154 trace!(" local {}: {:?}", i, local.value);
157 // Let the reported error determine the return code.
162 static DIAGNOSTICS: RefCell<Vec<NonHaltingDiagnostic>> = RefCell::new(Vec::new());
165 /// Schedule a diagnostic for emitting. This function works even if you have no `InterpCx` available.
166 /// The diagnostic will be emitted after the current interpreter step is finished.
167 pub fn register_diagnostic(e: NonHaltingDiagnostic) {
168 DIAGNOSTICS.with(|diagnostics| diagnostics.borrow_mut().push(e));
171 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
172 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
173 /// Emit all diagnostics that were registed with `register_diagnostics`
174 fn process_diagnostics(&self) {
175 let this = self.eval_context_ref();
176 DIAGNOSTICS.with(|diagnostics| {
177 for e in diagnostics.borrow_mut().drain(..) {
178 use NonHaltingDiagnostic::*;
180 PoppedTrackedPointerTag(item) =>
181 format!("popped tracked tag for item {:?}", item),
182 CreatedAlloc(AllocId(id)) =>
183 format!("created allocation with id {}", id),
185 report_msg(this, "tracking was triggered", msg, vec![], false);