]> git.lizzy.rs Git - rust.git/blobdiff - src/diagnostics.rs
Auto merge of #2195 - RalfJung:vtable-validation, r=RalfJung
[rust.git] / src / diagnostics.rs
index 12ad93a5289ee7524f4c4b19f779a5078dbde312..0e3e693e33f9010cbeca7dc83c608d9de9da1c64 100644 (file)
@@ -4,36 +4,54 @@
 
 use log::trace;
 
-use rustc_middle::ty::{self, TyCtxt};
-use rustc_span::{source_map::DUMMY_SP, Span};
+use rustc_middle::ty;
+use rustc_span::{source_map::DUMMY_SP, Span, SpanData, Symbol};
 
+use crate::helpers::HexRange;
+use crate::stacked_borrows::{diagnostics::TagHistory, AccessKind, SbTag};
 use crate::*;
 
 /// Details of premature program termination.
 pub enum TerminationInfo {
     Exit(i64),
-    Abort(Option<String>),
+    Abort(String),
     UnsupportedInIsolation(String),
-    ExperimentalUb { msg: String, url: String },
+    StackedBorrowsUb {
+        msg: String,
+        help: Option<String>,
+        history: Option<TagHistory>,
+    },
     Deadlock,
+    MultipleSymbolDefinitions {
+        link_name: Symbol,
+        first: SpanData,
+        first_crate: Symbol,
+        second: SpanData,
+        second_crate: Symbol,
+    },
+    SymbolShimClashing {
+        link_name: Symbol,
+        span: SpanData,
+    },
 }
 
 impl fmt::Display for TerminationInfo {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         use TerminationInfo::*;
         match self {
-            Exit(code) =>
-                write!(f, "the evaluated program completed with exit code {}", code),
-            Abort(None) =>
-                write!(f, "the evaluated program aborted execution"),
-            Abort(Some(msg)) =>
-                write!(f, "the evaluated program aborted execution: {}", msg),
-            UnsupportedInIsolation(msg) =>
-                write!(f, "{}", msg),
-            ExperimentalUb { msg, .. } =>
-                write!(f, "{}", msg),
-            Deadlock =>
-                write!(f, "the evaluated program deadlocked"),
+            Exit(code) => write!(f, "the evaluated program completed with exit code {}", code),
+            Abort(msg) => write!(f, "{}", msg),
+            UnsupportedInIsolation(msg) => write!(f, "{}", msg),
+            StackedBorrowsUb { msg, .. } => write!(f, "{}", msg),
+            Deadlock => write!(f, "the evaluated program deadlocked"),
+            MultipleSymbolDefinitions { link_name, .. } =>
+                write!(f, "multiple definitions of symbol `{}`", link_name),
+            SymbolShimClashing { link_name, .. } =>
+                write!(
+                    f,
+                    "found `{}` symbol definition that clashes with a built-in shim",
+                    link_name
+                ),
         }
     }
 }
