Skip to content

Commit 955bacc

Browse files
committed
Auto merge of #2047 - RalfJung:no-extras, r=RalfJung
adjust for MemoryExtra being merged into Machine The Miri side of rust-lang/rust#95620
2 parents 95559c9 + 0512b2a commit 955bacc

25 files changed

+311
-329
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6af09d2505f38e4f1df291df56d497fb2ad935ed
1+
634770c0a7f8598164ab825cfe419cc8b03c36e5

src/data_race.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ use crate::{
7979
};
8080

8181
pub type AllocExtra = VClockAlloc;
82-
pub type MemoryExtra = GlobalState;
8382

8483
/// Valid atomic read-write operations, alias of atomic::Ordering (not non-exhaustive).
8584
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -596,9 +595,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
596595
let eq = this.binary_op(mir::BinOp::Eq, &old, expect_old)?;
597596
// If the operation would succeed, but is "weak", fail some portion
598597
// of the time, based on `rate`.
599-
let rate = this.memory.extra.cmpxchg_weak_failure_rate;
598+
let rate = this.machine.cmpxchg_weak_failure_rate;
600599
let cmpxchg_success = eq.to_scalar()?.to_bool()?
601-
&& (!can_fail_spuriously || this.memory.extra.rng.get_mut().gen::<f64>() < rate);
600+
&& (!can_fail_spuriously || this.machine.rng.get_mut().gen::<f64>() < rate);
602601
let res = Immediate::ScalarPair(
603602
old.to_scalar_or_uninit(),
604603
Scalar::from_bool(cmpxchg_success).into(),
@@ -690,7 +689,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
690689
/// Update the data-race detector for an atomic fence on the current thread.
691690
fn validate_atomic_fence(&mut self, atomic: AtomicFenceOp) -> InterpResult<'tcx> {
692691
let this = self.eval_context_mut();
693-
if let Some(data_race) = &mut this.memory.extra.data_race {
692+
if let Some(data_race) = &mut this.machine.data_race {
694693
data_race.maybe_perform_sync_operation(move |index, mut clocks| {
695694
log::trace!("Atomic fence on {:?} with ordering {:?}", index, atomic);
696695

@@ -725,7 +724,7 @@ pub struct VClockAlloc {
725724
impl VClockAlloc {
726725
/// Create a new data-race detector for newly allocated memory.
727726
pub fn new_allocation(
728-
global: &MemoryExtra,
727+
global: &GlobalState,
729728
len: Size,
730729
kind: MemoryKind<MiriMemoryKind>,
731730
) -> VClockAlloc {
@@ -796,7 +795,7 @@ impl VClockAlloc {
796795
#[cold]
797796
#[inline(never)]
798797
fn report_data_race<'tcx>(
799-
global: &MemoryExtra,
798+
global: &GlobalState,
800799
range: &MemoryCellClocks,
801800
action: &str,
802801
is_atomic: bool,
@@ -950,13 +949,13 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
950949
#[inline]
951950
fn allow_data_races_ref<R>(&self, op: impl FnOnce(&MiriEvalContext<'mir, 'tcx>) -> R) -> R {
952951
let this = self.eval_context_ref();
953-
let old = if let Some(data_race) = &this.memory.extra.data_race {
952+
let old = if let Some(data_race) = &this.machine.data_race {
954953
data_race.multi_threaded.replace(false)
955954
} else {
956955
false
957956
};
958957
let result = op(this);
959-
if let Some(data_race) = &this.memory.extra.data_race {
958+
if let Some(data_race) = &this.machine.data_race {
960959
data_race.multi_threaded.set(old);
961960
}
962961
result
@@ -971,13 +970,13 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
971970
op: impl FnOnce(&mut MiriEvalContext<'mir, 'tcx>) -> R,
972971
) -> R {
973972
let this = self.eval_context_mut();
974-
let old = if let Some(data_race) = &this.memory.extra.data_race {
973+
let old = if let Some(data_race) = &this.machine.data_race {
975974
data_race.multi_threaded.replace(false)
976975
} else {
977976
false
978977
};
979978
let result = op(this);
980-
if let Some(data_race) = &this.memory.extra.data_race {
979+
if let Some(data_race) = &this.machine.data_race {
981980
data_race.multi_threaded.set(old);
982981
}
983982
result
@@ -997,14 +996,13 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
997996
) -> Result<(), DataRace>,
998997
) -> InterpResult<'tcx> {
999998
let this = self.eval_context_ref();
1000-
if let Some(data_race) = &this.memory.extra.data_race {
999+
if let Some(data_race) = &this.machine.data_race {
10011000
if data_race.multi_threaded.get() {
10021001
let size = place.layout.size;
1003-
let (alloc_id, base_offset, ptr) = this.memory.ptr_get_alloc(place.ptr)?;
1002+
let (alloc_id, base_offset, ptr) = this.ptr_get_alloc_id(place.ptr)?;
10041003
// Load and log the atomic operation.
10051004
// Note that atomic loads are possible even from read-only allocations, so `get_alloc_extra_mut` is not an option.
1006-
let alloc_meta =
1007-
&this.memory.get_alloc_extra(alloc_id)?.data_race.as_ref().unwrap();
1005+
let alloc_meta = &this.get_alloc_extra(alloc_id)?.data_race.as_ref().unwrap();
10081006
log::trace!(
10091007
"Atomic op({}) with ordering {:?} on {:?} (size={})",
10101008
description,

src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub fn report_error<'tcx, 'mir>(
196196
Unsupported(_) =>
197197
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"))],
198198
UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. })
199-
if ecx.memory.extra.check_alignment == AlignmentCheck::Symbolic
199+
if ecx.machine.check_alignment == AlignmentCheck::Symbolic
200200
=>
201201
vec![
202202
(None, format!("this usually indicates that your program performed an invalid operation and caused Undefined Behavior")),
@@ -251,7 +251,7 @@ pub fn report_error<'tcx, 'mir>(
251251
access.uninit_offset.bytes(),
252252
access.uninit_offset.bytes() + access.uninit_size.bytes(),
253253
);
254-
eprintln!("{:?}", ecx.memory.dump_alloc(*alloc_id));
254+
eprintln!("{:?}", ecx.dump_alloc(*alloc_id));
255255
}
256256
_ => {}
257257
}

src/eval.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,18 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
153153
tcx: TyCtxt<'tcx>,
154154
entry_id: DefId,
155155
entry_type: EntryFnType,
156-
config: MiriConfig,
156+
config: &MiriConfig,
157157
) -> InterpResult<'tcx, (InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>, MPlaceTy<'tcx, Tag>)> {
158158
let param_env = ty::ParamEnv::reveal_all();
159159
let layout_cx = LayoutCx { tcx, param_env };
160160
let mut ecx = InterpCx::new(
161161
tcx,
162162
rustc_span::source_map::DUMMY_SP,
163163
param_env,
164-
Evaluator::new(&config, layout_cx),
165-
MemoryExtra::new(&config),
164+
Evaluator::new(config, layout_cx),
166165
);
167-
// Complete initialization.
168-
EnvVars::init(&mut ecx, config.excluded_env_vars, config.forwarded_env_vars)?;
169-
MemoryExtra::init_extern_statics(&mut ecx)?;
166+
// Some parts of initialization require a full `InterpCx`.
167+
Evaluator::late_init(&mut ecx, config)?;
170168

171169
// Make sure we have MIR. We check MIR for some stable monomorphic function in libcore.
172170
let sentinel = ecx.resolve_path(&["core", "ascii", "escape_default"]);
@@ -260,7 +258,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
260258
.unwrap()
261259
.unwrap();
262260

263-
let main_ptr = ecx.memory.create_fn_alloc(FnVal::Instance(entry_instance));
261+
let main_ptr = ecx.create_fn_alloc_ptr(FnVal::Instance(entry_instance));
264262

265263
ecx.call_function(
266264
start_instance,
@@ -296,7 +294,7 @@ pub fn eval_entry<'tcx>(
296294
// Copy setting before we move `config`.
297295
let ignore_leaks = config.ignore_leaks;
298296

299-
let (mut ecx, ret_place) = match create_ecx(tcx, entry_id, entry_type, config) {
297+
let (mut ecx, ret_place) = match create_ecx(tcx, entry_id, entry_type, &config) {
300298
Ok(v) => v,
301299
Err(err) => {
302300
err.print_backtrace();
@@ -354,7 +352,7 @@ pub fn eval_entry<'tcx>(
354352
}
355353
// Check for memory leaks.
356354
info!("Additonal static roots: {:?}", ecx.machine.static_roots);
357-
let leaks = ecx.memory.leak_report(&ecx.machine.static_roots);
355+
let leaks = ecx.leak_report(&ecx.machine.static_roots);
358356
if leaks != 0 {
359357
tcx.sess.err("the evaluated program leaked memory");
360358
tcx.sess.note_without_error("pass `-Zmiri-ignore-leaks` to disable this check");

src/helpers.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
199199
getrandom::getrandom(&mut data)
200200
.map_err(|err| err_unsup_format!("host getrandom failed: {}", err))?;
201201
} else {
202-
let rng = this.memory.extra.rng.get_mut();
202+
let rng = this.machine.rng.get_mut();
203203
rng.fill_bytes(&mut data);
204204
}
205205

206-
this.memory.write_bytes(ptr, data.iter().copied())
206+
this.write_bytes_ptr(ptr, data.iter().copied())
207207
}
208208

209209
/// Call a function: Push the stack frame and pass the arguments.
@@ -645,7 +645,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
645645
loop {
646646
// FIXME: We are re-getting the allocation each time around the loop.
647647
// Would be nice if we could somehow "extend" an existing AllocRange.
648-
let alloc = this.memory.get(ptr.offset(len, this)?.into(), size1, Align::ONE)?.unwrap(); // not a ZST, so we will get a result
648+
let alloc =
649+
this.get_ptr_alloc(ptr.offset(len, this)?.into(), size1, Align::ONE)?.unwrap(); // not a ZST, so we will get a result
649650
let byte = alloc.read_scalar(alloc_range(Size::ZERO, size1))?.to_u8()?;
650651
if byte == 0 {
651652
break;
@@ -655,7 +656,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
655656
}
656657

657658
// Step 2: get the bytes.
658-
this.memory.read_bytes(ptr.into(), len)
659+
this.read_bytes_ptr(ptr.into(), len)
659660
}
660661

661662
fn read_wide_str(&self, mut ptr: Pointer<Option<Tag>>) -> InterpResult<'tcx, Vec<u16>> {
@@ -667,7 +668,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
667668
loop {
668669
// FIXME: We are re-getting the allocation each time around the loop.
669670
// Would be nice if we could somehow "extend" an existing AllocRange.
670-
let alloc = this.memory.get(ptr.into(), size2, align2)?.unwrap(); // not a ZST, so we will get a result
671+
let alloc = this.get_ptr_alloc(ptr.into(), size2, align2)?.unwrap(); // not a ZST, so we will get a result
671672
let wchar = alloc.read_scalar(alloc_range(Size::ZERO, size2))?.to_u16()?;
672673
if wchar == 0 {
673674
break;
@@ -750,8 +751,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
750751
/// Mark a machine allocation that was just created as immutable.
751752
fn mark_immutable(&mut self, mplace: &MemPlace<Tag>) {
752753
let this = self.eval_context_mut();
753-
this.memory
754-
.mark_immutable(mplace.ptr.into_pointer_or_addr().unwrap().provenance.alloc_id)
754+
this.alloc_mark_immutable(mplace.ptr.into_pointer_or_addr().unwrap().provenance.alloc_id)
755755
.unwrap();
756756
}
757757
}

src/intptrcast.rs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use rustc_target::abi::{HasDataLayout, Size};
99

1010
use crate::*;
1111

12-
pub type MemoryExtra = RefCell<GlobalState>;
12+
pub type GlobalState = RefCell<GlobalStateInner>;
1313

1414
#[derive(Clone, Debug)]
15-
pub struct GlobalState {
15+
pub struct GlobalStateInner {
1616
/// This is used as a map between the address of each allocation and its `AllocId`.
1717
/// It is always sorted
1818
int_to_ptr_map: Vec<(u64, AllocId)>,
@@ -29,9 +29,9 @@ pub struct GlobalState {
2929
strict_provenance: bool,
3030
}
3131

32-
impl GlobalState {
32+
impl GlobalStateInner {
3333
pub fn new(config: &MiriConfig) -> Self {
34-
GlobalState {
34+
GlobalStateInner {
3535
int_to_ptr_map: Vec::default(),
3636
base_addr: FxHashMap::default(),
3737
next_base_addr: STACK_ADDR,
@@ -40,13 +40,10 @@ impl GlobalState {
4040
}
4141
}
4242

43-
impl<'mir, 'tcx> GlobalState {
44-
pub fn ptr_from_addr(
45-
addr: u64,
46-
memory: &Memory<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
47-
) -> Pointer<Option<Tag>> {
43+
impl<'mir, 'tcx> GlobalStateInner {
44+
pub fn ptr_from_addr(addr: u64, ecx: &MiriEvalContext<'mir, 'tcx>) -> Pointer<Option<Tag>> {
4845
trace!("Casting 0x{:x} to a pointer", addr);
49-
let global_state = memory.extra.intptrcast.borrow();
46+
let global_state = ecx.machine.intptrcast.borrow();
5047

5148
if global_state.strict_provenance {
5249
return Pointer::new(None, Size::from_bytes(addr));
@@ -64,7 +61,11 @@ impl<'mir, 'tcx> GlobalState {
6461
let offset = addr - glb;
6562
// If the offset exceeds the size of the allocation, don't use this `alloc_id`.
6663
if offset
67-
<= memory.get_size_and_align(alloc_id, AllocCheck::MaybeDead).unwrap().0.bytes()
64+
<= ecx
65+
.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead)
66+
.unwrap()
67+
.0
68+
.bytes()
6869
{
6970
Some(alloc_id)
7071
} else {
@@ -79,11 +80,8 @@ impl<'mir, 'tcx> GlobalState {
7980
)
8081
}
8182

82-
fn alloc_base_addr(
83-
memory: &Memory<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
84-
alloc_id: AllocId,
85-
) -> u64 {
86-
let mut global_state = memory.extra.intptrcast.borrow_mut();
83+
fn alloc_base_addr(ecx: &MiriEvalContext<'mir, 'tcx>, alloc_id: AllocId) -> u64 {
84+
let mut global_state = ecx.machine.intptrcast.borrow_mut();
8785
let global_state = &mut *global_state;
8886

8987
match global_state.base_addr.entry(alloc_id) {
@@ -92,12 +90,12 @@ impl<'mir, 'tcx> GlobalState {
9290
// There is nothing wrong with a raw pointer being cast to an integer only after
9391
// it became dangling. Hence `MaybeDead`.
9492
let (size, align) =
95-
memory.get_size_and_align(alloc_id, AllocCheck::MaybeDead).unwrap();
93+
ecx.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead).unwrap();
9694

9795
// This allocation does not have a base address yet, pick one.
9896
// Leave some space to the previous allocation, to give it some chance to be less aligned.
9997
let slack = {
100-
let mut rng = memory.extra.rng.borrow_mut();
98+
let mut rng = ecx.machine.rng.borrow_mut();
10199
// This means that `(global_state.next_base_addr + slack) % 16` is uniformly distributed.
102100
rng.gen_range(0..16)
103101
};
@@ -129,27 +127,21 @@ impl<'mir, 'tcx> GlobalState {
129127
}
130128

131129
/// Convert a relative (tcx) pointer to an absolute address.
132-
pub fn rel_ptr_to_addr(
133-
memory: &Memory<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
134-
ptr: Pointer<AllocId>,
135-
) -> u64 {
130+
pub fn rel_ptr_to_addr(ecx: &MiriEvalContext<'mir, 'tcx>, ptr: Pointer<AllocId>) -> u64 {
136131
let (alloc_id, offset) = ptr.into_parts(); // offset is relative
137-
let base_addr = GlobalState::alloc_base_addr(memory, alloc_id);
132+
let base_addr = GlobalStateInner::alloc_base_addr(ecx, alloc_id);
138133

139134
// Add offset with the right kind of pointer-overflowing arithmetic.
140-
let dl = memory.data_layout();
135+
let dl = ecx.data_layout();
141136
dl.overflowing_offset(base_addr, offset.bytes()).0
142137
}
143138

144-
pub fn abs_ptr_to_rel(
145-
memory: &Memory<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
146-
ptr: Pointer<Tag>,
147-
) -> Size {
139+
pub fn abs_ptr_to_rel(ecx: &MiriEvalContext<'mir, 'tcx>, ptr: Pointer<Tag>) -> Size {
148140
let (tag, addr) = ptr.into_parts(); // addr is absolute
149-
let base_addr = GlobalState::alloc_base_addr(memory, tag.alloc_id);
141+
let base_addr = GlobalStateInner::alloc_base_addr(ecx, tag.alloc_id);
150142

151143
// Wrapping "addr - base_addr"
152-
let dl = memory.data_layout();
144+
let dl = ecx.data_layout();
153145
let neg_base_addr = (base_addr as i64).wrapping_neg();
154146
Size::from_bytes(dl.overflowing_signed_offset(addr.bytes(), neg_base_addr).0)
155147
}
@@ -170,7 +162,7 @@ mod tests {
170162

171163
#[test]
172164
fn test_align_addr() {
173-
assert_eq!(GlobalState::align_addr(37, 4), 40);
174-
assert_eq!(GlobalState::align_addr(44, 4), 44);
165+
assert_eq!(GlobalStateInner::align_addr(37, 4), 40);
166+
assert_eq!(GlobalStateInner::align_addr(44, 4), 44);
175167
}
176168
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ pub use crate::eval::{
6666
};
6767
pub use crate::helpers::EvalContextExt as HelpersEvalContextExt;
6868
pub use crate::machine::{
69-
AllocExtra, Evaluator, FrameData, MemoryExtra, MiriEvalContext, MiriEvalContextExt,
70-
MiriMemoryKind, Tag, NUM_CPUS, PAGE_SIZE, STACK_ADDR, STACK_SIZE,
69+
AllocExtra, Evaluator, FrameData, MiriEvalContext, MiriEvalContextExt, MiriMemoryKind, Tag,
70+
NUM_CPUS, PAGE_SIZE, STACK_ADDR, STACK_SIZE,
7171
};
7272
pub use crate::mono_hash_map::MonoHashMap;
7373
pub use crate::operator::EvalContextExt as OperatorEvalContextExt;

0 commit comments

Comments
 (0)