show int2ptr warning once for each span (but don't duplicate the long help)

This commit is contained in:
Ralf Jung 2022-06-27 20:22:50 -04:00
parent 67e89b53e2
commit c1eddbc7fe
4 changed files with 32 additions and 9 deletions

View File

@ -69,7 +69,9 @@ pub enum NonHaltingDiagnostic {
FreedAlloc(AllocId),
RejectedIsolatedOp(String),
ProgressReport,
Int2Ptr,
Int2Ptr {
details: bool,
},
}
/// Level of Miri specific diagnostics
@ -451,13 +453,13 @@ fn process_diagnostics(&self, info: TopFrameInfo<'tcx>) {
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"),
Int2Ptr { .. } => format!("integer-to-pointer cast"),
};
let (title, diag_level) = match e {
RejectedIsolatedOp(_) =>
("operation rejected by isolation", DiagLevel::Warning),
Int2Ptr => ("integer-to-pointer cast", DiagLevel::Warning),
Int2Ptr { .. } => ("integer-to-pointer cast", DiagLevel::Warning),
CreatedPointerTag(..)
| PoppedPointerTag(..)
| CreatedCallId(..)
@ -467,7 +469,7 @@ fn process_diagnostics(&self, info: TopFrameInfo<'tcx>) {
};
let helps = match e {
Int2Ptr =>
Int2Ptr { details: true } =>
vec![
(None, format!("this program is using integer-to-pointer casts or (equivalently) `from_exposed_addr`,")),
(None, format!("which means that Miri might miss pointer bugs in this program")),

View File

@ -5,6 +5,7 @@
use rand::Rng;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_span::Span;
use rustc_target::abi::{HasDataLayout, Size};
use crate::*;
@ -140,12 +141,18 @@ pub fn ptr_from_addr_cast(
match global_state.provenance_mode {
ProvenanceMode::Default => {
// The first time this happens, print a warning.
use std::sync::atomic::{AtomicBool, Ordering};
static FIRST_WARNING: AtomicBool = AtomicBool::new(true);
if FIRST_WARNING.swap(false, Ordering::Relaxed) {
register_diagnostic(NonHaltingDiagnostic::Int2Ptr);
// The first time this happens at a particular location, print a warning.
thread_local! {
// `Span` is non-`Send`, so we use a thread-local instead.
static PAST_WARNINGS: RefCell<FxHashSet<Span>> = RefCell::default();
}
PAST_WARNINGS.with_borrow_mut(|past_warnings| {
let first = past_warnings.is_empty();
if past_warnings.insert(ecx.cur_span()) {
// Newly inserted, so first time we see this span.
register_diagnostic(NonHaltingDiagnostic::Int2Ptr { details: first });
}
});
}
ProvenanceMode::Strict => {
throw_unsup_format!(

View File

@ -8,6 +8,7 @@
#![feature(yeet_expr)]
#![feature(is_some_with)]
#![feature(nonzero_ops)]
#![feature(local_key_cell_methods)]
#![warn(rust_2018_idioms)]
#![allow(
clippy::collapsible_else_if,

View File

@ -18,3 +18,16 @@ note: inside `main` at $DIR/box.rs:LL:CC
LL | into_raw();
| ^^^^^^^^^^
warning: integer-to-pointer cast
--> $DIR/box.rs:LL:CC
|
LL | let r = ((u.as_ptr() as usize) + 0) as *mut i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ integer-to-pointer cast
|
= note: inside `into_unique` at $DIR/box.rs:LL:CC
note: inside `main` at $DIR/box.rs:LL:CC
--> $DIR/box.rs:LL:CC
|
LL | into_unique();
| ^^^^^^^^^^^^^