Skip to content

Commit a2ea46d

Browse files
author
Ralph Küpper
committed
fix(object): GC-safe key snapshots + per-key enumerability; preserve data-symbol order
Address CodeRabbit review on #5177: - field_get_set.rs (js_object_values / js_object_entries): snapshot own keys as owned bytes instead of NaN-boxed pointers. A getter fired by js_object_get_field_by_name can delete a future key and GC before we visit it; a key kept only inside the Rust-heap Vec is not a stack-visible GC root, so it could dangle. Rematerialize the string at read time instead. - Move the enumerability filter entirely to the per-key read phase (drop the cached has_descriptors / snapshot-phase skip): per EnumerableOwnProperties an earlier getter can create a descriptor or flip a future key's enumerability, which the up-front filter and stale has_descriptors cache both missed. - symbol.rs (js_object_get_own_property_symbols): don't sort the whole merged set by creation id — that reordered data symbols already in insertion order (e.g. obj[b]=…; obj[a]=… with a created before b). Partition into data-valued (kept in insertion order) and the appended accessor-only tail (sorted by creation id for determinism). Validated vs node v26: Object.values/entries getter-add/remove/enumerability cases and data/accessor symbol ordering all match byte-for-byte. perry-runtime object:: tests 29/29.
1 parent 739d57d commit a2ea46d

2 files changed

Lines changed: 60 additions & 48 deletions

File tree

