1
Fork 0

interpret memory access hooks: also pass through the Pointer used for the access

This commit is contained in:
Ralf Jung 2025-03-19 16:40:21 +01:00
parent 75530e9f72
commit b9f59f6107
4 changed files with 26 additions and 2 deletions

View file

@ -22,7 +22,7 @@ use crate::errors::{LongRunning, LongRunningWarn};
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use crate::interpret::{ use crate::interpret::{
self, AllocId, AllocInit, AllocRange, ConstAllocation, CtfeProvenance, FnArg, Frame, self, AllocId, AllocInit, AllocRange, ConstAllocation, CtfeProvenance, FnArg, Frame,
GlobalAlloc, ImmTy, InterpCx, InterpResult, MPlaceTy, OpTy, RangeSet, Scalar, GlobalAlloc, ImmTy, InterpCx, InterpResult, MPlaceTy, OpTy, Pointer, RangeSet, Scalar,
compile_time_machine, interp_ok, throw_exhaust, throw_inval, throw_ub, throw_ub_custom, compile_time_machine, interp_ok, throw_exhaust, throw_inval, throw_ub, throw_ub_custom,
throw_unsup, throw_unsup_format, throw_unsup, throw_unsup_format,
}; };
@ -688,6 +688,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
_tcx: TyCtxtAt<'tcx>, _tcx: TyCtxtAt<'tcx>,
_machine: &mut Self, _machine: &mut Self,
_alloc_extra: &mut Self::AllocExtra, _alloc_extra: &mut Self::AllocExtra,
_ptr: Pointer<Option<Self::Provenance>>,
(_alloc_id, immutable): (AllocId, bool), (_alloc_id, immutable): (AllocId, bool),
range: AllocRange, range: AllocRange,
) -> InterpResult<'tcx> { ) -> InterpResult<'tcx> {

View file

@ -400,6 +400,8 @@ pub trait Machine<'tcx>: Sized {
) -> InterpResult<'tcx, Self::AllocExtra>; ) -> InterpResult<'tcx, Self::AllocExtra>;
/// Hook for performing extra checks on a memory read access. /// Hook for performing extra checks on a memory read access.
/// `ptr` will always be a pointer with the provenance in `prov` pointing to the beginning of
/// `range`.
/// ///
/// This will *not* be called during validation! /// This will *not* be called during validation!
/// ///
@ -413,6 +415,7 @@ pub trait Machine<'tcx>: Sized {
_tcx: TyCtxtAt<'tcx>, _tcx: TyCtxtAt<'tcx>,
_machine: &Self, _machine: &Self,
_alloc_extra: &Self::AllocExtra, _alloc_extra: &Self::AllocExtra,
_ptr: Pointer<Option<Self::Provenance>>,
_prov: (AllocId, Self::ProvenanceExtra), _prov: (AllocId, Self::ProvenanceExtra),
_range: AllocRange, _range: AllocRange,
) -> InterpResult<'tcx> { ) -> InterpResult<'tcx> {
@ -432,11 +435,14 @@ pub trait Machine<'tcx>: Sized {
/// Hook for performing extra checks on a memory write access. /// Hook for performing extra checks on a memory write access.
/// This is not invoked for ZST accesses, as no write actually happens. /// This is not invoked for ZST accesses, as no write actually happens.
/// `ptr` will always be a pointer with the provenance in `prov` pointing to the beginning of
/// `range`.
#[inline(always)] #[inline(always)]
fn before_memory_write( fn before_memory_write(
_tcx: TyCtxtAt<'tcx>, _tcx: TyCtxtAt<'tcx>,
_machine: &mut Self, _machine: &mut Self,
_alloc_extra: &mut Self::AllocExtra, _alloc_extra: &mut Self::AllocExtra,
_ptr: Pointer<Option<Self::Provenance>>,
_prov: (AllocId, Self::ProvenanceExtra), _prov: (AllocId, Self::ProvenanceExtra),
_range: AllocRange, _range: AllocRange,
) -> InterpResult<'tcx> { ) -> InterpResult<'tcx> {
@ -444,11 +450,14 @@ pub trait Machine<'tcx>: Sized {
} }
/// Hook for performing extra operations on a memory deallocation. /// Hook for performing extra operations on a memory deallocation.
/// `ptr` will always be a pointer with the provenance in `prov` pointing to the beginning of
/// the allocation.
#[inline(always)] #[inline(always)]
fn before_memory_deallocation( fn before_memory_deallocation(
_tcx: TyCtxtAt<'tcx>, _tcx: TyCtxtAt<'tcx>,
_machine: &mut Self, _machine: &mut Self,
_alloc_extra: &mut Self::AllocExtra, _alloc_extra: &mut Self::AllocExtra,
_ptr: Pointer<Option<Self::Provenance>>,
_prov: (AllocId, Self::ProvenanceExtra), _prov: (AllocId, Self::ProvenanceExtra),
_size: Size, _size: Size,
_align: Align, _align: Align,

View file

@ -385,6 +385,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
self.tcx, self.tcx,
&mut self.machine, &mut self.machine,
&mut alloc.extra, &mut alloc.extra,
ptr,
(alloc_id, prov), (alloc_id, prov),
size, size,
alloc.align, alloc.align,
@ -727,6 +728,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
self.tcx, self.tcx,
&self.machine, &self.machine,
&alloc.extra, &alloc.extra,
ptr,
(alloc_id, prov), (alloc_id, prov),
range, range,
)?; )?;
@ -816,7 +818,14 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
if let Some((alloc_id, offset, prov, alloc, machine)) = ptr_and_alloc { if let Some((alloc_id, offset, prov, alloc, machine)) = ptr_and_alloc {
let range = alloc_range(offset, size); let range = alloc_range(offset, size);
if !validation_in_progress { if !validation_in_progress {
M::before_memory_write(tcx, machine, &mut alloc.extra, (alloc_id, prov), range)?; M::before_memory_write(
tcx,
machine,
&mut alloc.extra,
ptr,
(alloc_id, prov),
range,
)?;
} }
interp_ok(Some(AllocRefMut { alloc, range, tcx: *tcx, alloc_id })) interp_ok(Some(AllocRefMut { alloc, range, tcx: *tcx, alloc_id }))
} else { } else {
@ -1373,6 +1382,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
tcx, tcx,
&self.machine, &self.machine,
&src_alloc.extra, &src_alloc.extra,
src,
(src_alloc_id, src_prov), (src_alloc_id, src_prov),
src_range, src_range,
)?; )?;
@ -1403,6 +1413,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
tcx, tcx,
extra, extra,
&mut dest_alloc.extra, &mut dest_alloc.extra,
dest,
(dest_alloc_id, dest_prov), (dest_alloc_id, dest_prov),
dest_range, dest_range,
)?; )?;

