Skip to content

Commit 2adbf38

Browse files
authored
Rollup merge of #131593 - RalfJung:alloc-no-clone, r=saethlin
miri: avoid cloning AllocExtra We shouldn't be cloning Miri allocations, so make `AllocExtra::clone` panic instead, and adjust the one case where we *do* clone (the leak check) to avoid cloning. This is in preparation for rust-lang#3966 where I am adding something to `AllocExtra` that cannot (easily) be cloned. r? ``@saethlin``
2 parents ff7418f + 9542cc1 commit 2adbf38

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,14 +473,14 @@ pub fn report_leaks<'tcx>(
473473
leaks: Vec<(AllocId, MemoryKind, Allocation<Provenance, AllocExtra<'tcx>, MiriAllocBytes>)>,
474474
) {
475475
let mut any_pruned = false;
476-
for (id, kind, mut alloc) in leaks {
476+
for (id, kind, alloc) in leaks {
477477
let mut title = format!(
478478
"memory leaked: {id:?} ({}, size: {:?}, align: {:?})",
479479
kind,
480480
alloc.size().bytes(),
481481
alloc.align.bytes()
482482
);
483-
let Some(backtrace) = alloc.extra.backtrace.take() else {
483+
let Some(backtrace) = alloc.extra.backtrace else {
484484
ecx.tcx.dcx().err(title);
485485
continue;
486486
};

src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ pub fn eval_entry<'tcx>(
476476
}
477477
// Check for memory leaks.
478478
info!("Additional static roots: {:?}", ecx.machine.static_roots);
479-
let leaks = ecx.find_leaked_allocations(&ecx.machine.static_roots);
479+
let leaks = ecx.take_leaked_allocations(|ecx| &ecx.machine.static_roots);
480480
if !leaks.is_empty() {
481481
report_leaks(&ecx, leaks);
482482
tcx.dcx().note("set `MIRIFLAGS=-Zmiri-ignore-leaks` to disable this check");

src/machine.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl ProvenanceExtra {
321321
}
322322

323323
/// Extra per-allocation data
324-
#[derive(Debug, Clone)]
324+
#[derive(Debug)]
325325
pub struct AllocExtra<'tcx> {
326326
/// Global state of the borrow tracker, if enabled.
327327
pub borrow_tracker: Option<borrow_tracker::AllocState>,
@@ -338,6 +338,14 @@ pub struct AllocExtra<'tcx> {
338338
pub backtrace: Option<Vec<FrameInfo<'tcx>>>,
339339
}
340340

341+
// We need a `Clone` impl because the machine passes `Allocation` through `Cow`...
342+
// but that should never end up actually cloning our `AllocExtra`.
343+
impl<'tcx> Clone for AllocExtra<'tcx> {
344+
fn clone(&self) -> Self {
345+
panic!("our allocations should never be cloned");
346+
}
347+
}
348+
341349
impl VisitProvenance for AllocExtra<'_> {
342350
fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
343351
let AllocExtra { borrow_tracker, data_race, weak_memory, backtrace: _ } = self;

0 commit comments

Comments
 (0)