Skip to content

Commit 2a5eae3

Browse files
committed
provide mutable borrows when hooking memory write access
1 parent 3545dae commit 2a5eae3

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

src/librustc_mir/interpret/machine.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ use super::{
2424
EvalContext, PlaceTy, OpTy, Pointer, MemPlace, MemoryKind,
2525
};
2626

27-
/// Classifying memory accesses
28-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29-
pub enum MemoryAccess {
30-
Read,
31-
Write,
32-
}
33-
3427
/// Whether this kind of memory is allowed to leak
3528
pub trait MayLeak: Copy {
3629
fn may_leak(self) -> bool;
@@ -181,17 +174,22 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
181174
dest: PlaceTy<'tcx, Self::PointerTag>,
182175
) -> EvalResult<'tcx>;
183176

184-
/// Hook for performing extra checks on a memory access.
185-
///
186-
/// Takes read-only access to the allocation so we can keep all the memory read
187-
/// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
188-
/// need to mutate.
177+
/// Hook for performing extra checks on a memory read access.
189178
#[inline]
190-
fn memory_accessed(
179+
fn memory_read(
191180
_alloc: &Allocation<Self::PointerTag, Self::AllocExtra>,
192181
_ptr: Pointer<Self::PointerTag>,
193182
_size: Size,
194-
_access: MemoryAccess,
183+
) -> EvalResult<'tcx> {
184+
Ok(())
185+
}
186+
187+
/// Hook for performing extra checks on a memory write access.
188+
#[inline]
189+
fn memory_written(
190+
_alloc: &mut Allocation<Self::PointerTag, Self::AllocExtra>,
191+
_ptr: Pointer<Self::PointerTag>,
192+
_size: Size,
195193
) -> EvalResult<'tcx> {
196194
Ok(())
197195
}

src/librustc_mir/interpret/memory.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use syntax::ast::Mutability;
3030
use super::{
3131
Pointer, AllocId, Allocation, ConstValue, GlobalId,
3232
EvalResult, Scalar, EvalErrorKind, AllocType, PointerArithmetic,
33-
Machine, MemoryAccess, AllocMap, MayLeak, ScalarMaybeUndef, ErrorHandled,
33+
Machine, AllocMap, MayLeak, ScalarMaybeUndef, ErrorHandled,
3434
};
3535

3636
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
@@ -644,7 +644,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
644644
}
645645

646646
let alloc = self.get(ptr.alloc_id)?;
647-
M::memory_accessed(alloc, ptr, size, MemoryAccess::Read)?;
647+
M::memory_read(alloc, ptr, size)?;
648648

649649
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
650650
assert_eq!(size.bytes() as usize as u64, size.bytes());
@@ -690,7 +690,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
690690
self.clear_relocations(ptr, size)?;
691691

692692
let alloc = self.get_mut(ptr.alloc_id)?;
693-
M::memory_accessed(alloc, ptr, size, MemoryAccess::Write)?;
693+
M::memory_written(alloc, ptr, size)?;
694694

695695
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
696696
assert_eq!(size.bytes() as usize as u64, size.bytes());

src/librustc_mir/interpret/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub use self::place::{Place, PlaceTy, MemPlace, MPlaceTy};
3434

3535
pub use self::memory::{Memory, MemoryKind};
3636

37-
pub use self::machine::{Machine, AllocMap, MemoryAccess, MayLeak};
37+
pub use self::machine::{Machine, AllocMap, MayLeak};
3838

3939
pub use self::operand::{ScalarMaybeUndef, Value, ValTy, Operand, OpTy};
4040

0 commit comments

Comments
 (0)