Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog.d/11363-gc-test-map-fixture-residue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **Tests (gc):** fixed an intermittent SIGSEGV in the full `perry-runtime` lib suite (#11362). It surfaced in `gc::tests::barrier::test_dirty_page_map_entry_scan_is_external_range_bounded` and `test_incremental_barrier_marks_external_map_and_set_slots`. The cause was a test fixture, not the collector. The hand-built old-generation Map fixture got its header from `arena_alloc_gc_old` and initialised only `size`, `used`, `capacity` and `entries`. That allocator reuses swept holes without zeroing, so `MapHeader.store` held an earlier test's bytes (`0x7878…`, a `"xxx…"` string). The thread-exit arena drop (`walk_block_maps` → `drop_map_store_at_thread_exit`) then `Box::from_raw`'d that value after the test had passed. The fixture now lives once in `gc/tests/support.rs` and zeroes the whole header first. The Set fixture's residue `meta`/`used` get the same treatment. A new regression test poisons the header with `0x78` before initialising it.
84 changes: 53 additions & 31 deletions crates/perry-runtime/src/gc/tests/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,6 @@ use super::super::barrier::RememberedSetClearState;
use super::super::*;
use super::support::*;

unsafe fn alloc_old_test_map(
capacity: u32,
) -> (*mut crate::map::MapHeader, *mut u64, std::alloc::Layout) {
let map = crate::arena::arena_alloc_gc_old(
std::mem::size_of::<crate::map::MapHeader>(),
8,
GC_TYPE_MAP,
) as *mut crate::map::MapHeader;
let layout = std::alloc::Layout::from_size_align((capacity as usize * 16).max(8), 8)
.expect("valid map entries layout");
let entries = std::alloc::alloc_zeroed(layout) as *mut u64;
assert!(!entries.is_null());
(*map).size = 0;
(*map).used = 0;
(*map).capacity = capacity;
(*map).entries = entries as *mut f64;
(map, entries, layout)
}

unsafe fn retire_old_test_map(
map: *mut crate::map::MapHeader,
entries: *mut u64,
layout: std::alloc::Layout,
) {
(*map).size = 0;
(*map).used = 0;
(*map).capacity = 0;
(*map).entries = std::ptr::null_mut();
std::alloc::dealloc(entries as *mut u8, layout);
}

unsafe fn field_index_not_on_last_page(fields: *mut u64, field_count: u32) -> usize {
assert!(field_count > 1);
let last_page =
Expand Down Expand Up @@ -926,6 +895,59 @@ fn test_dirty_page_scan_dedupes_object_spanning_dirty_pages() {
remembered_set_clear();
}

/// #11362: the hand-built fixture Map must not inherit recycled arena bytes.
/// `arena_alloc_gc_old` does not zero, so poison the payload the way a
/// previous occupant would (the suite's crash carried `0x7878…`) and require
/// the fixture initialiser to leave the owned `store` box and the traced
/// `meta` edge null. A non-null `store` is freed by the thread-exit arena
/// drop, which is how the entry-scan test SIGSEGV'd after it had passed.
#[test]
fn old_test_map_fixture_initialises_every_header_word_over_recycled_bytes() {
let _guard = GcTestIsolationGuard::new();
let map = crate::arena::arena_alloc_gc_old(
std::mem::size_of::<crate::map::MapHeader>(),
8,
GC_TYPE_MAP,
) as *mut crate::map::MapHeader;
let layout = std::alloc::Layout::from_size_align(16 * 4, 8).unwrap();
let entries = unsafe { std::alloc::alloc_zeroed(layout) } as *mut u64;
assert!(!entries.is_null());
let (store, meta, size, used, epoch) = unsafe {
std::ptr::write_bytes(
map as *mut u8,
0x78,
std::mem::size_of::<crate::map::MapHeader>(),
);
init_test_map_header(map, entries, 4);
let observed = (
crate::map::test_map_store_word(map),
(*map).meta as usize,
(*map).size,
(*map).used,
(*map).compaction_epoch,
);
// Scrub BEFORE asserting: a failing arm must fail as a test, not as a
// thread-exit SIGSEGV that takes the whole suite down with it.
std::ptr::write_bytes(
map as *mut u8,
0,
std::mem::size_of::<crate::map::MapHeader>(),
);
retire_old_test_map(map, entries, layout);
observed
};
assert_eq!(
store, 0,
"fixture left recycled bytes in MapHeader.store; the thread-exit \
arena drop would Box::from_raw them"
);
assert_eq!(
meta, 0,
"fixture left recycled bytes in the traced meta edge"
);
assert_eq!((size, used, epoch), (0, 0, 0));
}

#[test]
fn test_dirty_page_map_entry_scan_is_external_range_bounded() {
let _guard = GcTestIsolationGuard::new();
Expand Down
31 changes: 0 additions & 31 deletions crates/perry-runtime/src/gc/tests/helper_stores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,6 @@ use std::fmt::Write as _;
const OLD_BORN_FIELDS: u32 =
(crate::gc::LARGE_OBJECT_STORAGE_YOUNG_BIRTH_CEILING_BYTES / 8) as u32 + 64;

unsafe fn alloc_old_test_map(
capacity: u32,
) -> (*mut crate::map::MapHeader, *mut u64, std::alloc::Layout) {
let map = crate::arena::arena_alloc_gc_old(
std::mem::size_of::<crate::map::MapHeader>(),
8,
GC_TYPE_MAP,
) as *mut crate::map::MapHeader;
let layout = std::alloc::Layout::from_size_align((capacity as usize * 16).max(8), 8)
.expect("valid map entries layout");
let entries = std::alloc::alloc_zeroed(layout) as *mut u64;
assert!(!entries.is_null());
(*map).size = 0;
(*map).used = 0;
(*map).capacity = capacity;
(*map).entries = entries as *mut f64;
(map, entries, layout)
}

unsafe fn retire_old_test_map(
map: *mut crate::map::MapHeader,
entries: *mut u64,
layout: std::alloc::Layout,
) {
(*map).size = 0;
(*map).used = 0;
(*map).capacity = 0;
(*map).entries = std::ptr::null_mut();
std::alloc::dealloc(entries as *mut u8, layout);
}

fn assert_verified_copied_minor(trace: &GcCycleTrace) {
assert_copied_minor_trace(trace, true, CopiedMinorFallbackReason::None, false);
assert!(
Expand Down
67 changes: 66 additions & 1 deletion crates/perry-runtime/src/gc/tests/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,14 @@ pub(super) unsafe fn alloc_old_test_set(
.expect("valid set elements layout");
let elements = std::alloc::alloc_zeroed(layout) as *mut u64;
assert!(!elements.is_null());
(*set).size = 0;
// #11362: the arena does not zero recycled holes; leaving `meta` (a
// traced edge) or `used` (the scanned extent) as residue is the same bug
// as the Map fixture's `store`. Zero the whole payload first.
std::ptr::write_bytes(
set as *mut u8,
0,
std::mem::size_of::<crate::set::SetHeader>(),
);
(*set).capacity = capacity;
(*set).elements = elements as *mut f64;
(set, elements, layout)
Expand Down Expand Up @@ -977,3 +984,61 @@ pub(super) fn old_test_header_and_size(user: usize) -> (*mut GcHeader, usize) {
let total = unsafe { (*header).size as usize };
(header, total)
}

/// Allocate an OLD-generation `MapHeader` for a remembered-set fixture, with a
/// plain zeroed malloc block as its entries buffer (the test owns it; pair
/// with [`retire_old_test_map`]).
///
/// #11362: `arena_alloc_gc_old` reuses swept same-size holes and bump space
/// WITHOUT zeroing, so every header word the fixture does not write carries
/// whatever the previous occupant left there. The old per-file copies of this
/// fixture wrote only `size`/`used`/`capacity`/`entries`, leaving the owned
/// `store` box and the traced `meta` edge as residue from an earlier test. The
/// thread-exit arena drop (`walk_block_maps` → `drop_map_store_at_thread_exit`)
/// then `Box::from_raw`'d that residue — `0x7878…`, an earlier test's `"xxx…"`
/// string bytes — and SIGSEGV'd the whole suite after the test had passed.
pub(super) unsafe fn alloc_old_test_map(
capacity: u32,
) -> (*mut crate::map::MapHeader, *mut u64, std::alloc::Layout) {
let map = crate::arena::arena_alloc_gc_old(
std::mem::size_of::<crate::map::MapHeader>(),
8,
GC_TYPE_MAP,
) as *mut crate::map::MapHeader;
let layout = std::alloc::Layout::from_size_align((capacity as usize * 16).max(8), 8)
.expect("valid map entries layout");
let entries = std::alloc::alloc_zeroed(layout) as *mut u64;
assert!(!entries.is_null());
init_test_map_header(map, entries, capacity);
(map, entries, layout)
}

/// Initialise EVERY word of a fixture `MapHeader`, as `js_map_alloc` does —
/// never only the fields the test happens to read (#11362). Zeroing the whole
/// payload first is what makes `store` and `meta` null regardless of what the
/// recycled arena bytes held.
pub(super) unsafe fn init_test_map_header(
map: *mut crate::map::MapHeader,
entries: *mut u64,
capacity: u32,
) {
std::ptr::write_bytes(
map as *mut u8,
0,
std::mem::size_of::<crate::map::MapHeader>(),
);
(*map).capacity = capacity;
(*map).entries = entries as *mut f64;
}

pub(super) unsafe fn retire_old_test_map(
map: *mut crate::map::MapHeader,
entries: *mut u64,
layout: std::alloc::Layout,
) {
(*map).size = 0;
(*map).used = 0;
(*map).capacity = 0;
(*map).entries = std::ptr::null_mut();
std::alloc::dealloc(entries as *mut u8, layout);
}
4 changes: 3 additions & 1 deletion crates/perry-runtime/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ pub(crate) use store::{
release_current_thread_map_side_allocations,
};
#[cfg(test)]
pub(crate) use store::{test_from_space_map_finalizations, test_map_side_allocation};
pub(crate) use store::{
test_from_space_map_finalizations, test_map_side_allocation, test_map_store_word,
};

#[cfg(test)]
thread_local! {
Expand Down
8 changes: 8 additions & 0 deletions crates/perry-runtime/src/map/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ pub(crate) fn test_map_side_allocation(addr: usize) -> Option<(usize, usize)> {
}
}

/// The raw owned-store word of a Map header, read without trusting it. A
/// fixture that builds a `MapHeader` by hand must leave this null, or the
/// thread-exit arena drop frees whatever it holds (#11362).
#[cfg(test)]
pub(crate) unsafe fn test_map_store_word(map: *const MapHeader) -> usize {
(*map).store as usize
}

/// TLS destructors cannot access GC accounting TLS, which may already be gone.
pub(crate) unsafe fn drop_map_store_at_thread_exit(map: *mut MapHeader) {
let store = std::mem::replace(&mut (*map).store, ptr::null_mut());
Expand Down
Loading