You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit a3c8dbf
Browse filesBrowse the repository at this point in the historyBrowse files
[JSC] Avoid accessing missing indexing headers in Butterfly helpers.
https://bugs.webkit.org/show_bug.cgi?id=324343
Reviewed by Yusuke Suzuki.
An object whose properties all have names, and which has no indexed elements,
keeps them in a butterfly with no IndexingHeader, since that header only
describes indexed elements. Butterfly::indexingHeader() hands back a pointer to
one regardless, by subtracting a slot from the butterfly pointer. It cannot do
better, because it never sees the Structure and so has no way to know whether a
header is there; on a headerless butterfly the address it returns lands just
past the end of the allocation.
preCapacity() and indexingPayloadSizeInBytes() were reached through it. Both
consult the indexing type and answer zero when nothing is indexed, so they look
safe, but that check ran too late: calling them meant deriving the header
pointer first and passing it as the object they operate on. Inside a non-static
member function the language guarantees that the "this" object is really there,
so a compiler need not prove that reading its fields is safe; the call itself is
the proof. GCC uses that, from version 13: it speculatively dereferences the
header pointer before the check has run, so the check still answers zero but too
late to prevent the read. On a headerless butterfly that read comes from an
address outside the butterfly's allocation which crashes when unmapped.
For more details see the analysis at gcc.gnu.org/PR127434 which was filed
there first as a compiler bug.
Whether that read crashes depends on what follows the butterfly in memory.
WPE 2.52 crashed on 32-bit ARM while growing named property storage because
bmalloc, which it used there, can end a heap block right before an unmapped
page. Main does not reproduce it, since libpas, mimalloc and the system malloc
all keep mapped memory after a heap block, so the read succeeds and its value
is discarded; nothing uses that value unless the indexing type says a header
exists, so a successful read is harmless. However, the read is still in the
code main compiles. I verified this by preloading 2.52 bmalloc into a main
build and that brings the crash back on 32-bit ARM, in the same place.
Since the pointer is formed before the check can run, no guard inside those
functions can help. Move both queries to Butterfly, which is what they describe:
they answer how large this butterfly indexed payload is and how much room sits
before its property storage, they feed Butterfly::totalSize(), and Butterfly
already hosts this kind of structure-aware layout query next to base(Structure*).
They consult the Structure first and only derive a header in the branches whose
indexing type guarantees one. IndexingHeader is then left with no function that
takes a Structure, which is the rule worth keeping: needing a Structure to answer
means having to cope with there being no header, so the header cannot be the
object the function is called on. VMInspector and ObjectInitializationScope read
header fields before deciding one existed, so they are fixed in a similar way.
The new test adds named properties to objects until their storage has to grow,
and runs with --libpasForcePGMWithRate=1, which puts an inaccessible page after
every allocation so the early read faults instead of landing in readable heap
bytes. Before this patch, a build of main with GCC at -O2 crashes always, while
Clang 22 passes because it leaves the read inside the guard. So the test only
catches this when the compiler speculatively moves the read.
It is worth running everywhere regardless, because the mistake is in our code
rather than in the compiler, and a newer Clang version could start moving the
read too.
* JSTests/stress/butterfly-headerless-property-growth.js: Added.
* Source/JavaScriptCore/CMakeLists.txt:
* Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj:
* Source/JavaScriptCore/runtime/Butterfly.h:
(JSC::Butterfly::propertyStorage):
(JSC::Butterfly::propertyStorage const):
(JSC::Butterfly::indexOfPropertyStorage):
* Source/JavaScriptCore/runtime/ButterflyInlines.h:
(JSC::Butterfly::preCapacity):
(JSC::Butterfly::indexingPayloadSizeInBytes):
(JSC::Butterfly::base):
(JSC::Butterfly::createOrGrowPropertyStorage):
(JSC::Butterfly::growArrayRight):
(JSC::Butterfly::reallocArrayRightIfPossible):
(JSC::Butterfly::resizeArray):
(JSC::Butterfly::unshift):
* Source/JavaScriptCore/runtime/IndexingHeader.h:
* Source/JavaScriptCore/runtime/IndexingHeaderInlines.h: Removed.
* Source/JavaScriptCore/runtime/JSObject.cpp:
(JSC::JSObjectWithButterfly::markAuxiliaryAndVisitOutOfLineProperties):
(JSC::JSObject::reallocateAndShrinkButterfly):
(JSC::JSObject::shiftButterflyAfterFlattening):
* Source/JavaScriptCore/runtime/JSObject.h:
(JSC::JSObject::butterflyPreCapacity):
(JSC::JSObject::butterflyTotalSize):
* Source/JavaScriptCore/runtime/ObjectInitializationScope.cpp:
(JSC::ObjectInitializationScope::verifyPropertiesAreInitialized):
* Source/JavaScriptCore/runtime/RegExpMatchesArray.cpp:
(JSC::createRegExpMatchesArrayWithGroupsOrIndices):
(JSC::createRegExpMatchesArrayForPlainRegExpHavingABadTime):
* Source/JavaScriptCore/runtime/RegExpMatchesArray.h:
(JSC::createRegExpMatchesArrayForPlainRegExp):
* Source/JavaScriptCore/runtime/Structure.cpp:
(JSC::Structure::flattenDictionaryStructure):
* Source/JavaScriptCore/tools/VMInspector.cpp:
(JSC::VMInspector::dumpCellMemoryToStream):
* Source/WebCore/WebCorePrefix.h:
* Source/WebKit/WebKitPrefix.h:
Canonical link: https://commits.webkit.org/324229@main
0 commit comments