diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-03-13 17:44:09 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-13 17:44:09 +0100 |
| commit | 9339bc61c921ae8e55e6bcbb8318b9adaa1289c2 (patch) | |
| tree | b48589772ceb1796f4883ab9d3d6e28fc25019b3 /src | |
| parent | 6db8e5ade5319eb9adbe9593e8ac6a1e33f77b41 (diff) | |
| parent | 03c1b43d9ed36f5ba52c356ce685088e3838740d (diff) | |
| download | rust-9339bc61c921ae8e55e6bcbb8318b9adaa1289c2.tar.gz rust-9339bc61c921ae8e55e6bcbb8318b9adaa1289c2.zip | |
Rollup merge of #138417 - RalfJung:interpret-cleanup, r=oli-obk
minor interpreter cleanups - remove the `eval_inline_asm` hook that `@saethlin` added; the usage never materialized and he agreed with removing it - I tried merging `init_alloc_extra` and `adjust_global_allocation` and it didn't work; leave a comment as to why. Also, make the allocation code path a bit more clear by renaming `init_alloc_extra` to `init_local_allocation`. r? `@oli-obk`
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/miri/src/alloc_addresses/mod.rs | 3 | ||||
| -rw-r--r-- | src/tools/miri/src/machine.rs | 107 |
2 files changed, 60 insertions, 50 deletions
diff --git a/src/tools/miri/src/alloc_addresses/mod.rs b/src/tools/miri/src/alloc_addresses/mod.rs index 5d257029a46..0a2d3ac63a7 100644 --- a/src/tools/miri/src/alloc_addresses/mod.rs +++ b/src/tools/miri/src/alloc_addresses/mod.rs @@ -377,7 +377,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { fn get_global_alloc_bytes( &self, id: AllocId, - kind: MemoryKind, bytes: &[u8], align: Align, ) -> InterpResult<'tcx, MiriAllocBytes> { @@ -386,7 +385,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // In native lib mode, MiriAllocBytes for global allocations are handled via `prepared_alloc_bytes`. // This additional call ensures that some `MiriAllocBytes` are always prepared, just in case // this function gets called before the first time `addr_from_alloc_id` gets called. - this.addr_from_alloc_id(id, kind)?; + this.addr_from_alloc_id(id, MiriMemoryKind::Global.into())?; // The memory we need here will have already been allocated during an earlier call to // `addr_from_alloc_id` for this allocation. So don't create a new `MiriAllocBytes` here, instead // fetch the previously prepared bytes from `prepared_alloc_bytes`. diff --git a/src/tools/miri/src/machine.rs b/src/tools/miri/src/machine.rs index dbb092f6728..90beffbf830 100644 --- a/src/tools/miri/src/machine.rs +++ b/src/tools/miri/src/machine.rs @@ -814,6 +814,59 @@ impl<'tcx> MiriMachine<'tcx> { .and_then(|(_allocated, deallocated)| *deallocated) .map(Span::data) } + + fn init_allocation( + ecx: &MiriInterpCx<'tcx>, + id: AllocId, + kind: MemoryKind, + size: Size, + align: Align, + ) -> InterpResult<'tcx, AllocExtra<'tcx>> { + if ecx.machine.tracked_alloc_ids.contains(&id) { + ecx.emit_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id, size, align, kind)); + } + + let borrow_tracker = ecx + .machine + .borrow_tracker + .as_ref() + .map(|bt| bt.borrow_mut().new_allocation(id, size, kind, &ecx.machine)); + + let data_race = ecx.machine.data_race.as_ref().map(|data_race| { + data_race::AllocState::new_allocation( + data_race, + &ecx.machine.threads, + size, + kind, + ecx.machine.current_span(), + ) + }); + let weak_memory = ecx.machine.weak_memory.then(weak_memory::AllocState::new_allocation); + + // If an allocation is leaked, we want to report a backtrace to indicate where it was + // allocated. We don't need to record a backtrace for allocations which are allowed to + // leak. + let backtrace = if kind.may_leak() || !ecx.machine.collect_leak_backtraces { + None + } else { + Some(ecx.generate_stacktrace()) + }; + + if matches!(kind, MemoryKind::Machine(kind) if kind.should_save_allocation_span()) { + ecx.machine + .allocation_spans + .borrow_mut() + .insert(id, (ecx.machine.current_span(), None)); + } + + interp_ok(AllocExtra { + borrow_tracker, + data_race, + weak_memory, + backtrace, + sync: FxHashMap::default(), + }) + } } impl VisitProvenance for MiriMachine<'_> { @@ -1200,57 +1253,15 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> { } } - fn init_alloc_extra( + fn init_local_allocation( ecx: &MiriInterpCx<'tcx>, id: AllocId, kind: MemoryKind, size: Size, align: Align, ) -> InterpResult<'tcx, Self::AllocExtra> { - if ecx.machine.tracked_alloc_ids.contains(&id) { - ecx.emit_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id, size, align, kind)); - } - - let borrow_tracker = ecx - .machine - .borrow_tracker - .as_ref() - .map(|bt| bt.borrow_mut().new_allocation(id, size, kind, &ecx.machine)); - - let data_race = ecx.machine.data_race.as_ref().map(|data_race| { - data_race::AllocState::new_allocation( - data_race, - &ecx.machine.threads, - size, - kind, - ecx.machine.current_span(), - ) - }); - let weak_memory = ecx.machine.weak_memory.then(weak_memory::AllocState::new_allocation); - - // If an allocation is leaked, we want to report a backtrace to indicate where it was - // allocated. We don't need to record a backtrace for allocations which are allowed to - // leak. - let backtrace = if kind.may_leak() || !ecx.machine.collect_leak_backtraces { - None - } else { - Some(ecx.generate_stacktrace()) - }; - - if matches!(kind, MemoryKind::Machine(kind) if kind.should_save_allocation_span()) { - ecx.machine - .allocation_spans - .borrow_mut() - .insert(id, (ecx.machine.current_span(), None)); - } - - interp_ok(AllocExtra { - borrow_tracker, - data_race, - weak_memory, - backtrace, - sync: FxHashMap::default(), - }) + assert!(kind != MiriMemoryKind::Global.into()); + MiriMachine::init_allocation(ecx, id, kind, size, align) } fn adjust_alloc_root_pointer( @@ -1340,13 +1351,13 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> { alloc: &'b Allocation, ) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>> { - let kind = Self::GLOBAL_KIND.unwrap().into(); let alloc = alloc.adjust_from_tcx( &ecx.tcx, - |bytes, align| ecx.get_global_alloc_bytes(id, kind, bytes, align), + |bytes, align| ecx.get_global_alloc_bytes(id, bytes, align), |ptr| ecx.global_root_pointer(ptr), )?; - let extra = Self::init_alloc_extra(ecx, id, kind, alloc.size(), alloc.align)?; + let kind = MiriMemoryKind::Global.into(); + let extra = MiriMachine::init_allocation(ecx, id, kind, alloc.size(), alloc.align)?; interp_ok(Cow::Owned(alloc.with_extra(extra))) } |
