]> git.lizzy.rs Git - rust.git/blobdiff - src/diagnostics.rs
Typo
[rust.git] / src / diagnostics.rs
index 0e3e693e33f9010cbeca7dc83c608d9de9da1c64..1ffcdc799edf059c1cff168d30a0228c830708c7 100644 (file)
@@ -6,9 +6,10 @@
 
 use rustc_middle::ty;
 use rustc_span::{source_map::DUMMY_SP, Span, SpanData, Symbol};
+use rustc_target::abi::{Align, Size};
 
 use crate::helpers::HexRange;
-use crate::stacked_borrows::{diagnostics::TagHistory, AccessKind, SbTag};
+use crate::stacked_borrows::{diagnostics::TagHistory, AccessKind};
 use crate::*;
 
 /// Details of premature program termination.
@@ -21,6 +22,7 @@ pub enum TerminationInfo {
         help: Option<String>,
         history: Option<TagHistory>,
     },
+    Int2PtrWithStrictProvenance,
     Deadlock,
     MultipleSymbolDefinitions {
         link_name: Symbol,
@@ -42,6 +44,11 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
             Exit(code) => write!(f, "the evaluated program completed with exit code {}", code),
             Abort(msg) => write!(f, "{}", msg),
             UnsupportedInIsolation(msg) => write!(f, "{}", msg),
+            Int2PtrWithStrictProvenance =>
+                write!(
+                    f,
+                    "integer-to-pointer casts and `ptr::from_exposed_addr` are not supported with `-Zmiri-strict-provenance`"
+                ),
             StackedBorrowsUb { msg, .. } => write!(f, "{}", msg),
             Deadlock => write!(f, "the evaluated program deadlocked"),
             MultipleSymbolDefinitions { link_name, .. } =>
@@ -61,13 +68,17 @@ impl MachineStopType for TerminationInfo {}
 /// Miri specific diagnostics
 pub enum NonHaltingDiagnostic {
     CreatedPointerTag(NonZeroU64),
-    /// 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)>),
+    /// This `Item` was popped from the borrow stack, either due to an access with the given tag or
+    /// a deallocation when the second argument is `None`.
+    PoppedPointerTag(Item, Option<(SbTagExtra, AccessKind)>),
     CreatedCallId(CallId),
-    CreatedAlloc(AllocId),
+    CreatedAlloc(AllocId, Size, Align, MemoryKind<MiriMemoryKind>),
     FreedAlloc(AllocId),
     RejectedIsolatedOp(String),
+    ProgressReport,
+    Int2Ptr {
+        details: bool,
+    },
 }
 
 /// Level of Miri specific diagnostics
@@ -86,6 +97,9 @@ fn prune_stacktrace<'mir, 'tcx>(
 ) -> (Vec<FrameInfo<'tcx>>, bool) {
     match ecx.machine.backtrace_style {
         BacktraceStyle::Off => {
+            // Remove all frames marked with `caller_location` -- that attribute indicates we
+            // usually want to point at the caller, not them.
+            stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(*ecx.tcx));
             // Retain one frame so that we can print a span for the error itself
             stacktrace.truncate(1);
             (stacktrace, false)
@@ -97,6 +111,10 @@ fn prune_stacktrace<'mir, 'tcx>(
             // 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 {
+                // Remove all frames marked with `caller_location` -- that attribute indicates we
+                // usually want to point at the caller, not them.
+                stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(*ecx.tcx));
+
                 // 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
@@ -144,7 +162,8 @@ pub fn report_error<'tcx, 'mir>(
             let title = match info {
                 Exit(code) => return Some(*code),
                 Abort(_) => Some("abnormal termination"),
-                UnsupportedInIsolation(_) => Some("unsupported operation"),
+                UnsupportedInIsolation(_) | Int2PtrWithStrictProvenance =>
+                    Some("unsupported operation"),
                 StackedBorrowsUb { .. } => Some("Undefined Behavior"),
                 Deadlock => Some("deadlock"),
                 MultipleSymbolDefinitions { .. } | SymbolShimClashing { .. } => None,
@@ -173,25 +192,7 @@ pub fn report_error<'tcx, 'mir>(
                             }
                             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()));
+                                helps.push((Some(*protection_span), format!("this protector is live for this call")));
                             }
                         }
                         None => {}
@@ -205,6 +206,8 @@ pub fn report_error<'tcx, 'mir>(
                     ],
                 SymbolShimClashing { link_name, span } =>
                     vec![(Some(*span), format!("the `{}` symbol is defined here", link_name))],
+                Int2PtrWithStrictProvenance =>
+                    vec![(None, format!("use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead"))],
                 _ => vec![],
             };
             (title, helps)
@@ -461,19 +464,42 @@ fn process_diagnostics(&self, info: TopFrameInfo<'tcx>) {
                             }
                         },
                     CreatedCallId(id) => format!("function call with id {id}"),
-                    CreatedAlloc(AllocId(id)) => format!("created allocation with id {id}"),
+                    CreatedAlloc(AllocId(id), size, align, kind) =>
+                        format!("created {kind} allocation of {} bytes (alignment {} bytes) with id {id}", size.bytes(), align.bytes()),
                     FreedAlloc(AllocId(id)) => format!("freed allocation with id {id}"),
                     RejectedIsolatedOp(ref op) =>
                         format!("{op} was made to return an error due to isolation"),
+                    ProgressReport =>
+                        format!("progress report: current operation being executed is here"),
+                    Int2Ptr { .. } => format!("integer-to-pointer cast"),
                 };
 
                 let (title, diag_level) = match e {
                     RejectedIsolatedOp(_) =>
                         ("operation rejected by isolation", DiagLevel::Warning),
-                    _ => ("tracking was triggered", DiagLevel::Note),
+                    Int2Ptr { .. } => ("integer-to-pointer cast", DiagLevel::Warning),
+                    CreatedPointerTag(..)
+                    | PoppedPointerTag(..)
+                    | CreatedCallId(..)
+                    | CreatedAlloc(..)
+                    | FreedAlloc(..)
+                    | ProgressReport => ("tracking was triggered", DiagLevel::Note),
+                };
+
+                let helps = match e {
+                    Int2Ptr { details: true } =>
+                        vec![
+                            (None, format!("This program is using integer-to-pointer casts or (equivalently) `ptr::from_exposed_addr`,")),
+                            (None, format!("which means that Miri might miss pointer bugs in this program.")),
+                            (None, format!("See https://doc.rust-lang.org/nightly/std/ptr/fn.from_exposed_addr.html for more details on that operation.")),
+                            (None, format!("To ensure that Miri does not miss bugs in your program, use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead.")),
+                            (None, format!("You can then pass the `-Zmiri-strict-provenance` flag to Miri, to ensure you are not relying on `from_exposed_addr` semantics.")),
+                            (None, format!("Alternatively, the `-Zmiri-permissive-provenance` flag disables this warning.")),
+                        ],
+                    _ => vec![],
                 };
 
-                report_msg(this, diag_level, title, vec![msg], vec![], &stacktrace);
+                report_msg(this, diag_level, title, vec![msg], helps, &stacktrace);
             }
         });
     }