Repository navigation
Conversation
|
Preview build of 6297c8b: |
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline findings, I also checked two things in Source/JavaScriptCore/runtime/Structure.cpp and ruled them out: the hand-off in takePropertyTableFrom (the dictionary drops its table and the transition pins it with no allocation in between, the same window the pre-existing unpinned move already had), and whether isDepartedDictionary() can be true for a live structure (every path that creates a dictionary-kind structure pins its table before the structure is published, and the GC only clears unpinned tables, so an unpinned dictionary can only be one whose table was taken).
Extended reasoning...
The change rewires how every Structure transition in JSC obtains its PropertyTable, steals the table from dictionary structures, and teaches the DFG status computations, PropertyCondition and the type profiler to skip the resulting departed dictionaries; it touches no security-sensitive surface such as auth or injection. It is core engine internals with concurrency and GC interactions, so the inline findings plus a human read are warranted; this note only records what else was examined.
cf96a63 to
6297c8b
Compare
…rty table instead of copying it A delete, an attribute change, a prototype change, seal, freeze or a private brand on an object with more than 128 named properties copied the object's property table. The loop of #44583 (delete and re-add one key of a 1000 key object) ran about 100 times slower than node. WEBKIT_VERSION points at the preview build of oven-sh/WebKit#803.
| { | ||
| // This must always return a property table. It can't return null. | ||
| ASSERT(isDictionary()); | ||
| ASSERT(isPinnedPropertyTable()); | ||
| PropertyTable* result = propertyTableOrNull(); | ||
| if (result) { | ||
| if (isPinnedPropertyTable()) | ||
| return result->copy(vm, result->size() + 1); | ||
| ASSERT(result); | ||
| ConcurrentJSLocker locker(m_lock); | ||
| setIsPinnedPropertyTable(false); | ||
| setPropertyTable(vm, nullptr); | ||
| return result; | ||
| } | ||
|
|
There was a problem hiding this comment.
🔴 JSON.stringify of a large object can now read a freed property-name string when a toJSON, getter or replacer deletes and adds properties on that object. Stringifier::Holder keeps raw PropertyName keys from the dictionary's table (JSONObject.cpp:550), and takePropertyTableFromDictionary (Structure.cpp:937-947) now moves that table into the delete transition, whose remove() derefs the key; the following add pins and clears m_transitionPropertyName, freeing it. Fix: a departed dictionary must not let its former keys die while raw-key holders exist, e.g. keep copying when the structure may be observed, or have holders ref the keys they copy out of a table.
Why this was flagged
JSON.stringify(o) where o has over 128 computed-key properties (a cacheable dictionary structure D), the temporary key JSStrings have been collected, and one property value has a toJSON (or o has a getter, or a replacer is used) that runs delete o[k]; o.z = 1 for a not-yet-visited key k.…
Verification: normal — security-relevant (use-after-free of a UniquedStringImpl reachable from plain JS). Trigger: JSON.stringify of an object whose structure D is a cacheable dictionary (>128 properties), where a toJSON/getter/replacer runs delete o[k]; o.z = 1 with a computed key k for a not-yet-visited property, and a GC runs before k is visited (collecting the temp JSString that holds the atom).
…le instead of copying it A dictionary structure belongs to one object. When that object deletes a property, changes an attribute, is sealed, frozen, made non-extensible, gets a new prototype or a private brand, it moves to a new structure. Structure::takePropertyTableOrCloneIfPinned and copyPropertyTableForPinning copied the dictionary's pinned table for that transition, so each of these operations cost O(n) in the number of properties and allocated a table of n entries. On an object with 1000 properties, delete plus re-add ran about 25 us and allocated 25 KB per pair, 60 to 130 times slower than V8. Nothing needs a dictionary's table after its object leaves: no structure rebuilds a table through a dictionary, as a dictionary has no previousID chain of its own. So Structure::takePropertyTableFrom, which every transition now gets its table through, takes the table and pins it in the transition, as the transition cannot rebuild it either. A delete or an attribute change keeps its previousID and name, so that it is still listed and counted; the other transitions cut the chain with pin() as they did before. The dictionary is departed after that (Structure::isDepartedDictionary): no object has it and it has no table. An inline cache, a profile or the type profiler log can still hold it. The status computations of the DFG skip such a structure instead of taking the slow path for the whole access, which keeps the compiled code for a site that saw the object before a delete as it was. No property condition holds on it, and a condition that found the property before the dictionary departed is not watchable on it. The type profiler reads the shape from the object's structure instead. Delete plus re-add on a 1000 property object: 2278 ms to 78 ms per 100000 pairs, 25090 to 160 bytes allocated per pair (the Structure cell).
6297c8b to
a1cb492
Compare
Related to oven-sh/bun#44583
Problem
delete object.key0; object.key0 = ion 1000 properties runs 2278 ms per 100000 pairs, node 50 to 115 ms.Structure::takePropertyTableOrCloneIfPinned(Structure.cpp:929) copied the table. Attribute changes, seal, freeze, preventExtensions, prototype changes, private brands and the uncacheable transition copied it too.Fix
Structure::takePropertyTableFrom: every transition gets its table through it. A dictionary's table moves into the transition and is pinned there. Other pinned tables are still copied.previousIDchain.isDepartedDictionary). The DFG status computations skip it, noPropertyConditionholds or is watchable on it, and the type profiler reads the object's structure instead.allocated 24755 bytes per iteration), about 25000 filteredrun-javascriptcore-testsruns locally with the same failures as main, and the CI test lanes (every mode of the runner).Background
Downsides
Structurecell: 160 bytes, 0.8 us per delete plus re-add. Objects with 100 properties are unchanged: 265 ms on main, 264 ms here per 1000000 pairs (noise about 20%).delete globalThis.x, and a read site the DFG compiles after a delete: same as main (Notes).size). The common transition path gets oneisDictionary()test.Notes
Measurements. Release
jscshell on Linux x64, this branch against its base 5d2ceba, interleaved, 3 to 5 runs each, medians. Times from$vm.monotonicTimeNow(), bytes from$vm.heapTotalBytesAllocated(), which counts a 16 KB block when it is taken, so it is an average over many iterations.delete globalThis.xprocess.env. In Bun,process.envwith more than 128 variables is such an object: one table perdelete process.env.Xwith 236 variables, covered by the Bun test.Why the per-delete cost was O(n).
Structure::Structure(VM&, StructureVariant, Structure* previous)copies the dictionary kind, so the structure thatremoveNewPropertyTransitioncreates from a cacheable dictionary is a cacheable dictionary too, unpinned, withpreviousIDset to the old one. The add that follows pins it in place. The next delete finds a pinned table and copies it again. So a delete plus re-add loop copies the table once per pair, and the GC frees one table per pair (the reporter'sPropertyTable::destroysamples). Attribute changes with no add in between take the unpinned table along a growing chain and do not copy, which is why the sibling case is an attribute change after an add.Rebase. The branch was first based on 5d2ceba and measured there. It is now rebased onto main after the upstream merge (#814); the patch applied without conflicts. The first CI run failed the new stress test in the eager-JIT, no-LLInt and collect-continuously modes: the compilers' own allocations inside the measured loops, and the delete inline cache flattening the dictionary before the chain check. The test now takes the least of several rounds, deletes with
Reflect.deletePropertyin the chain check, and sizes its loops withtestLoopCount. A departed structure in aPutByIdReplacecache now yields the slow status instead ofNoInformation, which the DFG reads as a site that never ran (review comment).Self-review. A review of the diff raised 8 concerns, all addressed: the transitions that pinned their table with
pin()on main (seal, freeze, preventExtensions, prototype, brand, dictionary kind) cut the chain to the dictionary again, so a delete chain is released at those transitions as before ($vm.getStructureTransitionListlength 1, checked in the stress test);PropertyCondition::isWatchableWhenValidno longer asserts on a property that a departed dictionary lost between its two unlocked reads on a compiler thread; the stress test covers every transition kind, thecopyPropertyTableForPinningpath (setPrototypeOf,$vm.toCacheableDictionary) and the one-shot transitions on fresh objects; the type profiler reads a departed structure's shape from the object, with a test;process.envis named above and covered by the Bun test.Who could read a departed dictionary's table.
GetByStatus,PutByStatus,InByStatusandComplexGetStatuscallgetConcurrentlyon structures from inline caches and bytecode metadata: they now skip a departed structure, and without the skip they returned the slow variant for the whole access.PropertyCondition::isStillValidand the adaptive watchpoints readobject()->structure(), the live one, but a compiler thread holds that pointer across the two reads ofisWatchable, and the object can leave the structure in between: the second read then finds no property and the condition is not watchable, where it asserted before. TheRELEASE_ASSERT(isWatchable)callers inDFGAdaptiveStructureWatchpointandLLIntPrototypeLoadAdaptiveStructureWatchpointrun on the main thread with the live structure.TypeProfilerLog::processLogEntriesreads a logged structure later on the main thread throughforEachPropertyConcurrently, which does not materialize: a departed one gave an empty shape, now it uses the object's structure. ThestructureEnsuresValiditycallers in the DFG pass structures withConcurrency::ConcurrentThread, which never materializes.ObjectPropertyConditionSetgeneration,presenceConditionIfConsistentandtryGetConstantPropertyread live structures. The inline cache compiler callsstartWatchingPropertyForReplacementswith an offset, not a name.tryCacheDeleteByandtryCachePutByreturn before any table read when the old structure is a dictionary.checkOffsetConsistencyruns in release builds and would crash on a table rebuilt from a dictionary with no table, so the stress run above is also a check that nothing does.Differential runs against the base build, JIT on and off, identical output: delete and re-add with reads and collections on objects of 10 to 5000 properties,
definePropertyattribute flips mixed with adds and deletes,setPrototypeOf,seal,freezeandpreventExtensionsfrom a dictionary, symbol keys and accessors,delete globalThis.xin a loop with built-in reads, inline-cached reads in optimized code across deletes, deleting every property then re-adding, private brands on a dictionary, for-in with deletes and attribute changes in the loop body, spread,Object.assign,Reflect.ownKeys,JSON.stringify.Review. A design review weighed three places for the fix: the two sites of the symptom (delete and attribute change), the dictionary policy (convert to an uncacheable dictionary on the first delete), and the helper every transition gets its table through. It picked the helper, with the departed marker for the status layer. A self-review of the diff follows in the PR.