Skip to content

Commit ebda1de

Browse files
committed
Auto merge of #2397 - RalfJung:cleanup, r=RalfJung
only do env var cleanup if all threads have stopped Hopefully fixes #2396
2 parents ddde70c + 5fbf036 commit ebda1de

File tree

2 files changed

+45
-46
lines changed

2 files changed

+45
-46
lines changed

src/concurrency/data_race.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -436,45 +436,6 @@ impl MemoryCellClocks {
436436
/// Evaluation context extensions.
437437
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {}
438438
pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
439-
/// Temporarily allow data-races to occur. This should only be used in
440-
/// one of these cases:
441-
/// - One of the appropriate `validate_atomic` functions will be called to
442-
/// to treat a memory access as atomic.
443-
/// - The memory being accessed should be treated as internal state, that
444-
/// cannot be accessed by the interpreted program.
445-
/// - Execution of the interpreted program execution has halted.
446-
#[inline]
447-
fn allow_data_races_ref<R>(&self, op: impl FnOnce(&MiriEvalContext<'mir, 'tcx>) -> R) -> R {
448-
let this = self.eval_context_ref();
449-
if let Some(data_race) = &this.machine.data_race {
450-
data_race.ongoing_action_data_race_free.set(true);
451-
}
452-
let result = op(this);
453-
if let Some(data_race) = &this.machine.data_race {
454-
data_race.ongoing_action_data_race_free.set(false);
455-
}
456-
result
457-
}
458-
459-
/// Same as `allow_data_races_ref`, this temporarily disables any data-race detection and
460-
/// so should only be used for atomic operations or internal state that the program cannot
461-
/// access.
462-
#[inline]
463-
fn allow_data_races_mut<R>(
464-
&mut self,
465-
op: impl FnOnce(&mut MiriEvalContext<'mir, 'tcx>) -> R,
466-
) -> R {
467-
let this = self.eval_context_mut();
468-
if let Some(data_race) = &this.machine.data_race {
469-
data_race.ongoing_action_data_race_free.set(true);
470-
}
471-
let result = op(this);
472-
if let Some(data_race) = &this.machine.data_race {
473-
data_race.ongoing_action_data_race_free.set(false);
474-
}
475-
result
476-
}
477-
478439
/// Atomic variant of read_scalar_at_offset.
479440
fn read_scalar_at_offset_atomic(
480441
&self,
@@ -1044,6 +1005,45 @@ impl VClockAlloc {
10441005

10451006
impl<'mir, 'tcx: 'mir> EvalContextPrivExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {}
10461007
trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
1008+
/// Temporarily allow data-races to occur. This should only be used in
1009+
/// one of these cases:
1010+
/// - One of the appropriate `validate_atomic` functions will be called to
1011+
/// to treat a memory access as atomic.
1012+
/// - The memory being accessed should be treated as internal state, that
1013+
/// cannot be accessed by the interpreted program.
1014+
/// - Execution of the interpreted program execution has halted.
1015+
#[inline]
1016+
fn allow_data_races_ref<R>(&self, op: impl FnOnce(&MiriEvalContext<'mir, 'tcx>) -> R) -> R {
1017+
let this = self.eval_context_ref();
1018+
if let Some(data_race) = &this.machine.data_race {
1019+
data_race.ongoing_action_data_race_free.set(true);
1020+
}
1021+
let result = op(this);
1022+
if let Some(data_race) = &this.machine.data_race {
1023+
data_race.ongoing_action_data_race_free.set(false);
1024+
}
1025+
result
1026+
}
1027+
1028+
/// Same as `allow_data_races_ref`, this temporarily disables any data-race detection and
1029+
/// so should only be used for atomic operations or internal state that the program cannot
1030+
/// access.
1031+
#[inline]
1032+
fn allow_data_races_mut<R>(
1033+
&mut self,
1034+
op: impl FnOnce(&mut MiriEvalContext<'mir, 'tcx>) -> R,
1035+
) -> R {
1036+
let this = self.eval_context_mut();
1037+
if let Some(data_race) = &this.machine.data_race {
1038+
data_race.ongoing_action_data_race_free.set(true);
1039+
}
1040+
let result = op(this);
1041+
if let Some(data_race) = &this.machine.data_race {
1042+
data_race.ongoing_action_data_race_free.set(false);
1043+
}
1044+
result
1045+
}
1046+
10471047
/// Generic atomic operation implementation
10481048
fn validate_atomic_op<A: Debug + Copy>(
10491049
&self,

src/eval.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,12 @@ pub fn eval_entry<'tcx>(
363363
panic::resume_unwind(panic_payload)
364364
});
365365

366-
// Machine cleanup.
367-
// Execution of the program has halted so any memory access we do here
368-
// cannot produce a real data race. If we do not do something to disable
369-
// data race detection here, some uncommon combination of errors will
370-
// cause a data race to be detected:
371-
// https://github.com/rust-lang/miri/issues/2020
372-
ecx.allow_data_races_mut(|ecx| EnvVars::cleanup(ecx).unwrap());
366+
// Machine cleanup. Only do this if all threads have terminated; threads that are still running
367+
// might cause data races (https://github.com/rust-lang/miri/issues/2020) or Stacked Borrows
368+
// errors (https://github.com/rust-lang/miri/issues/2396) if we deallocate here.
369+
if ecx.have_all_terminated() {
370+
EnvVars::cleanup(&mut ecx).unwrap();
371+
}
373372

374373
// Process the result.
375374
match res {

0 commit comments

Comments
 (0)