Skip to content

Commit

Permalink
show int2ptr warning once for each span (but don't duplicate the long…
Browse files Browse the repository at this point in the history
… help)
  • Loading branch information
RalfJung committed Jun 28, 2022
1 parent 67e89b5 commit 1b38a9a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub enum NonHaltingDiagnostic {
FreedAlloc(AllocId),
RejectedIsolatedOp(String),
ProgressReport,
Int2Ptr,
Int2Ptr { details: bool },
}

/// Level of Miri specific diagnostics
Expand Down Expand Up @@ -451,13 +451,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, '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(..)
Expand All @@ -467,7 +467,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, '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")),
Expand Down
17 changes: 12 additions & 5 deletions src/intptrcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use log::trace;
use rand::Rng;

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_span::Span;
use rustc_target::abi::{HasDataLayout, Size};

use crate::*;
Expand Down Expand Up @@ -140,12 +141,18 @@ impl<'mir, 'tcx> GlobalStateInner {

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!(
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions tests/pass/box.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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();
| ^^^^^^^^^^^^^

0 comments on commit 1b38a9a

Please sign in to comment.