‎crates/perry-runtime/src/object/field_get_set.rs‎

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,16 +1987,22 @@ pub extern "C" fn js_object_values(obj: *const ObjectHeader) -> *mut ArrayHeader
19871987
None => j as u32,
19881988
}
19891989
};
1990-
// #5046: skip keys a descriptor marks non-enumerable, like
1991-
// `js_object_keys` does. Cheap any-descriptor probe first so
1992-
// descriptor-free objects stay on the fast path.
1993-
let has_descriptors =
1994-
PROPERTY_DESCRIPTORS.with(|m| m.borrow().keys().any(|(ptr, _)| *ptr == obj as usize));
1995-
1996-
// Snapshot the enumerable key list before reading values, then read each
1990+
// Snapshot the own key list before reading values, then read each
19971991
// through the name-keyed `[[Get]]` so own accessors fire and getter side
19981992
// effects don't perturb the key set (mirrors `js_object_entries`).
1999-
let mut snapshot_keys: Vec<f64> = Vec::with_capacity(count);
1993+
//
1994+
// Two correctness requirements drive this shape:
1995+
// * GC safety — a getter fired by `js_object_get_field_by_name` can
1996+
// delete a future key and allocate/GC before we visit it. A key kept
1997+
// only as a NaN-boxed pointer inside this Rust-heap `Vec` is not a
1998+
// stack-visible GC root, so it could dangle. We snapshot the owned
1999+
// key *bytes* and rematerialize the string at read time instead.
2000+
// * EnumerableOwnProperties — enumerability is determined per key at
2001+
// read time, not cached up front: an earlier getter can create a
2002+
// descriptor or flip a future key's enumerability, so we defer the
2003+
// `descriptor_marks_non_enumerable` check to the read phase.
2004+
let mut snapshot_keys: Vec<Vec<u8>> = Vec::with_capacity(count);
2005+
let mut key_buf = [0u8; crate::value::SHORT_STRING_MAX_LEN];
20002006
for j in 0..count {
20012007
let i = pos(j);
20022008
if keys.is_null() || i >= crate::array::js_array_length(keys) {
@@ -2006,25 +2012,23 @@ pub extern "C" fn js_object_values(obj: *const ObjectHeader) -> *mut ArrayHeader
20062012
if instance_private_key_hidden(obj, key_val) {
20072013
continue;
20082014
}
2009-
if has_descriptors && descriptor_marks_non_enumerable(obj, key_val) {
2010-
continue;
2015+
if let Some(bytes) = crate::string::js_string_key_bytes(key_val, &mut key_buf) {
2016+
snapshot_keys.push(bytes.to_vec());
20112017
}
2012-
snapshot_keys.push(f64::from_bits(key_val.bits()));
20132018
}
2014-
for key_f64 in snapshot_keys {
2019+
for key_bytes in snapshot_keys {
20152020
let key_str =
2016-
crate::value::js_get_string_pointer_unified(key_f64) as *const crate::StringHeader;
2021+
crate::string::js_string_from_bytes(key_bytes.as_ptr(), key_bytes.len() as u32);
20172022
if key_str.is_null() {
20182023
continue;
20192024
}
20202025
// Re-check own + enumerable at read time (a prior getter may have
2021-
// removed/hidden the key) — see `js_object_entries`.
2026+
// removed/hidden the key, or created a descriptor) — see
2027+
// `js_object_entries`.
20222028
if !super::own_key_present(obj as *mut ObjectHeader, key_str) {
20232029
continue;
20242030
}
2025-
if has_descriptors
2026-
&& descriptor_marks_non_enumerable(obj, JSValue::from_bits(key_f64.to_bits()))
2027-
{
2031+
if descriptor_marks_non_enumerable(obj, JSValue::string_ptr(key_str)) {
20282032
continue;
20292033
}
20302034
let value = js_object_get_field_by_name(obj as *const ObjectHeader, key_str);
@@ -2157,20 +2161,22 @@ pub extern "C" fn js_object_entries(obj: *const ObjectHeader) -> *mut ArrayHeade
21572161
None => j as u32,
21582162
}
21592163
};
2160-
// #5046: skip keys a descriptor marks non-enumerable, like
2161-
// `js_object_keys` does. Cheap any-descriptor probe first so
2162-
// descriptor-free objects stay on the fast path.
2163-
let has_descriptors =
2164-
PROPERTY_DESCRIPTORS.with(|m| m.borrow().keys().any(|(ptr, _)| *ptr == obj as usize));
2165-
2166-
// Spec (EnumerableOwnProperties): the own enumerable key list is
2167-
// determined ONCE up front, then `[[Get]]` is invoked per key. A getter
2168-
// that adds, removes, or hides a future key during enumeration must not
2169-
// change the set of entries reported (test262 entries/getter-adding-key,
2164+
// Spec (EnumerableOwnProperties): the own key list is determined ONCE up
2165+
// front, then `[[Get]]` is invoked per key. A getter that adds, removes,
2166+
// or hides a future key during enumeration must not change the set of
2167+
// entries reported (test262 entries/getter-adding-key,
21702168
// getter-removing-future-key, getter-making-future-key-nonenumerable).
2171-
// Snapshot the enumerable string keys (NaN-boxed) before reading any
2172-
// value so getter side effects on `keys_array` can't perturb the loop.
2173-
let mut snapshot_keys: Vec<f64> = Vec::with_capacity(count);
2169+
//
2170+
// Snapshot the own key *bytes* (not NaN-boxed pointers): a getter fired
2171+
// by `js_object_get_field_by_name` can delete a future key and
2172+
// allocate/GC before we visit it, and a key kept only inside this
2173+
// Rust-heap `Vec` is not a stack-visible GC root — it could dangle.
2174+
// Owning the bytes and rematerializing the string at read time sidesteps
2175+
// that. Enumerability is likewise re-evaluated per key in the read phase
2176+
// (an earlier getter can create a descriptor or flip a future key's
2177+
// enumerability), so we deliberately do NOT filter it during the snapshot.
2178+
let mut snapshot_keys: Vec<Vec<u8>> = Vec::with_capacity(count);
2179+
let mut key_buf = [0u8; crate::value::SHORT_STRING_MAX_LEN];
21742180
for j in 0..count {
21752181
let i = pos(j);
21762182
if keys.is_null() || i >= crate::array::js_array_length(keys) {
@@ -2180,15 +2186,14 @@ pub extern "C" fn js_object_entries(obj: *const ObjectHeader) -> *mut ArrayHeade
21802186
if instance_private_key_hidden(obj, key_val) {
21812187
continue;
21822188
}
2183-
if has_descriptors && descriptor_marks_non_enumerable(obj, key_val) {
2184-
continue;
2189+
if let Some(bytes) = crate::string::js_string_key_bytes(key_val, &mut key_buf) {
2190+
snapshot_keys.push(bytes.to_vec());
21852191
}
2186-
snapshot_keys.push(f64::from_bits(key_val.bits()));
21872192
}
21882193

2189-
for key_f64 in snapshot_keys {
2194+
for key_bytes in snapshot_keys {
21902195
let key_str =
2191-
crate::value::js_get_string_pointer_unified(key_f64) as *const crate::StringHeader;
2196+
crate::string::js_string_from_bytes(key_bytes.as_ptr(), key_bytes.len() as u32);
21922197
if key_str.is_null() {
21932198
continue;
21942199
}
@@ -2201,14 +2206,15 @@ pub extern "C" fn js_object_entries(obj: *const ObjectHeader) -> *mut ArrayHeade
22012206
if !super::own_key_present(obj as *mut ObjectHeader, key_str) {
22022207
continue;
22032208
}
2204-
if has_descriptors
2205-
&& descriptor_marks_non_enumerable(obj, JSValue::from_bits(key_f64.to_bits()))
2206-
{
2209+
if descriptor_marks_non_enumerable(obj, JSValue::string_ptr(key_str)) {
22072210
continue;
22082211
}
22092212
// Create a pair array [key, value].
22102213
let pair = crate::array::js_array_alloc(2);
2211-
crate::array::js_array_push_f64(pair, key_f64);
2214+
crate::array::js_array_push_f64(
2215+
pair,
2216+
f64::from_bits(JSValue::string_ptr(key_str).bits()),
2217+
);
22122218

22132219
// Read the value through the name-keyed `[[Get]]`, which fires an
22142220
// own accessor's getter (the raw index-based field read returned the

‎crates/perry-runtime/src/symbol.rs‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,10 @@ pub unsafe extern "C" fn js_object_get_own_property_symbols(obj_f64: f64) -> i64
23132313
.cloned()
23142314
.unwrap_or_default();
23152315
drop(guard);
2316+
// `entries[..data_len]` are the data-valued symbol properties from
2317+
// `SYMBOL_PROPERTIES`, already in their true insertion order. Everything
2318+
// appended after `data_len` is an accessor-only symbol.
2319+
let data_len = entries.len();
23162320
for sym_key in accessors::owner_symbol_accessor_keys(obj_key) {
23172321
if !entries.iter().any(|(existing, _)| *existing == sym_key) {
23182322
entries.push((sym_key, 0));
@@ -2322,15 +2326,17 @@ pub unsafe extern "C" fn js_object_get_own_property_symbols(obj_f64: f64) -> i64
23222326
return crate::array::js_array_alloc(0) as i64;
23232327
}
23242328
// `[[OwnPropertyKeys]]` reports symbol keys in property-creation order.
2325-
// Data-valued symbols already arrive in insertion order, but accessor-only
2326-
// symbols are appended from a HashMap (`owner_symbol_accessor_keys`) in
2327-
// nondeterministic order, so a `defineProperty(o, sym, {get})` pair came
2328-
// out reversed (test262 assign/strings-and-symbol-order,
2329-
// getOwnPropertyDescriptors/order-after-define-property). Sort the merged
2330-
// set by the symbol's monotonic creation id, which equals source order for
2331-
// symbols created and assigned in sequence — the convention the class-ref
2332-
// symbol path already uses.
2333-
entries.sort_by_key(|(sym_ptr_usize, _)| {
2329+
// Data-valued symbols already arrive in insertion order, so we must NOT
2330+
// reorder them (an unconditional sort by creation id would reorder e.g.
2331+
// `obj[b]=…; obj[a]=…` when `a` was created before `b`). Accessor-only
2332+
// symbols, however, are appended from a HashMap (`owner_symbol_accessor_keys`)
2333+
// in nondeterministic order, so a `defineProperty(o, sym, {get})` pair came
2334+
// out unstable (test262 assign/strings-and-symbol-order,
2335+
// getOwnPropertyDescriptors/order-after-define-property). Sort ONLY that
2336+
// appended accessor-only tail by the symbol's monotonic creation id (the
2337+
// convention the class-ref symbol path already uses), leaving the data-symbol
2338+
// insertion order intact.
2339+
entries[data_len..].sort_by_key(|(sym_ptr_usize, _)| {
23342340
let ptr = *sym_ptr_usize as *const SymbolHeader;
23352341
if ptr.is_null() {
23362342
u64::MAX

0 commit comments

Comments
 (0)