Skip to content

Commit 12a9316

Browse files
committed
miri: avoid making a full copy of all new allocations
1 parent 6744092 commit 12a9316

File tree

2 files changed

+19
-37
lines changed

2 files changed

+19
-37
lines changed

src/concurrency/thread.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -862,14 +862,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
862862
if tcx.is_foreign_item(def_id) {
863863
throw_unsup_format!("foreign thread-local statics are not supported");
864864
}
865-
let allocation = this.ctfe_query(|tcx| tcx.eval_static_initializer(def_id))?;
866-
let mut allocation = allocation.inner().clone();
865+
let alloc = this.ctfe_query(|tcx| tcx.eval_static_initializer(def_id))?;
866+
// We make a full copy of this allocation.
867+
let mut alloc = alloc.inner().adjust_from_tcx(&this.tcx, |ptr| this.global_root_pointer(ptr))?;
867868
// This allocation will be deallocated when the thread dies, so it is not in read-only memory.
868-
allocation.mutability = Mutability::Mut;
869+
alloc.mutability = Mutability::Mut;
869870
// Create a fresh allocation with this content.
870-
let new_alloc = this.allocate_raw_ptr(allocation, MiriMemoryKind::Tls.into())?;
871-
this.machine.threads.set_thread_local_alloc(def_id, new_alloc);
872-
Ok(new_alloc)
871+
let ptr = this.allocate_raw_ptr(alloc, MiriMemoryKind::Tls.into())?;
872+
this.machine.threads.set_thread_local_alloc(def_id, ptr);
873+
Ok(ptr)
873874
}
874875
}
875876

src/machine.rs

+12-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Global machine state as well as implementation of the interpreter engine
22
//! `Machine` trait.
33
4-
use std::borrow::Cow;
54
use std::cell::RefCell;
65
use std::collections::hash_map::Entry;
76
use std::fmt;
@@ -1086,40 +1085,33 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
10861085
}
10871086
}
10881087

1089-
fn adjust_allocation<'b>(
1088+
fn init_alloc_extra(
10901089
ecx: &MiriInterpCx<'tcx>,
10911090
id: AllocId,
1092-
alloc: Cow<'b, Allocation>,
1093-
kind: Option<MemoryKind>,
1094-
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>
1095-
{
1096-
let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
1091+
kind: MemoryKind,
1092+
size: Size,
1093+
align: Align,
1094+
) -> InterpResult<'tcx, Self::AllocExtra> {
10971095
if ecx.machine.tracked_alloc_ids.contains(&id) {
1098-
ecx.emit_diagnostic(NonHaltingDiagnostic::CreatedAlloc(
1099-
id,
1100-
alloc.size(),
1101-
alloc.align,
1102-
kind,
1103-
));
1096+
ecx.emit_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id, size, align, kind));
11041097
}
11051098

1106-
let alloc = alloc.into_owned();
11071099
let borrow_tracker = ecx
11081100
.machine
11091101
.borrow_tracker
11101102
.as_ref()
1111-
.map(|bt| bt.borrow_mut().new_allocation(id, alloc.size(), kind, &ecx.machine));
1103+
.map(|bt| bt.borrow_mut().new_allocation(id, size, kind, &ecx.machine));
11121104

1113-
let race_alloc = ecx.machine.data_race.as_ref().map(|data_race| {
1105+
let data_race = ecx.machine.data_race.as_ref().map(|data_race| {
11141106
data_race::AllocState::new_allocation(
11151107
data_race,
11161108
&ecx.machine.threads,
1117-
alloc.size(),
1109+
size,
11181110
kind,
11191111
ecx.machine.current_span(),
11201112
)
11211113
});
1122-
let buffer_alloc = ecx.machine.weak_memory.then(weak_memory::AllocState::new_allocation);
1114+
let weak_memory = ecx.machine.weak_memory.then(weak_memory::AllocState::new_allocation);
11231115

11241116
// If an allocation is leaked, we want to report a backtrace to indicate where it was
11251117
// allocated. We don't need to record a backtrace for allocations which are allowed to
@@ -1130,25 +1122,14 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
11301122
Some(ecx.generate_stacktrace())
11311123
};
11321124

1133-
let alloc: Allocation<Provenance, Self::AllocExtra, Self::Bytes> = alloc.adjust_from_tcx(
1134-
&ecx.tcx,
1135-
AllocExtra {
1136-
borrow_tracker,
1137-
data_race: race_alloc,
1138-
weak_memory: buffer_alloc,
1139-
backtrace,
1140-
},
1141-
|ptr| ecx.global_root_pointer(ptr),
1142-
)?;
1143-
11441125
if matches!(kind, MemoryKind::Machine(kind) if kind.should_save_allocation_span()) {
11451126
ecx.machine
11461127
.allocation_spans
11471128
.borrow_mut()
11481129
.insert(id, (ecx.machine.current_span(), None));
11491130
}
11501131

1151-
Ok(Cow::Owned(alloc))
1132+
Ok(AllocExtra { borrow_tracker, data_race, weak_memory, backtrace })
11521133
}
11531134

11541135
fn adjust_alloc_root_pointer(
@@ -1357,7 +1338,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
13571338
}
13581339

13591340
#[inline(always)]
1360-
fn init_frame_extra(
1341+
fn init_frame(
13611342
ecx: &mut InterpCx<'tcx, Self>,
13621343
frame: Frame<'tcx, Provenance>,
13631344
) -> InterpResult<'tcx, Frame<'tcx, Provenance, FrameExtra<'tcx>>> {

0 commit comments

Comments
 (0)