Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions rts/motoko-rts-tests/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ fn test_gc(
) {
let heap = MotokoHeap::new(refs, roots, closure_table, gc);

// Check `check_dynamic_heap` sanity
// Check `create_dynamic_heap` sanity
check_dynamic_heap(
false, // before gc
refs,
roots,
closure_table,
Expand All @@ -85,6 +86,7 @@ fn test_gc(
let heap_ptr_offset = heap.heap_ptr_offset();
let closure_table_ptr_offset = heap.closure_table_ptr_offset();
check_dynamic_heap(
true, // after gc
refs,
roots,
closure_table,
Expand All @@ -98,14 +100,15 @@ fn test_gc(

/// Check the dynamic heap:
///
/// - All and only reachable objects should be in the heap. Reachable objects are those in the
/// transitive closure of roots.
/// - All (and in post-gc mode, only) reachable objects should be in the heap. Reachable objects
/// are those in the transitive closure of roots.
///
/// - Objects should point to right objects. E.g. if object with index X points to objects with
/// indices Y and Z in the `objects` map, it should point to objects with indices Y and Z on the
/// heap.
///
fn check_dynamic_heap(
post_gc: bool,
objects: &HashMap<ObjectIdx, Vec<ObjectIdx>>,
roots: &[ObjectIdx],
closure_table: &[ObjectIdx],
Expand Down Expand Up @@ -197,34 +200,37 @@ fn check_dynamic_heap(
.copied()
.collect();

// Unreachable objects that we've seen in the heap
let extra_objects: Vec<ObjectIdx> = seen_objects
.difference(&reachable_objects)
.copied()
.collect();

let mut error_message = String::new();

if !missing_objects.is_empty() {
write!(
&mut error_message,
"Reachable objects missing in the post-GC heap: {:?}",
"Reachable objects missing in the {} heap: {:?}",
if post_gc { "post-gc" } else { "pre-gc" },
missing_objects,
)
.unwrap();
}

if !extra_objects.is_empty() {
if !error_message.is_empty() {
error_message.push('\n');
}
if post_gc {
// Unreachable objects that we've seen in the heap
let extra_objects: Vec<ObjectIdx> = seen_objects
.difference(&reachable_objects)
.copied()
.collect();

write!(
&mut error_message,
"Unreachable objects seen in the post-GC heap: {:?}",
extra_objects,
)
.unwrap();
if !extra_objects.is_empty() {
if !error_message.is_empty() {
error_message.push('\n');
}

write!(
&mut error_message,
"Unreachable objects seen in the post-GC heap: {:?}",
extra_objects,
)
.unwrap();
}
}

if !error_message.is_empty() {
Expand Down