View file

@ -1366,6 +1366,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
_tcx: TyCtxtAt<'tcx>, _tcx: TyCtxtAt<'tcx>,
machine: &Self, machine: &Self,
alloc_extra: &AllocExtra<'tcx>, alloc_extra: &AllocExtra<'tcx>,
_ptr: Pointer,
(alloc_id, prov_extra): (AllocId, Self::ProvenanceExtra), (alloc_id, prov_extra): (AllocId, Self::ProvenanceExtra),
range: AllocRange, range: AllocRange,
) -> InterpResult<'tcx> { ) -> InterpResult<'tcx> {
@ -1390,6 +1391,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
_tcx: TyCtxtAt<'tcx>, _tcx: TyCtxtAt<'tcx>,
machine: &mut Self, machine: &mut Self,
alloc_extra: &mut AllocExtra<'tcx>, alloc_extra: &mut AllocExtra<'tcx>,
_ptr: Pointer,
(alloc_id, prov_extra): (AllocId, Self::ProvenanceExtra), (alloc_id, prov_extra): (AllocId, Self::ProvenanceExtra),
range: AllocRange, range: AllocRange,
) -> InterpResult<'tcx> { ) -> InterpResult<'tcx> {
@ -1414,6 +1416,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
_tcx: TyCtxtAt<'tcx>, _tcx: TyCtxtAt<'tcx>,
machine: &mut Self, machine: &mut Self,
alloc_extra: &mut AllocExtra<'tcx>, alloc_extra: &mut AllocExtra<'tcx>,
_ptr: Pointer,
(alloc_id, prove_extra): (AllocId, Self::ProvenanceExtra), (alloc_id, prove_extra): (AllocId, Self::ProvenanceExtra),
size: Size, size: Size,
align: Align, align: Align,