Title: [231229] trunk
Revision
231229
Author
[email protected]
Date
2018-05-02 04:21:28 -0700 (Wed, 02 May 2018)

Log Message

emitCodeToGetArgumentsArrayLength should not crash on PhantomNewArrayWithSpread
https://bugs.webkit.org/show_bug.cgi?id=183172

Reviewed by Filip Pizlo.

JSTests:

* stress/length-of-new-array-with-spread.js: Added.
(foo):
(bar):
(baz):

Source/_javascript_Core:

DFGArgumentsEliminationPhase.cpp currently believes that allocations of NewArrayWithSpread can be deleted if they are only used by GetArrayLength,
but when it then calls emitCodeToGetArgumentsArrayLength, the latter has no idea what to do with GetArrayLength.

I fix the problem by teaching emitCodeToGetArgumentsArrayLength how to deal with GetArrayLength.
Because this requires emitting an Add that can overflow and thus exit, we also tell DFGArgumentsEliminationPhase to give up on eliminating
a NewArrayWithSpread when it is used by a GetArrayLength that is not allowed to exit.

* dfg/DFGArgumentsEliminationPhase.cpp:
* dfg/DFGArgumentsUtilities.cpp:
(JSC::DFG::emitCodeToGetArgumentsArrayLength):

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (231228 => 231229)


--- trunk/JSTests/ChangeLog	2018-05-02 11:06:50 UTC (rev 231228)
+++ trunk/JSTests/ChangeLog	2018-05-02 11:21:28 UTC (rev 231229)
@@ -1,3 +1,15 @@
+2018-05-02  Robin Morisset  <[email protected]>
+
+        emitCodeToGetArgumentsArrayLength should not crash on PhantomNewArrayWithSpread
+        https://bugs.webkit.org/show_bug.cgi?id=183172
+
+        Reviewed by Filip Pizlo.
+
+        * stress/length-of-new-array-with-spread.js: Added.
+        (foo):
+        (bar):
+        (baz):
+
 2018-05-01  Yusuke Suzuki  <[email protected]>
 
         [JSC] Add SameValue DFG node

Added: trunk/JSTests/stress/length-of-new-array-with-spread.js (0 => 231229)


--- trunk/JSTests/stress/length-of-new-array-with-spread.js	                        (rev 0)
+++ trunk/JSTests/stress/length-of-new-array-with-spread.js	2018-05-02 11:21:28 UTC (rev 231229)
@@ -0,0 +1,31 @@
+function foo()
+{
+    var a = [1, 2];
+    var l = [...a, 42, ...a].length;
+    if (l != 5)
+        throw "Wrong length in foo: " + l;
+}
+noInline(foo);
+
+function bar(...b)
+{
+    var l = [...b, 43, ...b].length;
+    if (l != 7)
+        throw "Wrong length in bar: " + l
+}
+noInline(bar);
+
+function baz(arg0, ...c)
+{
+    var x = [...c, ...c];
+    var l = [...x, ...x, ...x].length;
+    if (l != 24)
+        throw "Wrong length in baz: " + l
+}
+noInline(baz);
+
+for (var i = 0; i < 10000; ++i) {
+    foo();
+    bar(1, 2, 3);
+    baz(0, 1, 2, 3, 4);
+}

Modified: trunk/Source/_javascript_Core/ChangeLog (231228 => 231229)


--- trunk/Source/_javascript_Core/ChangeLog	2018-05-02 11:06:50 UTC (rev 231228)
+++ trunk/Source/_javascript_Core/ChangeLog	2018-05-02 11:21:28 UTC (rev 231229)
@@ -1,3 +1,21 @@
+2018-05-02  Robin Morisset  <[email protected]>
+
+        emitCodeToGetArgumentsArrayLength should not crash on PhantomNewArrayWithSpread
+        https://bugs.webkit.org/show_bug.cgi?id=183172
+
+        Reviewed by Filip Pizlo.
+
+        DFGArgumentsEliminationPhase.cpp currently believes that allocations of NewArrayWithSpread can be deleted if they are only used by GetArrayLength,
+        but when it then calls emitCodeToGetArgumentsArrayLength, the latter has no idea what to do with GetArrayLength.
+
+        I fix the problem by teaching emitCodeToGetArgumentsArrayLength how to deal with GetArrayLength.
+        Because this requires emitting an Add that can overflow and thus exit, we also tell DFGArgumentsEliminationPhase to give up on eliminating
+        a NewArrayWithSpread when it is used by a GetArrayLength that is not allowed to exit.
+
+        * dfg/DFGArgumentsEliminationPhase.cpp:
+        * dfg/DFGArgumentsUtilities.cpp:
+        (JSC::DFG::emitCodeToGetArgumentsArrayLength):
+
 2018-05-02  Yusuke Suzuki  <[email protected]>
 
         Unreviewed, stackPointer signature is different from declaration

