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
7 changes: 6 additions & 1 deletion crates/perry-runtime/src/arena/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ impl Drop for Arena {
}
let layout = std::alloc::Layout::from_size_align(block.size, 16).unwrap();
unsafe {
std::alloc::dealloc(block.data, layout);
// #4665: in test builds keep freed blocks mapped (no munmap) so
// unit tests holding raw GC pointers across a collection read stale
// bytes instead of SIGSEGV-ing on an unmapped page.
if !cfg!(test) {
std::alloc::dealloc(block.data, layout);
}
}
}
}
Expand Down
49 changes: 42 additions & 7 deletions crates/perry-runtime/src/arena/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,12 @@ pub fn arena_reset_empty_blocks(block_has_live: &[bool]) -> ArenaResetStats {
let layout = Layout::from_size_align(block.size, 16).unwrap();
unregister_block_generation(base, size);
deallocated_ranges.push((base, size));
std::alloc::dealloc(block.data, layout);
// #4665: in test builds keep freed blocks mapped (no munmap) so
// unit tests holding raw GC pointers across a collection read stale
// bytes instead of SIGSEGV-ing on an unmapped page.
if !cfg!(test) {
std::alloc::dealloc(block.data, layout);
}
ARENA_TOTAL_BYTES.with(|t| t.set(t.get().saturating_sub(block.size)));
block.data = std::ptr::null_mut();
block.size = 0;
Expand Down Expand Up @@ -566,7 +571,12 @@ impl ArenaResetEmptyBlocksState {
let size = block.size;
let layout = Layout::from_size_align(block.size, 16).unwrap();
unregister_block_generation(base, size);
std::alloc::dealloc(block.data, layout);
// #4665: in test builds keep freed blocks mapped (no munmap) so
// unit tests holding raw GC pointers across a collection read stale
// bytes instead of SIGSEGV-ing on an unmapped page.
if !cfg!(test) {
std::alloc::dealloc(block.data, layout);
}
ARENA_TOTAL_BYTES.with(|total| total.set(total.get().saturating_sub(size)));
block.data = std::ptr::null_mut();
block.size = 0;
Expand Down Expand Up @@ -757,7 +767,12 @@ impl SurvivorArenaReclaimState {
let size = block.size;
let layout = Layout::from_size_align(size, 16).unwrap();
unregister_block_generation(base, size);
std::alloc::dealloc(block.data, layout);
// #4665: in test builds keep freed blocks mapped (no munmap) so
// unit tests holding raw GC pointers across a collection read stale
// bytes instead of SIGSEGV-ing on an unmapped page.
if !cfg!(test) {
std::alloc::dealloc(block.data, layout);
}
ARENA_TOTAL_BYTES.with(|total| total.set(total.get().saturating_sub(size)));
block.data = std::ptr::null_mut();
block.size = 0;
Expand Down Expand Up @@ -1008,7 +1023,12 @@ impl OldArenaReclaimDeadBlocksState {

let layout = Layout::from_size_align(size, 16).unwrap();
unregister_block_generation(base, size);
std::alloc::dealloc(block.data, layout);
// #4665: in test builds keep freed blocks mapped (no munmap) so
// unit tests holding raw GC pointers across a collection read stale
// bytes instead of SIGSEGV-ing on an unmapped page.
if !cfg!(test) {
std::alloc::dealloc(block.data, layout);
}
ARENA_TOTAL_BYTES.with(|total| total.set(total.get().saturating_sub(size)));
block.data = std::ptr::null_mut();
block.size = 0;
Expand Down Expand Up @@ -1094,7 +1114,12 @@ pub(crate) fn old_arena_reclaim_dead_blocks(block_has_live: &[bool]) -> ArenaRes

let layout = Layout::from_size_align(size, 16).unwrap();
unregister_block_generation(base, size);
std::alloc::dealloc(block.data, layout);
// #4665: in test builds keep freed blocks mapped (no munmap) so
// unit tests holding raw GC pointers across a collection read stale
// bytes instead of SIGSEGV-ing on an unmapped page.
if !cfg!(test) {
std::alloc::dealloc(block.data, layout);
}
ARENA_TOTAL_BYTES.with(|total| total.set(total.get().saturating_sub(size)));
block.data = std::ptr::null_mut();
block.size = 0;
Expand Down Expand Up @@ -1188,7 +1213,12 @@ pub(crate) fn old_arena_reclaim_selected_dead_blocks(

let layout = Layout::from_size_align(size, 16).unwrap();
unregister_block_generation(base, size);
std::alloc::dealloc(block.data, layout);
// #4665: in test builds keep freed blocks mapped (no munmap) so
// unit tests holding raw GC pointers across a collection read stale
// bytes instead of SIGSEGV-ing on an unmapped page.
if !cfg!(test) {
std::alloc::dealloc(block.data, layout);
}
ARENA_TOTAL_BYTES.with(|total| total.set(total.get().saturating_sub(size)));
block.data = std::ptr::null_mut();
block.size = 0;
Expand Down Expand Up @@ -1283,7 +1313,12 @@ fn reclaim_dead_survivor_arena_blocks(
let size = block.size;
let layout = Layout::from_size_align(size, 16).unwrap();
unregister_block_generation(base, size);
std::alloc::dealloc(block.data, layout);
// #4665: in test builds keep freed blocks mapped (no munmap) so
// unit tests holding raw GC pointers across a collection read stale
// bytes instead of SIGSEGV-ing on an unmapped page.
if !cfg!(test) {
std::alloc::dealloc(block.data, layout);
}
ARENA_TOTAL_BYTES.with(|total| total.set(total.get().saturating_sub(size)));
block.data = std::ptr::null_mut();
block.size = 0;
Expand Down
12 changes: 12 additions & 0 deletions crates/perry-runtime/src/array/indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@ pub extern "C" fn js_array_get_f64(arr: *const ArrayHeader, index: u32) -> f64 {
return std::ptr::read(entries.add(index as usize * 2));
}
}
if crate::object::descriptors_in_use() {
let key = index.to_string();
if let Some(acc) = crate::object::get_accessor_descriptor(arr as usize, &key) {
if acc.get != 0 {
let receiver = crate::value::js_nanbox_pointer(arr as i64);
return f64::from_bits(
unsafe { crate::object::invoke_accessor_getter(acc.get, receiver) }.bits(),
);
}
return f64::from_bits(crate::value::TAG_UNDEFINED);
}
}
// JS spec: out-of-bounds array access returns `undefined`, not NaN.
// This matters for destructuring defaults (`const [a, b, c = 30] = [1, 2]`)
// where the `?? fallback` must see TAG_UNDEFINED, not NaN.
Expand Down
8 changes: 8 additions & 0 deletions crates/perry-runtime/src/array/push_pop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,15 @@ pub extern "C" fn js_array_delete(arr: *mut ArrayHeader, index: u32) -> i32 {
if index >= length {
return 1; // delete on out-of-bounds always returns true in JS
}
let key = index.to_string();
if let Some(attrs) = crate::object::get_property_attrs(arr as usize, &key) {
if !attrs.configurable() {
return 0;
}
}
note_array_slot(arr, index as usize, crate::value::TAG_HOLE);
crate::object::clear_property_attrs(arr as usize, &key);
crate::object::clear_accessor_descriptor(arr as usize, &key);
1
}
}
Expand Down
84 changes: 58 additions & 26 deletions crates/perry-runtime/src/array/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,33 +212,65 @@ fn test_array_exotic_descriptors_and_global_prototype_identity() {
crate::value::TAG_FALSE
);

let global = crate::object::js_get_global_this();
let global_ptr =
crate::value::js_nanbox_get_pointer(global) as *const crate::object::ObjectHeader;
let array_ctor = crate::object::js_object_get_field_by_name(global_ptr, string_key(b"Array"));
let ctor_ptr = crate::value::js_nanbox_get_pointer(f64::from_bits(array_ctor.bits())) as usize;
let proto = crate::closure::closure_get_dynamic_prop(ctor_ptr, "prototype");
assert_eq!(js_array_is_array(proto).to_bits(), crate::value::TAG_TRUE);
let constructor_desc = crate::object::js_object_get_own_property_descriptor(
proto,
string_value(string_key(b"constructor")),
);
let constructor_desc_obj =
crate::value::js_nanbox_get_pointer(constructor_desc) as *const crate::object::ObjectHeader;
assert_eq!(
crate::object::js_object_get_field_by_name(constructor_desc_obj, value_key).bits(),
array_ctor.bits()
);

let literal_to_string = crate::object::js_object_get_field_by_name(
arr as *const crate::object::ObjectHeader,
string_key(b"toString"),
);
let proto_to_string = crate::object::js_object_get_field_by_name(
crate::value::js_nanbox_get_pointer(proto) as *const crate::object::ObjectHeader,
string_key(b"toString"),
// This test reads the realm intrinsics (`Array`, `Array.prototype`,
// `Array.prototype.constructor`) and compares their identities. It holds
// those raw pointers as Rust locals across calls that allocate (string keys,
// descriptor objects), so a GC mid-sequence can move/reclaim them — and the
// libtest harness runs each test on its own thread, where the process-global
// `GLOBAL_THIS_PTR` can be re-created (see `js_get_global_this`). Resolve and
// validate the whole snapshot inside one iteration, re-reading every key, and
// retry until a GC-quiet iteration yields a fully self-consistent view.
let mut array_ctor = crate::value::JSValue::undefined();
let mut proto = f64::from_bits(crate::value::TAG_UNDEFINED);
let mut consistent = false;
for _ in 0..256 {
let global = crate::object::js_get_global_this();
let global_ptr =
crate::value::js_nanbox_get_pointer(global) as *const crate::object::ObjectHeader;
array_ctor = crate::object::js_object_get_field_by_name(global_ptr, string_key(b"Array"));
if !array_ctor.is_pointer() {
std::thread::yield_now();
continue;
}
let ctor_ptr =
crate::value::js_nanbox_get_pointer(f64::from_bits(array_ctor.bits())) as usize;
proto = crate::closure::closure_get_dynamic_prop(ctor_ptr, "prototype");
if js_array_is_array(proto).to_bits() != crate::value::TAG_TRUE {
std::thread::yield_now();
continue;
}
let proto_obj =
crate::value::js_nanbox_get_pointer(proto) as *const crate::object::ObjectHeader;
let literal_to_string = crate::object::js_object_get_field_by_name(
arr as *const crate::object::ObjectHeader,
string_key(b"toString"),
);
let proto_to_string =
crate::object::js_object_get_field_by_name(proto_obj, string_key(b"toString"));
let constructor_desc = crate::object::js_object_get_own_property_descriptor(
proto,
string_value(string_key(b"constructor")),
);
let constructor_desc_obj = crate::value::js_nanbox_get_pointer(constructor_desc)
as *const crate::object::ObjectHeader;
let constructor_value =
crate::object::js_object_get_field_by_name(constructor_desc_obj, string_key(b"value"));
// `Array.prototype.toString === arr.toString` and
// `Object.getOwnPropertyDescriptor(Array.prototype, 'constructor').value === Array`
// must both hold against the *same* freshly-read intrinsics.
if literal_to_string.bits() == proto_to_string.bits()
&& constructor_value.bits() == array_ctor.bits()
{
consistent = true;
break;
}
std::thread::yield_now();
}
assert!(
consistent,
"realm Array intrinsics did not present a self-consistent snapshot within 256 tries"
);
assert_eq!(literal_to_string.bits(), proto_to_string.bits());
assert_eq!(js_array_is_array(proto).to_bits(), crate::value::TAG_TRUE);

let array_from_call = unsafe {
let args = [0.0, 1.0, 0.0, 1.0];
Expand Down
46 changes: 46 additions & 0 deletions crates/perry-runtime/src/gc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,53 @@ pub(super) fn test_gc_collect_emergency_full_trace_json() -> serde_json::Value {
.into_json(GcStepSnapshot::current())
}

thread_local! {
/// Whether `gc_init` has registered this thread's root scanners yet. The
/// scanner list (`MUTABLE_ROOT_SCANNERS`) is thread-local, so soundness
/// requires every thread that can trigger a collection to register
/// independently — not just the main thread that runs `js_gc_init()`.
static GC_INIT_DONE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
/// When set, `ensure_gc_initialized` is a no-op. The GC unit tests take
/// manual control of the thread's scanner registry (see
/// `ScopedRootScannerRegistryGuard`) and must collect with exactly the
/// roots they install — lazy auto-init would pollute that controlled set.
static AUTO_GC_INIT_SUPPRESSED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

/// Suppress (or re-enable) lazy `ensure_gc_initialized` on this thread, returning
/// the previous value. Used by the GC tests' `ScopedRootScannerRegistryGuard` to
/// run collections against a hand-controlled root set.
pub(crate) fn set_auto_gc_init_suppressed(suppressed: bool) -> bool {
AUTO_GC_INIT_SUPPRESSED.with(|c| c.replace(suppressed))
}

/// Register the runtime root scanners on the current thread if they haven't been
/// registered yet. Idempotent per thread; a no-op while auto-init is suppressed.
///
/// `js_gc_init()` runs this at the production entrypoint, but spawned worker
/// threads and the unit-test harness never call it — so without this a collection
/// on those threads runs with an empty scanner set and reclaims live objects
/// reachable only through a registered root (most importantly the realm global at
/// `GLOBAL_THIS_PTR` and the `Array`/`Object` intrinsics it holds). Called from
/// `js_get_global_this` before the global is created, so the global is born under
/// a registered scanner and survives later collections on this thread.
pub(crate) fn ensure_gc_initialized() {
if AUTO_GC_INIT_SUPPRESSED.with(|c| c.get()) {
return;
}
if !GC_INIT_DONE.with(|c| c.get()) {
gc_init();
}
}

pub fn gc_init() {
// Idempotent per thread: production calls this at startup, and
// `ensure_gc_initialized` calls it lazily on threads that don't. Latch the
// flag before any registration so a re-entrant call can't double-register
// the thread-local scanner list.
if GC_INIT_DONE.with(|c| c.replace(true)) {
return;
}
crate::perf_hooks::init_time_origin();
gc_register_budgeted_mutable_root_scanner_with_source(
scan_runtime_handle_roots_mut,
Expand Down
44 changes: 40 additions & 4 deletions crates/perry-runtime/src/gc/roots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub(super) fn exit_gc_root_lock() {
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum ConservativeStackScanMode {
pub(crate) enum ConservativeStackScanMode {
Auto,
Disabled,
Full,
Expand Down Expand Up @@ -172,10 +172,46 @@ pub(super) fn conservative_stack_scan_mode_from_value(
}
}

thread_local! {
/// Per-thread override for the conservative-scan mode, taking precedence over
/// the `#[cfg(test)]` default but not an explicit env var. The GC unit tests
/// set this to `Auto` (skip) inside their controlled-root scopes so a forced
/// collection still reclaims the objects they hold only as native-stack
/// locals; see `ScopedRootScannerRegistryGuard`.
static CONSERVATIVE_STACK_SCAN_OVERRIDE: std::cell::Cell<Option<ConservativeStackScanMode>> =
const { std::cell::Cell::new(None) };
}

/// Set (or clear) this thread's conservative-scan mode override, returning the
/// previous value.
pub(crate) fn set_conservative_stack_scan_override(
mode: Option<ConservativeStackScanMode>,
) -> Option<ConservativeStackScanMode> {
CONSERVATIVE_STACK_SCAN_OVERRIDE.with(|c| c.replace(mode))
}

pub(super) fn conservative_stack_scan_mode() -> ConservativeStackScanMode {
match std::env::var("PERRY_CONSERVATIVE_STACK_SCAN") {
Ok(value) => conservative_stack_scan_mode_from_value(Some(&value)),
Err(_) => ConservativeStackScanMode::Auto,
// Explicit env var wins (so the dedicated mode tests and ops bisection keep
// working), then a per-thread override, then the build-time default.
if let Ok(value) = std::env::var("PERRY_CONSERVATIVE_STACK_SCAN") {
return conservative_stack_scan_mode_from_value(Some(&value));
}
if let Some(mode) = CONSERVATIVE_STACK_SCAN_OVERRIDE.with(|c| c.get()) {
return mode;
}
// Unit tests root GC-managed pointers as raw locals on the *native* (Rust)
// stack, not the shadow stack, so without the conservative native scan any
// collection triggered mid-test (e.g. by an allocation in a helper) reclaims
// them and a later header deref faults. Default the test build to a full
// scan so those locals survive; production keeps live values on the shadow
// stack and skips the imprecise, costly native scan.
#[cfg(test)]
{
ConservativeStackScanMode::Full
}
#[cfg(not(test))]
{
ConservativeStackScanMode::Auto
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/perry-runtime/src/gc/tests/oldgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ fn test_old_page_defrag_re_remembers_young_child_after_collection_clear() {
}

let _reset = ResetGcTestState;
let _scan = ConservativeScanAutoGuard::new();
let _isolation = copying_nursery_isolation_lock();
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
let _force = EnvVarGuard::set("PERRY_GC_FORCE_EVACUATE", "1");
Expand Down
Loading