Skip to content

Commit

Permalink
Extra assertions for mutators and epilogues (#1182)
Browse files Browse the repository at this point in the history
This commit adds assertions about the number of mutators and the list of
mutators returned from the VM binding.

Also asserts that epilogues are not silently dropped before they are
executed. It could happen if a buggy VM binding returns a number of
mutators that does not match the actual list of mutators.

This should detect bugs like mmtk/mmtk-ruby#84
  • Loading branch information
wks committed Aug 2, 2024
1 parent f9b85bc commit f785236
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/plan/marksweep/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ impl<VM: VMBinding> Plan for MarkSweep<VM> {
self.common.release(tls, true);
}

fn end_of_gc(&mut self, _tls: VMWorkerThread) {
self.ms.end_of_gc();
}

fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
self.base().collection_required(self, space_full)
}
Expand Down
8 changes: 7 additions & 1 deletion src/policy/immix/immixspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::policy::sft_map::SFTMap;
use crate::policy::space::{CommonSpace, Space};
use crate::util::alloc::allocator::AllocatorContext;
use crate::util::constants::LOG_BYTES_IN_PAGE;
use crate::util::copy::*;
use crate::util::heap::chunk_map::*;
use crate::util::heap::BlockPageResource;
use crate::util::heap::PageResource;
Expand All @@ -19,6 +18,7 @@ use crate::util::metadata::side_metadata::SideMetadataSpec;
use crate::util::metadata::vo_bit;
use crate::util::metadata::{self, MetadataSpec};
use crate::util::object_forwarding;
use crate::util::{copy::*, epilogue};
use crate::util::{Address, ObjectReference};
use crate::vm::*;
use crate::{
Expand Down Expand Up @@ -979,6 +979,12 @@ impl<VM: VMBinding> FlushPageResource<VM> {
}
}

impl<VM: VMBinding> Drop for FlushPageResource<VM> {
fn drop(&mut self) {
epilogue::debug_assert_counter_zero(&self.counter, "FlushPageResource::counter");
}
}

use crate::policy::copy_context::PolicyCopyContext;
use crate::util::alloc::Allocator;
use crate::util::alloc::ImmixAllocator;
Expand Down
14 changes: 14 additions & 0 deletions src/policy/marksweepspace/native_ms/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
scheduler::{GCWorkScheduler, GCWorker, WorkBucketStage},
util::{
copy::CopySemantics,
epilogue,
heap::{BlockPageResource, PageResource},
metadata::{self, side_metadata::SideMetadataSpec, MetadataSpec},
ObjectReference,
Expand Down Expand Up @@ -373,6 +374,13 @@ impl<VM: VMBinding> MarkSweepSpace<VM> {
self.scheduler.work_buckets[crate::scheduler::WorkBucketStage::Release].add(work_packet);
}

pub fn end_of_gc(&mut self) {
epilogue::debug_assert_counter_zero(
&self.pending_release_packets,
"pending_release_packets",
);
}

/// Release a block.
pub fn release_block(&self, block: Block) {
self.block_clear_metadata(block);
Expand Down Expand Up @@ -581,3 +589,9 @@ impl<VM: VMBinding> RecycleBlocks<VM> {
}
}
}

impl<VM: VMBinding> Drop for RecycleBlocks<VM> {
fn drop(&mut self) {
epilogue::debug_assert_counter_zero(&self.counter, "RecycleBlocks::counter");
}
}
28 changes: 20 additions & 8 deletions src/scheduler/gc_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ impl<C: GCWorkContext> GCWork<C::VM> for Prepare<C> {
plan_mut.prepare(worker.tls);

if plan_mut.constraints().needs_prepare_mutator {
for mutator in <C::VM as VMBinding>::VMActivePlan::mutators() {
mmtk.scheduler.work_buckets[WorkBucketStage::Prepare]
.add(PrepareMutator::<C::VM>::new(mutator));
}
let prepare_mutator_packets = <C::VM as VMBinding>::VMActivePlan::mutators()
.map(|mutator| Box::new(PrepareMutator::<C::VM>::new(mutator)) as _)
.collect::<Vec<_>>();
// Just in case the VM binding is inconsistent about the number of mutators and the actual mutator list.
debug_assert_eq!(
prepare_mutator_packets.len(),
<C::VM as VMBinding>::VMActivePlan::number_of_mutators()
);
mmtk.scheduler.work_buckets[WorkBucketStage::Prepare].bulk_add(prepare_mutator_packets);
}

for w in &mmtk.scheduler.worker_group.workers_shared {
let result = w.designated_work.push(Box::new(PrepareCollector));
debug_assert!(result.is_ok());
Expand Down Expand Up @@ -133,10 +139,16 @@ impl<C: GCWorkContext + 'static> GCWork<C::VM> for Release<C> {
let plan_mut: &mut C::PlanType = unsafe { &mut *(self.plan as *const _ as *mut _) };
plan_mut.release(worker.tls);

for mutator in <C::VM as VMBinding>::VMActivePlan::mutators() {
mmtk.scheduler.work_buckets[WorkBucketStage::Release]
.add(ReleaseMutator::<C::VM>::new(mutator));
}
let release_mutator_packets = <C::VM as VMBinding>::VMActivePlan::mutators()
.map(|mutator| Box::new(ReleaseMutator::<C::VM>::new(mutator)) as _)
.collect::<Vec<_>>();
// Just in case the VM binding is inconsistent about the number of mutators and the actual mutator list.
debug_assert_eq!(
release_mutator_packets.len(),
<C::VM as VMBinding>::VMActivePlan::number_of_mutators()
);
mmtk.scheduler.work_buckets[WorkBucketStage::Release].bulk_add(release_mutator_packets);

for w in &mmtk.scheduler.worker_group.workers_shared {
let result = w.designated_work.push(Box::new(ReleaseCollector));
debug_assert!(result.is_ok());
Expand Down
13 changes: 13 additions & 0 deletions src/util/epilogue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Utilities for implementing epilogues.
//!
//! Epilogues are operations that are done when several other operations are done.

use std::sync::atomic::{AtomicUsize, Ordering};

/// A debugging method for detecting the case when the epilogue is never executed.
pub fn debug_assert_counter_zero(counter: &AtomicUsize, what: &'static str) {
let value = counter.load(Ordering::SeqCst);
if value != 0 {
panic!("{what} is still {value}.");
}
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub mod test_util;
/// An analysis framework for collecting data and profiling in GC.
#[cfg(feature = "analysis")]
pub(crate) mod analysis;
pub(crate) mod epilogue;
/// Non-generic refs to generic types of `<VM>`.
pub(crate) mod erase_vm;
/// Finalization implementation.
Expand Down

0 comments on commit f785236

Please sign in to comment.