@@ -43,10 +61,71 @@ impl MachineStopType for TerminationInfo {}
 /// Miri specific diagnostics
 pub enum NonHaltingDiagnostic {
     CreatedPointerTag(NonZeroU64),
-    PoppedPointerTag(Item),
+    /// This `Item` was popped from the borrow stack, either due to a grant of
+    /// `AccessKind` to `SbTag` or a deallocation when the second argument is `None`.
+    PoppedPointerTag(Item, Option<(SbTag, AccessKind)>),
     CreatedCallId(CallId),
     CreatedAlloc(AllocId),
     FreedAlloc(AllocId),
+    RejectedIsolatedOp(String),
+}
+
+/// Level of Miri specific diagnostics
+enum DiagLevel {
+    Error,
+    Warning,
+    Note,
+}
+
+/// Attempts to prune a stacktrace to omit the Rust runtime, and returns a bool indicating if any
+/// frames were pruned. If the stacktrace does not have any local frames, we conclude that it must
+/// be pointing to a problem in the Rust runtime itself, and do not prune it at all.
+fn prune_stacktrace<'mir, 'tcx>(
+    ecx: &InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
+    mut stacktrace: Vec<FrameInfo<'tcx>>,
+) -> (Vec<FrameInfo<'tcx>>, bool) {
+    match ecx.machine.backtrace_style {
+        BacktraceStyle::Off => {
+            // Retain one frame so that we can print a span for the error itself
+            stacktrace.truncate(1);
+            (stacktrace, false)
+        }
+        BacktraceStyle::Short => {
+            let original_len = stacktrace.len();
+            // Only prune frames if there is at least one local frame. This check ensures that if
+            // we get a backtrace that never makes it to the user code because it has detected a
+            // bug in the Rust runtime, we don't prune away every frame.
+            let has_local_frame = stacktrace.iter().any(|frame| ecx.machine.is_local(frame));
+            if has_local_frame {
+                // This is part of the logic that `std` uses to select the relevant part of a
+                // backtrace. But here, we only look for __rust_begin_short_backtrace, not
+                // __rust_end_short_backtrace because the end symbol comes from a call to the default
+                // panic handler.
+                stacktrace = stacktrace
+                    .into_iter()
+                    .take_while(|frame| {
+                        let def_id = frame.instance.def_id();
+                        let path = ecx.tcx.tcx.def_path_str(def_id);
+                        !path.contains("__rust_begin_short_backtrace")
+                    })
+                    .collect::<Vec<_>>();
+
+                // After we prune frames from the bottom, there are a few left that are part of the
+                // Rust runtime. So we remove frames until we get to a local symbol, which should be
+                // main or a test.
+                // This len check ensures that we don't somehow remove every frame, as doing so breaks
+                // the primary error message.
+                while stacktrace.len() > 1
+                    && stacktrace.last().map_or(false, |frame| !ecx.machine.is_local(frame))
+                {
+                    stacktrace.pop();
+                }
+            }
+            let was_pruned = stacktrace.len() != original_len;
+            (stacktrace, was_pruned)
+        }
+        BacktraceStyle::Full => (stacktrace, false),
+    }
 }
 
 /// Emit a custom diagnostic without going through the miri-engine machinery
