Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b9f59f6

Browse files
committedMar 20, 2025
interpret memory access hooks: also pass through the Pointer used for the access
1 parent 75530e9 commit b9f59f6

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed
 

‎compiler/rustc_const_eval/src/const_eval/machine.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::errors::{LongRunning, LongRunningWarn};
2222
use crate::fluent_generated as fluent;
2323
use crate::interpret::{
2424
self, AllocId, AllocInit, AllocRange, ConstAllocation, CtfeProvenance, FnArg, Frame,
25-
GlobalAlloc, ImmTy, InterpCx, InterpResult, MPlaceTy, OpTy, RangeSet, Scalar,
25+
GlobalAlloc, ImmTy, InterpCx, InterpResult, MPlaceTy, OpTy, Pointer, RangeSet, Scalar,
2626
compile_time_machine, interp_ok, throw_exhaust, throw_inval, throw_ub, throw_ub_custom,
2727
throw_unsup, throw_unsup_format,
2828
};
@@ -688,6 +688,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
688688
_tcx: TyCtxtAt<'tcx>,
689689
_machine: &mut Self,
690690
_alloc_extra: &mut Self::AllocExtra,
691+
_ptr: Pointer<Option<Self::Provenance>>,
691692
(_alloc_id, immutable): (AllocId, bool),
692693
range: AllocRange,
693694
) -> InterpResult<'tcx> {

‎compiler/rustc_const_eval/src/interpret/machine.rs

+9
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ pub trait Machine<'tcx>: Sized {
400400
) -> InterpResult<'tcx, Self::AllocExtra>;
401401

402402
/// Hook for performing extra checks on a memory read access.
403+
/// `ptr` will always be a pointer with the provenance in `prov` pointing to the beginning of
404+
/// `range`.
403405
///
404406
/// This will *not* be called during validation!
405407
///
@@ -413,6 +415,7 @@ pub trait Machine<'tcx>: Sized {
413415
_tcx: TyCtxtAt<'tcx>,
414416
_machine: &Self,
415417
_alloc_extra: &Self::AllocExtra,
418+
_ptr: Pointer<Option<Self::Provenance>>,
416419
_prov: (AllocId, Self::ProvenanceExtra),
417420
_range: AllocRange,
418421
) -> InterpResult<'tcx> {
@@ -432,23 +435,29 @@ pub trait Machine<'tcx>: Sized {
432435

433436
/// Hook for performing extra checks on a memory write access.
434437
/// This is not invoked for ZST accesses, as no write actually happens.
438+
/// `ptr` will always be a pointer with the provenance in `prov` pointing to the beginning of
439+
/// `range`.
435440
#[inline(always)]
436441
fn before_memory_write(
437442
_tcx: TyCtxtAt<'tcx>,
438443
_machine: &mut Self,
439444
_alloc_extra: &mut Self::AllocExtra,
445+
_ptr: Pointer<Option<Self::Provenance>>,
440446
_prov: (AllocId, Self::ProvenanceExtra),
441447
_range: AllocRange,
442448
) -> InterpResult<'tcx> {
443449
interp_ok(())
444450
}
445451

446452
/// Hook for performing extra operations on a memory deallocation.
453+
/// `ptr` will always be a pointer with the provenance in `prov` pointing to the beginning of
454+
/// the allocation.
447455
#[inline(always)]
448456
fn before_memory_deallocation(
449457
_tcx: TyCtxtAt<'tcx>,
450458
_machine: &mut Self,
451459
_alloc_extra: &mut Self::AllocExtra,
460+
_ptr: Pointer<Option<Self::Provenance>>,
452461
_prov: (AllocId, Self::ProvenanceExtra),
453462
_size: Size,
454463
_align: Align,

‎compiler/rustc_const_eval/src/interpret/memory.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
385385
self.tcx,
386386
&mut self.machine,
387387
&mut alloc.extra,
388+
ptr,
388389
(alloc_id, prov),
389390
size,
390391
alloc.align,
@@ -727,6 +728,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
727728
self.tcx,
728729
&self.machine,
729730
&alloc.extra,
731+
ptr,
730732
(alloc_id, prov),
731733
range,
732734
)?;
@@ -816,7 +818,14 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
816818
if let Some((alloc_id, offset, prov, alloc, machine)) = ptr_and_alloc {
817819
let range = alloc_range(offset, size);
818820
if !validation_in_progress {
819-
M::before_memory_write(tcx, machine, &mut alloc.extra, (alloc_id, prov), range)?;
821+
M::before_memory_write(
822+
tcx,
823+
machine,
824+
&mut alloc.extra,
825+
ptr,
826+
(alloc_id, prov),
827+
range,
828+
)?;
820829
}
821830
interp_ok(Some(AllocRefMut { alloc, range, tcx: *tcx, alloc_id }))
822831
} else {
@@ -1373,6 +1382,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
13731382
tcx,
13741383
&self.machine,
13751384
&src_alloc.extra,
1385+
src,
13761386
(src_alloc_id, src_prov),
13771387
src_range,
13781388
)?;
@@ -1403,6 +1413,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
14031413
tcx,
14041414
extra,
14051415
&mut dest_alloc.extra,
1416+
dest,
14061417
(dest_alloc_id, dest_prov),
14071418
dest_range,
14081419
)?;

‎src/tools/miri/src/machine.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
13661366
_tcx: TyCtxtAt<'tcx>,
13671367
machine: &Self,
13681368
alloc_extra: &AllocExtra<'tcx>,
1369+
_ptr: Pointer,
13691370
(alloc_id, prov_extra): (AllocId, Self::ProvenanceExtra),
13701371
range: AllocRange,
13711372
) -> InterpResult<'tcx> {
@@ -1390,6 +1391,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
13901391
_tcx: TyCtxtAt<'tcx>,
13911392
machine: &mut Self,
13921393
alloc_extra: &mut AllocExtra<'tcx>,
1394+
_ptr: Pointer,
13931395
(alloc_id, prov_extra): (AllocId, Self::ProvenanceExtra),
13941396
range: AllocRange,
13951397
) -> InterpResult<'tcx> {
@@ -1414,6 +1416,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
14141416
_tcx: TyCtxtAt<'tcx>,
14151417
machine: &mut Self,
14161418
alloc_extra: &mut AllocExtra<'tcx>,
1419+
_ptr: Pointer,
14171420
(alloc_id, prove_extra): (AllocId, Self::ProvenanceExtra),
14181421
size: Size,
14191422
align: Align,

0 commit comments

Comments
 (0)
Failed to load comments.