Skip to content

Commit

Permalink
Cherry-pick 252432.1031@safari-7614-branch (9f7e401). https://bugs.we…
Browse files Browse the repository at this point in the history
…bkit.org/show_bug.cgi?id=250429

    Fix use-after-free in DFGFixupPhase for array indexOf
    https://bugs.webkit.org/show_bug.cgi?id=250429
    rdar://103852510

    Reviewed by Jonathan Bedard and Michael Saboff.

    During DFG fixup, array indexOf nodes are folded to -1 when the search element is speculated
    to be a different type than the array element (for instance, JSCell instead of Int32). When
    this happens, a speculation check is inserted, which can cause the DFG graph's varArgChildren
    array to reallocate. This invalidates the searchElement Edge reference, which we use
    immediately after the check insertion in the fixup phase. This patch fixes this potential
    use-after-free by grabbing the searchElement's associated node before inserting any checks,
    giving us a persistent pointer to a DFG node rather than a reference into a vector.

    * JSTests/stress/cell-speculated-array-indexof.js: Added.
    * Source/JavaScriptCore/dfg/DFGFixupPhase.cpp:
    (JSC::DFG::FixupPhase::fixupArrayIndexOf):

    Canonical link: https://commits.webkit.org/252432.1031@safari-7614-branch
  • Loading branch information
ddegazio authored and aperezdc committed Apr 5, 2023
1 parent b259c46 commit d88b287
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 10 additions & 0 deletions JSTests/stress/cell-speculated-array-indexof.js
@@ -0,0 +1,10 @@
for (let i = 0; i < 10000; ++i) {
const v0 = [];
const v1 = v0.length;
v0[0] %= v1;
const v2 = [0];
const v3 = v2.slice(v2);
const v4 = v2.indexOf(v3, 0);
const v5 = new Float64Array(0, 0, v1);
}

8 changes: 5 additions & 3 deletions Source/JavaScriptCore/dfg/DFGFixupPhase.cpp
Expand Up @@ -4316,24 +4316,26 @@ class FixupPhase : public Phase {
switch (node->arrayMode().type()) {
case Array::Double:
case Array::Int32: {
Node* searchElementNode = searchElement.node();

if (searchElement->shouldSpeculateCell()) {
m_insertionSet.insertNode(m_indexInBlock, SpecNone, Check, node->origin, Edge(searchElement.node(), CellUse));
m_graph.convertToConstant(node, jsNumber(-1));
observeUseKindOnNode<CellUse>(searchElement.node());
observeUseKindOnNode<CellUse>(searchElementNode);
return;
}

if (searchElement->shouldSpeculateOther()) {
m_insertionSet.insertNode(m_indexInBlock, SpecNone, Check, node->origin, Edge(searchElement.node(), OtherUse));
m_graph.convertToConstant(node, jsNumber(-1));
observeUseKindOnNode<OtherUse>(searchElement.node());
observeUseKindOnNode<OtherUse>(searchElementNode);
return;
}

if (searchElement->shouldSpeculateBoolean()) {
m_insertionSet.insertNode(m_indexInBlock, SpecNone, Check, node->origin, Edge(searchElement.node(), BooleanUse));
m_graph.convertToConstant(node, jsNumber(-1));
observeUseKindOnNode<BooleanUse>(searchElement.node());
observeUseKindOnNode<BooleanUse>(searchElementNode);
return;
}
break;
Expand Down

0 comments on commit d88b287

Please sign in to comment.