Skip to content

Commit c109480

Browse files
committed
fix(gc-tests): suppress automatic GC triggers around raw-pointer forwarding setup
prototype_addr_cache.rs and side_table_scanners.rs hold raw GC pointers (nursery/old-gen addresses in native locals, no root registered) across further arena_alloc_gc / arena_alloc_gc_old calls while hand-staging forwarding chains. If one of those allocations lands on the block-full slow path, arena_cell_alloc's gc_check_trigger() can run a real collection and move/free the held object out from under the test. Apply the sibling pattern already used in callback_scanners.rs, hook_dispatch_handles.rs, and transient_handles.rs: GcTriggerThresholdTestGuard::suppress_automatic_triggers() at every exposed call site. Sites that are not exposed carry a comment explaining why, rather than a blanket guard.
1 parent 97c6921 commit c109480

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Several `gc::tests::runtime_roots` helpers and tests in `prototype_addr_cache.rs`
2+
and `side_table_scanners.rs` held a raw GC pointer across `arena_alloc_gc` /
3+
`arena_alloc_gc_old` without suppressing automatic GC triggers. Those
4+
allocations can land on the block-full slow path, which calls
5+
`gc_check_trigger()` — an uncontrolled collection there could relocate or free
6+
the held pointer's object before the test finished with it, since the
7+
pointer was live only as a native local with no root registered.
8+
9+
Both files now use the same `GcTriggerThresholdTestGuard::suppress_automatic_triggers()`
10+
pattern already used by `callback_scanners.rs`, `hook_dispatch_handles.rs`, and
11+
`transient_handles.rs`, applied at every call site where a raw pointer/address
12+
is live across a further allocation. Sites that are not exposed carry a brief
13+
comment explaining why.

‎crates/perry-runtime/src/gc/tests/runtime_roots/prototype_addr_cache.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,27 @@ impl Drop for ProtoAddrCacheGuard {
5757
}
5858

5959
/// Allocate a nursery object to stand in for the intrinsic.
60+
///
61+
/// Not exposed on its own: nothing is live in this function before the
62+
/// allocation, so there is no raw pointer for an in-flight trigger to move
63+
/// out from under. Callers that keep the returned pointer live across a
64+
/// FURTHER allocation (`forwarded_pair`, the multi-hop test, the collector
65+
/// rewrite test) carry their own guard.
6066
fn nursery_stand_in() -> *mut u8 {
6167
crate::arena::arena_alloc_gc(64, 8, GC_TYPE_ARRAY)
6268
}
6369