@@ -56,93 +135,169 @@ pub fn report_error<'tcx, 'mir>(
 ) -> Option<i64> {
     use InterpError::*;
 
-    let (title, helps) = match &e.kind {
+    let mut msg = vec![];
+
+    let (title, helps) = match &e.kind() {
         MachineStop(info) => {
             let info = info.downcast_ref::<TerminationInfo>().expect("invalid MachineStop payload");
             use TerminationInfo::*;
             let title = match info {
                 Exit(code) => return Some(*code),
-                Abort(_) =>
-                    "abnormal termination",
-                UnsupportedInIsolation(_) =>
-                    "unsupported operation",
-                ExperimentalUb { .. } =>
-                    "Undefined Behavior",
-                Deadlock => "deadlock",
+                Abort(_) => Some("abnormal termination"),
+                UnsupportedInIsolation(_) => Some("unsupported operation"),
+                StackedBorrowsUb { .. } => Some("Undefined Behavior"),
+                Deadlock => Some("deadlock"),
+                MultipleSymbolDefinitions { .. } | SymbolShimClashing { .. } => None,
             };
+            #[rustfmt::skip]
             let helps = match info {
                 UnsupportedInIsolation(_) =>
-                    vec![format!("pass the flag `-Zmiri-disable-isolation` to disable isolation")],
-                ExperimentalUb { url, .. } =>
                     vec![
-                        format!("this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental"),
-                        format!("see {} for further information", url),
+                        (None, format!("pass the flag `-Zmiri-disable-isolation` to disable isolation;")),
+                        (None, format!("or pass `-Zmiri-isolation-error=warn` to configure Miri to return an error code from isolated operations (if supported for that operation) and continue with a warning")),
+                    ],
+                StackedBorrowsUb { help, history, .. } => {
+                    let url = "https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md";
+                    msg.extend(help.clone());
+                    let mut helps = vec![
+                        (None, format!("this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental")),
+                        (None, format!("see {url} for further information")),
+                    ];
+                    match history {
+                        Some(TagHistory::Tagged {tag, created: (created_range, created_span), invalidated, protected }) => {
+                            let msg = format!("{:?} was created by a retag at offsets {}", tag, HexRange(*created_range));
+                            helps.push((Some(*created_span), msg));
+                            if let Some((invalidated_range, invalidated_span)) = invalidated {
+                                let msg = format!("{:?} was later invalidated at offsets {}", tag, HexRange(*invalidated_range));
+                                helps.push((Some(*invalidated_span), msg));
+                            }
+                            if let Some((protecting_tag, protecting_tag_span, protection_span)) = protected {
+                                helps.push((Some(*protecting_tag_span), format!("{:?} was protected due to {:?} which was created here", tag, protecting_tag)));
+                                helps.push((Some(*protection_span), "this protector is live for this call".to_string()));
+                            }
+                        }
+                        Some(TagHistory::Untagged{ recently_created, recently_invalidated, matching_created, protected }) => {
+                            if let Some((range, span)) = recently_created {
+                                let msg = format!("tag was most recently created at offsets {}", HexRange(*range));
+                                helps.push((Some(*span), msg));
+                            }
+                            if let Some((range, span)) = recently_invalidated {
+                                let msg = format!("tag was later invalidated at offsets {}", HexRange(*range));
+                                helps.push((Some(*span), msg));
+                            }
+                            if let Some((range, span)) = matching_created {
+                                let msg = format!("this tag was also created here at offsets {}", HexRange(*range));
+                                helps.push((Some(*span), msg));
+                            }
+                            if let Some((protecting_tag, protecting_tag_span, protection_span)) = protected {
+                                helps.push((Some(*protecting_tag_span), format!("{:?} was protected due to a tag which was created here", protecting_tag)));
+                                helps.push((Some(*protection_span), "this protector is live for this call".to_string()));
+                            }
+                        }
+                        None => {}
+                    }
+                    helps
+                }
+                MultipleSymbolDefinitions { first, first_crate, second, second_crate, .. } =>
+                    vec![
+                        (Some(*first), format!("it's first defined here, in crate `{}`", first_crate)),
+                        (Some(*second), format!("then it's defined here again, in crate `{}`", second_crate)),
                     ],
+                SymbolShimClashing { link_name, span } =>
+                    vec![(Some(*span), format!("the `{}` symbol is defined here", link_name))],
                 _ => vec![],
             };
             (title, helps)
         }
         _ => {
-            let title = match e.kind {
+            #[rustfmt::skip]
+            let title = match e.kind() {
                 Unsupported(_) =>
                     "unsupported operation",
                 UndefinedBehavior(_) =>
                     "Undefined Behavior",
                 ResourceExhaustion(_) =>
                     "resource exhaustion",
-                InvalidProgram(InvalidProgramInfo::ReferencedConstant) =>
+                InvalidProgram(
+                    InvalidProgramInfo::AlreadyReported(_) |
+                    InvalidProgramInfo::Layout(..) |
+                    InvalidProgramInfo::ReferencedConstant
+                ) =>
                     "post-monomorphization error",
-                _ =>
-                    bug!("This error should be impossible in Miri: {}", e),
+                kind =>
+                    bug!("This error should be impossible in Miri: {:?}", kind),
             };
-            let helps = match e.kind {
-                Unsupported(UnsupportedOpInfo::NoMirFor(..)) =>
-                    vec![format!("make sure to use a Miri sysroot, which you can prepare with `cargo miri setup`")],
-                Unsupported(UnsupportedOpInfo::ReadBytesAsPointer | UnsupportedOpInfo::ThreadLocalStatic(_) | UnsupportedOpInfo::ReadExternStatic(_)) =>
-                    panic!("Error should never be raised by Miri: {:?}", e.kind),
-                Unsupported(_) =>
-                    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")],
+            #[rustfmt::skip]
+            let helps = match e.kind() {
+                Unsupported(
+                    UnsupportedOpInfo::ThreadLocalStatic(_) |
+                    UnsupportedOpInfo::ReadExternStatic(_)
+                ) =>
+                    panic!("Error should never be raised by Miri: {:?}", e.kind()),
+                Unsupported(
+                    UnsupportedOpInfo::Unsupported(_) |
+                    UnsupportedOpInfo::PartialPointerOverwrite(_) |
+                    UnsupportedOpInfo::ReadPointerAsBytes
+                ) =>
+                    vec![(None, format!("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::AlignmentCheckFailed { .. })
-                    if ecx.memory.extra.check_alignment == AlignmentCheck::Symbolic
+                    if ecx.machine.check_alignment == AlignmentCheck::Symbolic
                 =>
                     vec![
-                        format!("this usually indicates that your program performed an invalid operation and caused Undefined Behavior"),
-                        format!("but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives"),
+                        (None, format!("this usually indicates that your program performed an invalid operation and caused Undefined Behavior")),
+                        (None, format!("but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives")),
                     ],
                 UndefinedBehavior(_) =>
                     vec![
-                        format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior"),
-                        format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information"),
+                        (None, format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior")),
+                        (None, format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information")),
                     ],
-                _ => vec![],
+                InvalidProgram(_) | ResourceExhaustion(_) | MachineStop(_) =>
+                    vec![],
             };
-            (title, helps)
+            (Some(title), helps)
         }
     };
 
+    let stacktrace = ecx.generate_stacktrace();
+    let (stacktrace, was_pruned) = prune_stacktrace(ecx, stacktrace);
     e.print_backtrace();
-    let msg = e.to_string();
-    report_msg(*ecx.tcx, /*error*/true, &format!("{}: {}", title, msg), msg, helps, &ecx.generate_stacktrace());
+    msg.insert(0, e.to_string());
+    report_msg(
+        ecx,
+        DiagLevel::Error,
+        &if let Some(title) = title { format!("{}: {}", title, msg[0]) } else { msg[0].clone() },
+        msg,
+        helps,
+        &stacktrace,
+    );
+
+    // Include a note like `std` does when we omit frames from a backtrace
+    if was_pruned {
+        ecx.tcx.sess.diagnostic().note_without_error(
+            "some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace",
+        );
+    }
 
     // Debug-dump all locals.
     for (i, frame) in ecx.active_thread_stack().iter().enumerate() {
         trace!("-------------------");
         trace!("Frame {}", i);
-        trace!("    return: {:?}", frame.return_place.map(|p| *p));
+        trace!("    return: {:?}", *frame.return_place);
         for (i, local) in frame.locals.iter().enumerate() {
             trace!("    local {}: {:?}", i, local.value);
         }
     }
 
     // Extra output to help debug specific issues.
-    match e.kind {
-        UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some(access))) => {
+    match e.kind() {
+        UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some((alloc_id, access)))) => {
             eprintln!(
                 "Uninitialized read occurred at offsets 0x{:x}..0x{:x} into this allocation:",
-                access.uninit_ptr.offset.bytes(),
-                access.uninit_ptr.offset.bytes() + access.uninit_size.bytes(),
+                access.uninit_offset.bytes(),
+                access.uninit_offset.bytes() + access.uninit_size.bytes(),
             );
-            eprintln!("{:?}", ecx.memory.dump_alloc(access.uninit_ptr.alloc_id));
+            eprintln!("{:?}", ecx.dump_alloc(*alloc_id));
         }
         _ => {}
     }
@@ -152,39 +307,53 @@ pub fn report_error<'tcx, 'mir>(
 
 /// Report an error or note (depending on the `error` argument) with the given stacktrace.
 /// Also emits a full stacktrace of the interpreter stack.
-fn report_msg<'tcx>(
-    tcx: TyCtxt<'tcx>,
-    error: bool,
+/// We want to present a multi-line span message for some errors. Diagnostics do not support this
+/// directly, so we pass the lines as a `Vec<String>` and display each line after the first with an
+/// additional `span_label` or `note` call.
+fn report_msg<'mir, 'tcx>(
+    ecx: &InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
+    diag_level: DiagLevel,
     title: &str,
-    span_msg: String,
-    mut helps: Vec<String>,
+    span_msg: Vec<String>,
+    mut helps: Vec<(Option<SpanData>, String)>,
     stacktrace: &[FrameInfo<'tcx>],
 ) {
     let span = stacktrace.first().map_or(DUMMY_SP, |fi| fi.span);
-    let mut err = if error {
-        tcx.sess.struct_span_err(span, title)
-    } else {
-        tcx.sess.diagnostic().span_note_diag(span, title)
+    let sess = ecx.tcx.sess;
+    let mut err = match diag_level {
+        DiagLevel::Error => sess.struct_span_err(span, title).forget_guarantee(),
+        DiagLevel::Warning => sess.struct_span_warn(span, title),
+        DiagLevel::Note => sess.diagnostic().span_note_diag(span, title),
     };
+
     // Show main message.
     if span != DUMMY_SP {
-        err.span_label(span, span_msg);
+        for line in span_msg {
+            err.span_label(span, line);
+        }
     } else {
         // Make sure we show the message even when it is a dummy span.
-        err.note(&span_msg);
+        for line in span_msg {
+            err.note(&line);
+        }
         err.note("(no span available)");
     }
+
     // Show help messages.
     if !helps.is_empty() {
         // Add visual separator before backtrace.
-        helps.last_mut().unwrap().push_str("\n");
-        for help in helps {
-            err.help(&help);
+        helps.last_mut().unwrap().1.push('\n');
+        for (span_data, help) in helps {
+            if let Some(span_data) = span_data {
+                err.span_help(span_data.span(), &help);
+            } else {
+                err.help(&help);
+            }
         }
     }
     // Add backtrace
     for (idx, frame_info) in stacktrace.iter().enumerate() {
-        let is_local = frame_info.instance.def_id().is_local();
+        let is_local = ecx.machine.is_local(frame_info);
         // No span for non-local frames and the first frame (which is the error site).
         if is_local && idx > 0 {
             err.span_note(frame_info.span, &frame_info.to_string());
@@ -251,7 +420,10 @@ fn process_diagnostics(&self, info: TopFrameInfo<'tcx>) {
             }
             // Add popped frame back.
             if stacktrace.len() < info.stack_size {
-                assert!(stacktrace.len() == info.stack_size-1, "we should never pop more than one frame at once");
+                assert!(
+                    stacktrace.len() == info.stack_size - 1,
+                    "we should never pop more than one frame at once"
+                );
                 let frame_info = FrameInfo {
                     instance: info.instance.unwrap(),
                     span: info.span,
@@ -261,25 +433,47 @@ fn process_diagnostics(&self, info: TopFrameInfo<'tcx>) {
             } else if let Some(instance) = info.instance {
                 // Adjust topmost frame.
                 stacktrace[0].span = info.span;
-                assert_eq!(stacktrace[0].instance, instance, "we should not pop and push a frame in one step");
+                assert_eq!(
+                    stacktrace[0].instance, instance,
+                    "we should not pop and push a frame in one step"
+                );
             }
 
+            let (stacktrace, _was_pruned) = prune_stacktrace(this, stacktrace);
+
             // Show diagnostics.
             for e in diagnostics.drain(..) {
                 use NonHaltingDiagnostic::*;
                 let msg = match e {
-                    CreatedPointerTag(tag) =>
-                        format!("created tag {:?}", tag),
-                    PoppedPointerTag(item) =>
-                        format!("popped tracked tag for item {:?}", item),
-                    CreatedCallId(id) =>
-                        format!("function call with id {}", id),
-                    CreatedAlloc(AllocId(id)) =>
-                        format!("created allocation with id {}", id),
-                    FreedAlloc(AllocId(id)) =>
-                        format!("freed allocation with id {}", id),
+                    CreatedPointerTag(tag) => format!("created tag {:?}", tag),
+                    PoppedPointerTag(item, tag) =>
+                        match tag {
+                            None =>
+                                format!(
+                                    "popped tracked tag for item {:?} due to deallocation",
+                                    item
+                                ),
+                            Some((tag, access)) => {
+                                format!(
+                                    "popped tracked tag for item {:?} due to {:?} access for {:?}",
+                                    item, access, tag
+                                )
+                            }
+                        },
+                    CreatedCallId(id) => format!("function call with id {id}"),
+                    CreatedAlloc(AllocId(id)) => format!("created allocation with id {id}"),
+                    FreedAlloc(AllocId(id)) => format!("freed allocation with id {id}"),
+                    RejectedIsolatedOp(ref op) =>
+                        format!("{op} was made to return an error due to isolation"),
+                };
+
+                let (title, diag_level) = match e {
+                    RejectedIsolatedOp(_) =>
+                        ("operation rejected by isolation", DiagLevel::Warning),
+                    _ => ("tracking was triggered", DiagLevel::Note),
                 };
-                report_msg(*this.tcx, /*error*/false, "tracking was triggered", msg, vec![], &stacktrace);
+
+                report_msg(this, diag_level, title, vec![msg], vec![], &stacktrace);
             }
         });
     }