Modified: trunk/Source/_javascript_Core/dfg/DFGArgumentsEliminationPhase.cpp (231228 => 231229)


--- trunk/Source/_javascript_Core/dfg/DFGArgumentsEliminationPhase.cpp	2018-05-02 11:06:50 UTC (rev 231228)
+++ trunk/Source/_javascript_Core/dfg/DFGArgumentsEliminationPhase.cpp	2018-05-02 11:21:28 UTC (rev 231229)
@@ -128,7 +128,7 @@
                 case NewArrayWithSpread: {
                     if (m_graph.isWatchingHavingABadTimeWatchpoint(node)) {
                         BitVector* bitVector = node->bitVector();
-                        // We only allow for Spreads to be of rest nodes for now.
+                        // We only allow for Spreads to be of CreateRest or NewArrayBuffer nodes for now.
                         bool isOK = true;
                         for (unsigned i = 0; i < node->numChildren(); i++) {
                             if (bitVector->get(i)) {
@@ -320,8 +320,11 @@
                     break;
 
                 case GetArrayLength:
-                    // FIXME: It would not be hard to support NewArrayWithSpread here if it is only over Spread(CreateRest) nodes.
                     escape(node->child2(), node);
+                    // Computing the length of a NewArrayWithSpread can require some additions.
+                    // These additions can overflow if the array is sufficiently enormous, and in that case we will need to exit.
+                    if ((node->child1()->op() == NewArrayWithSpread) && !node->origin.exitOK)
+                        escape(node->child1(), node);
                     break;
 
                 case NewArrayWithSpread: {

Modified: trunk/Source/_javascript_Core/dfg/DFGArgumentsUtilities.cpp (231228 => 231229)


--- trunk/Source/_javascript_Core/dfg/DFGArgumentsUtilities.cpp	2018-05-02 11:06:50 UTC (rev 231228)
+++ trunk/Source/_javascript_Core/dfg/DFGArgumentsUtilities.cpp	2018-05-02 11:21:28 UTC (rev 231229)
@@ -65,11 +65,42 @@
     DFG_ASSERT(
         graph, arguments,
         arguments->op() == CreateDirectArguments || arguments->op() == CreateScopedArguments
-        || arguments->op() == CreateClonedArguments || arguments->op() == CreateRest || arguments->op() == NewArrayBuffer
+        || arguments->op() == CreateClonedArguments || arguments->op() == CreateRest
+        || arguments->op() == NewArrayBuffer
         || arguments->op() == PhantomDirectArguments || arguments->op() == PhantomClonedArguments
-        || arguments->op() == PhantomCreateRest || arguments->op() == PhantomNewArrayBuffer,
+        || arguments->op() == PhantomCreateRest || arguments->op() == PhantomNewArrayBuffer
+        || arguments->op() == PhantomNewArrayWithSpread,
         arguments->op());
 
+    if (arguments->op() == PhantomNewArrayWithSpread) {
+        unsigned numberOfNonSpreadArguments = 0;
+        BitVector* bitVector = arguments->bitVector();
+        Node* currentSum = nullptr;
+        for (unsigned i = 0; i < arguments->numChildren(); i++) {
+            if (bitVector->get(i)) {
+                Node* child = graph.varArgChild(arguments, i).node();
+                DFG_ASSERT(graph, child, child->op() == PhantomSpread, child->op());
+                DFG_ASSERT(graph, child->child1().node(),
+                    child->child1()->op() == PhantomCreateRest || child->child1()->op() == PhantomNewArrayBuffer,
+                    child->child1()->op());
+                Node* lengthOfChild = emitCodeToGetArgumentsArrayLength(insertionSet, child->child1().node(), nodeIndex, origin);
+                if (currentSum)
+                    currentSum = insertionSet.insertNode(nodeIndex, SpecInt32Only, ArithAdd, origin, OpInfo(Arith::CheckOverflow), Edge(currentSum, Int32Use), Edge(lengthOfChild, Int32Use));
+                else
+                    currentSum = lengthOfChild;
+            } else
+                numberOfNonSpreadArguments++;
+        }
+        if (currentSum) {
+            if (!numberOfNonSpreadArguments)
+                return currentSum;
+            return insertionSet.insertNode(
+                nodeIndex, SpecInt32Only, ArithAdd, origin, OpInfo(Arith::CheckOverflow), Edge(currentSum, Int32Use),
+                insertionSet.insertConstantForUse(nodeIndex, origin, jsNumber(numberOfNonSpreadArguments), Int32Use));
+        }
+        return insertionSet.insertConstant(nodeIndex, origin, jsNumber(numberOfNonSpreadArguments));
+    }
+
     if (arguments->op() == NewArrayBuffer || arguments->op() == PhantomNewArrayBuffer) {
         return insertionSet.insertConstant(
             nodeIndex, origin, jsNumber(arguments->castOperand<JSFixedArray*>()->length()));
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to