6470
/// Evacuate `from`: allocate an old-gen destination and forward `from` → `to`.
71+
///
72+
/// `from` is a raw pointer to a movable nursery object that stays live across
73+
/// the allocation below. If that allocation lands on the block-full slow
74+
/// path, `arena_cell_alloc` calls `gc_check_trigger()`, and — absent
75+
/// suppression — an automatic collection could relocate/free `from` before
76+
/// the forwarding address is installed, corrupting the synthetic setup this
77+
/// test file relies on (#6981's tests never intend to exercise a *real*
78+
/// concurrent collection here, only the hand-driven forwarding chain).
6579
fn evacuate(from: *mut u8) -> usize {
80+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
6681
let to = crate::arena::arena_alloc_gc_old(64, 8, GC_TYPE_ARRAY);
6782
unsafe {
6883
set_forwarding_address(header_from_user_ptr(from) as *mut GcHeader, to);
@@ -71,7 +86,13 @@ fn evacuate(from: *mut u8) -> usize {
7186
}
7287

7388
/// Allocate `from` and `to`, forward `from` → `to`, and return the pair.
89+
///
90+
/// `from` is also live in THIS frame across the call to `evacuate` (which
91+
/// performs the allocation). Guarded here too, in addition to `evacuate`'s
92+
/// own guard, so the suppression holds for as long as `from` is live in any
93+
/// frame on this call chain, independent of `evacuate`'s internals.
7494
fn forwarded_pair() -> (usize, usize) {
95+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
7596
let from = nursery_stand_in();
7697
let to = evacuate(from);
7798
(from as usize, to)
@@ -104,6 +125,9 @@ fn read_addr(which: &str) -> usize {
104125
#[test]
105126
fn prototype_addr_reads_through_a_forwarding_stub() {
106127
let _guard = ProtoAddrCacheGuard::new();
128+
// Not exposed at this level: `from`/`to` come back as plain `usize`s from
129+
// `forwarded_pair`, which carries its own trigger guard, and nothing else
130+
// in this loop body allocates.
107131

108132
for which in ["array", "object"] {
109133
let (from, to) = forwarded_pair();
@@ -132,6 +156,10 @@ fn prototype_addr_reads_through_a_forwarding_stub() {
132156
#[test]
133157
fn prototype_addr_reads_through_a_multi_hop_forwarding_chain() {
134158
let _guard = ProtoAddrCacheGuard::new();
159+
// `first` is live across `second`'s allocation, and both `first` and
160+
// `second` are live across `final_user`'s allocation — any of the three
161+
// could reach the block-full slow path's `gc_check_trigger()`.
162+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
135163

136164
let first = crate::arena::arena_alloc_gc(64, 8, GC_TYPE_ARRAY);
137165
let second = crate::arena::arena_alloc_gc(64, 8, GC_TYPE_ARRAY);
@@ -156,6 +184,10 @@ fn prototype_addr_reads_through_a_multi_hop_forwarding_chain() {
156184
#[test]
157185
fn prototype_addr_cache_is_rewritten_by_the_collector() {
158186
let _guard = ProtoAddrCacheGuard::new();
187+
// `array_from` is live across the second `nursery_stand_in` call below
188+
// (its own allocation, unguarded on its own), and both from-pointers stay
189+
// live across the `evacuate` calls that follow.
190+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
159191

160192
// The from-space objects must exist before the valid-pointer set is built —
161193
// that set is what tells the rewrite visitor an address is a real heap

‎crates/perry-runtime/src/gc/tests/runtime_roots/side_table_scanners.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ fn test_implicit_this_root_scanner_marks_and_rewrites() {
1010
// when the cell is its only root, and (b) REWRITE the cell to the moved
1111
// copy so the body's next `this`-derived dispatch derefs live memory
1212
// instead of the stale slot (the reported SIGSEGV in js_native_call_method).
13+
// `nursery_user` is live across the `arena_alloc_gc_old` call below (used
14+
// afterwards to build `nursery_hdr`); the block-full slow path in that
15+
// allocation can reach `gc_check_trigger()`, so suppress automatic
16+
// triggers for the setup below.
17+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
1318
clear_marks();
1419
clear_mark_seeds();
1520
let prev_this = crate::object::js_implicit_this_get();
@@ -59,6 +64,12 @@ fn test_implicit_this_root_scanner_marks_and_rewrites() {
5964
#[test]
6065
fn test_class_side_table_scanner_marks_values_but_not_function_keys() {
6166
let _guard = GcTestIsolationGuard::new();
67+
// `dynamic_value`/`prototype_value`/`cached_value`/`prototype_object` are
68+
// all live across the two `arena_alloc_gc` calls below (`parent_closure`,
69+
// `function_key`), and `parent_closure` is live across `function_key`'s —
70+
// any of those allocations can reach the block-full slow path's
71+
// `gc_check_trigger()`.
72+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
6273
clear_marks();
6374
clear_mark_seeds();
6475
crate::object::test_clear_class_side_table_roots();
@@ -115,6 +126,10 @@ fn test_class_side_table_scanner_marks_values_but_not_function_keys() {
115126
#[test]
116127
fn test_registered_class_side_table_scanner_rewrites_values_and_function_keys() {
117128
let _guard = GcTestIsolationGuard::new();
129+
// `value_user` is live across `key_user`'s `arena_alloc_gc` call, and both
130+
// `value_user`/`key_user` (plus `value_old` once allocated) are live
131+
// across the remaining `arena_alloc_gc`/`arena_alloc_gc_old` calls below.
132+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
118133
crate::object::test_clear_class_side_table_roots();
119134
gc_register_mutable_root_scanner(crate::object::scan_class_side_table_roots_mut);
120135

@@ -191,6 +206,10 @@ fn test_registered_class_side_table_scanner_rewrites_values_and_function_keys()
191206

192207
#[test]
193208
fn test_symbol_side_table_scanner_marks_keys_and_values_without_marking_owner() {
209+
// Not exposed: every allocation here goes through opaque helpers
210+
// (`js_object_alloc`, `alloc_nursery_test_symbol`, `young_leaf`) with no
211+
// direct `arena_alloc_gc`/`arena_alloc_gc_old` call written in this file,
212+
// so there is no in-file call site to guard.
194213
let _guard = GcTestIsolationGuard::new();
195214
clear_marks();
196215
clear_mark_seeds();
@@ -226,6 +245,10 @@ fn test_symbol_side_table_scanner_marks_keys_and_values_without_marking_owner()
226245
#[test]
227246
fn test_symbol_side_table_registered_scanner_rewrites_roots_and_metadata() {
228247
let _guard = GcTestIsolationGuard::new();
248+
// `owner`/`sym_key`/`value`/`static_sym_key`/`static_value` (and the
249+
// `_old` addresses as they're allocated) are all live across the three
250+
// `arena_alloc_gc_old` calls below.
251+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
229252
crate::symbol::test_clear_symbol_side_table_roots();
230253
gc_register_mutable_root_scanner(crate::symbol::scan_symbol_side_table_roots_mut);
231254

@@ -307,6 +330,8 @@ fn test_symbol_side_table_registered_scanner_rewrites_roots_and_metadata() {
307330

308331
#[test]
309332
fn test_runtime_root_visitor_rewrites_raw_pointer_slots() {
333+
// `nursery_user` is live across the `arena_alloc_gc_old` call below.
334+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
310335
let nursery_user = crate::arena::arena_alloc_gc(64, 8, GC_TYPE_OBJECT);
311336
let valid_ptrs = build_valid_pointer_set();
312337
let old_user = crate::arena::arena_alloc_gc_old(64, 8, GC_TYPE_OBJECT);
@@ -353,6 +378,10 @@ fn test_class_inheritance_side_table_roots_mark_and_rewrite() {
353378
const PROTO_CID: u32 = 0xDEAD_0001;
354379
const CLOSURE_CID: u32 = 0xDEAD_0002;
355380

381+
// `proto_user`/`decl_proto_user`/`closure_user` (and the `_old` addresses
382+
// as they're allocated) are all live across the later `arena_alloc_gc`/
383+
// `arena_alloc_gc_old` calls below.
384+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
356385
clear_marks();
357386
clear_mark_seeds();
358387

@@ -432,6 +461,8 @@ fn test_class_inheritance_side_table_roots_mark_and_rewrite() {
432461

433462
#[test]
434463
fn test_runtime_root_visitor_rewrites_cell_and_atomic_slots() {
464+
// `nursery_user` is live across the `arena_alloc_gc_old` call below.
465+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
435466
let nursery_user = crate::arena::arena_alloc_gc(64, 8, GC_TYPE_OBJECT);
436467
let valid_ptrs = build_valid_pointer_set();
437468
let old_user = crate::arena::arena_alloc_gc_old(64, 8, GC_TYPE_OBJECT);
@@ -485,6 +516,8 @@ fn test_runtime_root_visitor_rewrites_cell_and_atomic_slots() {
485516

486517
#[test]
487518
fn test_runtime_root_visitor_rewrites_metadata_without_marking() {
519+
// `nursery_user` is live across the `arena_alloc_gc_old` call below.
520+
let _trigger_guard = GcTriggerThresholdTestGuard::suppress_automatic_triggers();
488521
let nursery_user = crate::arena::arena_alloc_gc(64, 8, GC_TYPE_OBJECT);
489522
let valid_ptrs = build_valid_pointer_set();
490523
let old_user = crate::arena::arena_alloc_gc_old(64, 8, GC_TYPE_OBJECT);

0 commit comments

Comments
 (0)