Skip to content

Commit dce775a

Browse files
committed
Fix formatting and add ProvenanceMode enum
1 parent ff75dce commit dce775a

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

src/bin/miri.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_middle::{
3030
};
3131
use rustc_session::{config::ErrorOutputType, search_paths::PathKind, CtfeBacktrace};
3232

33-
use miri::BacktraceStyle;
33+
use miri::{BacktraceStyle, ProvenanceMode};
3434

3535
struct MiriCompilerCalls {
3636
miri_config: miri::MiriConfig,
@@ -384,12 +384,12 @@ fn main() {
384384
miri_config.tag_raw = true;
385385
}
386386
"-Zmiri-strict-provenance" => {
387-
miri_config.strict_provenance = true;
387+
miri_config.provenance_mode = ProvenanceMode::Strict;
388388
miri_config.tag_raw = true;
389389
miri_config.check_number_validity = true;
390390
}
391391
"-Zmiri-permissive-provenance" => {
392-
miri_config.permissive_provenance = true;
392+
miri_config.provenance_mode = ProvenanceMode::Permissive;
393393
miri_config.tag_raw = true;
394394
}
395395
"-Zmiri-mute-stdout-stderr" => {

src/eval.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ pub enum BacktraceStyle {
6868
Off,
6969
}
7070

71+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
72+
pub enum ProvenanceMode {
73+
/// Int2ptr casts return pointers with "wildcard" provenance
74+
/// that basically match that of all exposed pointers
75+
/// (and SB tags, if enabled).
76+
Permissive,
77+
/// Int2ptr casts return pointers with an invalid provenance,
78+
/// i.e., not valid for any memory access.
79+
Strict,
80+
/// Int2ptr casts return pointers valid for any allocation
81+
Legacy,
82+
}
83+
7184
/// Configuration needed to spawn a Miri instance.
7285
#[derive(Clone)]
7386
pub struct MiriConfig {
@@ -113,13 +126,8 @@ pub struct MiriConfig {
113126
pub panic_on_unsupported: bool,
114127
/// Which style to use for printing backtraces.
115128
pub backtrace_style: BacktraceStyle,
116-
/// Whether to allow "permissive provenance" rules. Enabling this means int2ptr casts return
117-
/// pointers with "wildcard" provenance that basically match that of all exposed pointers
118-
/// (and SB tags, if enabled).
119-
pub permissive_provenance: bool,
120-
/// Whether to enforce "strict provenance" rules. Enabling this means int2ptr casts return
121-
/// pointers with an invalid provenance, i.e., not valid for any memory access.
122-
pub strict_provenance: bool,
129+
/// Which provenance to use for int2ptr casts
130+
pub provenance_mode: ProvenanceMode,
123131
/// Whether to ignore any output by the program. This is helpful when debugging miri
124132
/// as its messages don't get intermingled with the program messages.
125133
pub mute_stdout_stderr: bool,
@@ -148,8 +156,7 @@ impl Default for MiriConfig {
148156
measureme_out: None,
149157
panic_on_unsupported: false,
150158
backtrace_style: BacktraceStyle::Short,
151-
permissive_provenance: false,
152-
strict_provenance: false,
159+
provenance_mode: ProvenanceMode::Legacy,
153160
mute_stdout_stderr: false,
154161
}
155162
}

src/intptrcast.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@ pub struct GlobalStateInner {
2727
/// This is used as a memory address when a new pointer is casted to an integer. It
2828
/// is always larger than any address that was previously made part of a block.
2929
next_base_addr: u64,
30-
/// Whether to allow "permissive provenance" rules. Enabling this means int2ptr casts return
31-
/// pointers with "wildcard" provenance that basically match that of all exposed pointers
32-
/// (and SB tags, if enabled).
33-
permissive_provenance: bool,
34-
/// Whether to enforce "strict provenance" rules. Enabling this means int2ptr casts return
35-
/// pointers with an invalid provenance, i.e., not valid for any memory access.
36-
strict_provenance: bool,
30+
/// The provenance to use for int2ptr casts
31+
provenance_mode: ProvenanceMode,
3732
}
3833

3934
impl GlobalStateInner {
@@ -43,8 +38,7 @@ impl GlobalStateInner {
4338
base_addr: FxHashMap::default(),
4439
exposed: FxHashSet::default(),
4540
next_base_addr: STACK_ADDR,
46-
permissive_provenance: config.permissive_provenance,
47-
strict_provenance: config.strict_provenance,
41+
provenance_mode: config.provenance_mode,
4842
}
4943
}
5044
}
@@ -81,11 +75,12 @@ impl<'mir, 'tcx> GlobalStateInner {
8175
}
8276
}
8377
}?;
84-
85-
if global_state.permissive_provenance && !global_state.exposed.contains(&alloc_id) {
86-
None
87-
} else {
88-
Some(alloc_id)
78+
79+
match global_state.provenance_mode {
80+
ProvenanceMode::Legacy => Some(alloc_id),
81+
ProvenanceMode::Permissive if global_state.exposed.contains(&alloc_id) =>
82+
Some(alloc_id),
83+
_ => None,
8984
}
9085
}
9186

@@ -104,12 +99,14 @@ impl<'mir, 'tcx> GlobalStateInner {
10499
Self::ptr_from_casted_addr(ecx, addr)
105100
}
106101

107-
pub fn ptr_from_casted_addr(ecx: &MiriEvalContext<'mir, 'tcx>, addr: u64) -> Pointer<Option<Tag>> {
102+
pub fn ptr_from_casted_addr(
103+
ecx: &MiriEvalContext<'mir, 'tcx>,
104+
addr: u64,
105+
) -> Pointer<Option<Tag>> {
108106
trace!("Casting 0x{:x} to a pointer", addr);
109-
let global_state = ecx.machine.intptrcast.borrow();
110107

111108
// Special-case NULL, it is always an invalid pointer.
112-
if addr == 0 || global_state.strict_provenance {
109+
if addr == 0 {
113110
return Pointer::new(None, Size::from_bytes(addr));
114111
}
115112

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ pub use crate::diagnostics::{
7575
NonHaltingDiagnostic, TerminationInfo,
7676
};
7777
pub use crate::eval::{
78-
create_ecx, eval_entry, AlignmentCheck, BacktraceStyle, IsolatedOp, MiriConfig, RejectOpWith,
78+
create_ecx, eval_entry, AlignmentCheck, BacktraceStyle, IsolatedOp, MiriConfig, ProvenanceMode,
79+
RejectOpWith,
7980
};
8081
pub use crate::helpers::EvalContextExt as HelpersEvalContextExt;
8182
pub use crate::machine::{

0 commit comments

Comments
 (0)