2020-01-08 13:02:55 +01:00
|
|
|
use std::cell::RefCell;
|
2019-12-24 00:11:40 +01:00
|
|
|
|
2020-03-22 18:50:12 +01:00
|
|
|
use rustc_span::DUMMY_SP;
|
|
|
|
|
2019-12-24 00:11:40 +01:00
|
|
|
use crate::*;
|
|
|
|
|
2020-03-22 19:48:59 +01:00
|
|
|
/// Details of premature program termination.
|
|
|
|
pub enum TerminationInfo {
|
|
|
|
Exit(i64),
|
|
|
|
Abort(Option<String>),
|
|
|
|
UnsupportedInIsolation(String),
|
|
|
|
}
|
|
|
|
|
2020-01-09 12:42:56 +01:00
|
|
|
/// Miri specific diagnostics
|
2020-01-08 13:02:55 +01:00
|
|
|
pub enum NonHaltingDiagnostic {
|
|
|
|
PoppedTrackedPointerTag(Item),
|
2020-03-06 09:11:41 +01:00
|
|
|
CreatedAlloc(AllocId),
|
2020-01-08 13:02:55 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 12:42:56 +01:00
|
|
|
/// Emit a custom diagnostic without going through the miri-engine machinery
|
2020-03-22 19:48:59 +01:00
|
|
|
pub fn report_error<'tcx, 'mir>(
|
2019-12-24 00:11:40 +01:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
|
|
|
|
mut e: InterpErrorInfo<'tcx>,
|
|
|
|
) -> Option<i64> {
|
2020-03-22 18:50:12 +01:00
|
|
|
use InterpError::*;
|
2020-03-22 19:48:59 +01:00
|
|
|
|
|
|
|
e.print_backtrace();
|
|
|
|
let (title, msg, help) = match e.kind {
|
|
|
|
MachineStop(info) => {
|
2019-12-24 00:11:40 +01:00
|
|
|
let info = info.downcast_ref::<TerminationInfo>().expect("invalid MachineStop payload");
|
2020-03-22 19:48:59 +01:00
|
|
|
use TerminationInfo::*;
|
|
|
|
let (title, msg) = match info {
|
|
|
|
Exit(code) => return Some(*code),
|
|
|
|
Abort(None) =>
|
|
|
|
("abnormal termination", format!("the evaluated program aborted execution")),
|
|
|
|
Abort(Some(msg)) =>
|
|
|
|
("abnormal termination", format!("the evaluated program aborted execution: {}", msg)),
|
|
|
|
UnsupportedInIsolation(msg) =>
|
|
|
|
("unsupported operation", format!("{}", msg)),
|
|
|
|
};
|
|
|
|
let help = match info {
|
|
|
|
UnsupportedInIsolation(_) =>
|
|
|
|
Some("pass the flag `-Zmiri-disable-isolation` to disable isolation"),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
(title, msg, help)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let (title, msg) = match e.kind {
|
|
|
|
Unsupported(_) =>
|
|
|
|
("unsupported operation", e.to_string()),
|
|
|
|
UndefinedBehavior(_) =>
|
|
|
|
("Undefined Behavior", e.to_string()),
|
|
|
|
ResourceExhaustion(_) =>
|
|
|
|
("resource exhaustion", e.to_string()),
|
|
|
|
_ =>
|
|
|
|
bug!("This error should be impossible in Miri: {}", e),
|
|
|
|
};
|
|
|
|
let help = match e.kind {
|
|
|
|
Unsupported(UnsupportedOpInfo::NoMirFor(..)) =>
|
2020-03-22 20:09:14 +01:00
|
|
|
Some("make sure to use a Miri sysroot, which you can prepare with `cargo miri setup`"),
|
2020-03-22 19:48:59 +01:00
|
|
|
Unsupported(_) =>
|
|
|
|
Some("this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support"),
|
|
|
|
UndefinedBehavior(UndefinedBehaviorInfo::UbExperimental(_)) =>
|
|
|
|
Some("this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental"),
|
|
|
|
UndefinedBehavior(_) =>
|
|
|
|
Some("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior"),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
(title, msg, help)
|
2019-12-24 00:11:40 +01:00
|
|
|
}
|
2020-03-22 18:50:12 +01:00
|
|
|
};
|
|
|
|
report_msg(ecx, &format!("{}: {}", title, msg), msg, help, true)
|
2020-01-08 13:02:55 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 12:42:56 +01:00
|
|
|
/// Report an error or note (depending on the `error` argument) at the current frame's current statement.
|
|
|
|
/// Also emits a full stacktrace of the interpreter stack.
|
2020-03-22 19:48:59 +01:00
|
|
|
fn report_msg<'tcx, 'mir>(
|
2020-01-08 13:02:55 +01:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
|
2020-03-22 18:50:12 +01:00
|
|
|
title: &str,
|
|
|
|
span_msg: String,
|
|
|
|
help: Option<&str>,
|
2020-01-08 13:02:55 +01:00
|
|
|
error: bool,
|
|
|
|
) -> Option<i64> {
|
2020-03-22 18:50:12 +01:00
|
|
|
let span = if let Some(frame) = ecx.stack().last() {
|
|
|
|
frame.current_source_info().unwrap().span
|
|
|
|
} else {
|
|
|
|
DUMMY_SP
|
|
|
|
};
|
|
|
|
let mut err = if error {
|
|
|
|
ecx.tcx.sess.struct_span_err(span, title)
|
|
|
|
} else {
|
|
|
|
ecx.tcx.sess.diagnostic().span_note_diag(span, title)
|
|
|
|
};
|
|
|
|
err.span_label(span, span_msg);
|
|
|
|
if let Some(help) = help {
|
|
|
|
err.help(help);
|
|
|
|
}
|
|
|
|
// Add backtrace
|
|
|
|
let frames = ecx.generate_stacktrace(None);
|
|
|
|
// We iterate with indices because we need to look at the next frame (the caller).
|
|
|
|
for idx in 0..frames.len() {
|
|
|
|
let frame_info = &frames[idx];
|
|
|
|
let call_site_is_local = frames
|
|
|
|
.get(idx + 1)
|
|
|
|
.map_or(false, |caller_info| caller_info.instance.def_id().is_local());
|
|
|
|
if call_site_is_local {
|
|
|
|
err.span_note(frame_info.call_site, &frame_info.to_string());
|
2020-01-08 13:02:55 +01:00
|
|
|
} else {
|
2020-03-22 18:50:12 +01:00
|
|
|
err.note(&frame_info.to_string());
|
2019-12-24 00:11:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:50:12 +01:00
|
|
|
err.emit();
|
|
|
|
|
2019-12-24 00:11:40 +01:00
|
|
|
for (i, frame) in ecx.stack().iter().enumerate() {
|
|
|
|
trace!("-------------------");
|
|
|
|
trace!("Frame {}", i);
|
|
|
|
trace!(" return: {:?}", frame.return_place.map(|p| *p));
|
|
|
|
for (i, local) in frame.locals.iter().enumerate() {
|
|
|
|
trace!(" local {}: {:?}", i, local.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Let the reported error determine the return code.
|
|
|
|
return None;
|
|
|
|
}
|
2019-12-24 00:22:32 +01:00
|
|
|
|
|
|
|
thread_local! {
|
2020-01-08 13:20:39 +01:00
|
|
|
static DIAGNOSTICS: RefCell<Vec<NonHaltingDiagnostic>> = RefCell::new(Vec::new());
|
2019-12-24 00:22:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 12:42:56 +01:00
|
|
|
/// Schedule a diagnostic for emitting. This function works even if you have no `InterpCx` available.
|
|
|
|
/// The diagnostic will be emitted after the current interpreter step is finished.
|
2020-01-08 13:20:39 +01:00
|
|
|
pub fn register_diagnostic(e: NonHaltingDiagnostic) {
|
|
|
|
DIAGNOSTICS.with(|diagnostics| diagnostics.borrow_mut().push(e));
|
2019-12-24 00:22:32 +01:00
|
|
|
}
|
|
|
|
|
2019-12-24 02:04:07 +01:00
|
|
|
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
|
|
|
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
2020-01-09 12:42:56 +01:00
|
|
|
/// Emit all diagnostics that were registed with `register_diagnostics`
|
2020-01-08 13:20:39 +01:00
|
|
|
fn process_diagnostics(&self) {
|
2019-12-24 02:04:07 +01:00
|
|
|
let this = self.eval_context_ref();
|
2020-01-08 13:20:39 +01:00
|
|
|
DIAGNOSTICS.with(|diagnostics| {
|
|
|
|
for e in diagnostics.borrow_mut().drain(..) {
|
2020-03-06 09:11:41 +01:00
|
|
|
use NonHaltingDiagnostic::*;
|
2020-01-08 13:02:55 +01:00
|
|
|
let msg = match e {
|
2020-03-06 09:11:41 +01:00
|
|
|
PoppedTrackedPointerTag(item) =>
|
2020-01-08 13:02:55 +01:00
|
|
|
format!("popped tracked tag for item {:?}", item),
|
2020-03-06 09:11:41 +01:00
|
|
|
CreatedAlloc(AllocId(id)) =>
|
|
|
|
format!("created allocation with id {}", id),
|
2020-01-08 13:02:55 +01:00
|
|
|
};
|
2020-03-22 18:50:12 +01:00
|
|
|
report_msg(this, "tracking was triggered", msg, None, false);
|
2019-12-24 02:04:07 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-12-24 00:22:32 +01:00
|
|
|
}
|