<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[214071] trunk/Source/JavaScriptCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/214071">214071</a></dd>
<dt>Author</dt> <dd>mark.lam@apple.com</dd>
<dt>Date</dt> <dd>2017-03-16 14:53:33 -0700 (Thu, 16 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>The new array with spread operation needs to check for length overflows.
https://bugs.webkit.org/show_bug.cgi?id=169780
&lt;rdar://problem/31072182&gt;

Reviewed by Filip Pizlo.

* dfg/DFGOperations.cpp:
* dfg/DFGSpeculativeJIT.cpp:
(JSC::DFG::SpeculativeJIT::compileNewArrayWithSpread):
* ftl/FTLLowerDFGToB3.cpp:
(JSC::FTL::DFG::LowerDFGToB3::compileNewArrayWithSpread):
* ftl/FTLOperations.cpp:
(JSC::FTL::operationMaterializeObjectInOSR):
* llint/LLIntSlowPaths.cpp:
* runtime/CommonSlowPaths.cpp:
(JSC::SLOW_PATH_DECL):
* runtime/JSGlobalObject.cpp:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGOperationscpp">trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGSpeculativeJITcpp">trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLLowerDFGToB3cpp">trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLOperationscpp">trunk/Source/JavaScriptCore/ftl/FTLOperations.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorellintLLIntSlowPathscpp">trunk/Source/JavaScriptCore/llint/LLIntSlowPaths.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeCommonSlowPathscpp">trunk/Source/JavaScriptCore/runtime/CommonSlowPaths.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSGlobalObjectcpp">trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (214070 => 214071)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2017-03-16 21:49:30 UTC (rev 214070)
+++ trunk/Source/JavaScriptCore/ChangeLog        2017-03-16 21:53:33 UTC (rev 214071)
</span><span class="lines">@@ -1,3 +1,23 @@
</span><ins>+2017-03-16  Mark Lam  &lt;mark.lam@apple.com&gt;
+
+        The new array with spread operation needs to check for length overflows.
+        https://bugs.webkit.org/show_bug.cgi?id=169780
+        &lt;rdar://problem/31072182&gt;
+
+        Reviewed by Filip Pizlo.
+
+        * dfg/DFGOperations.cpp:
+        * dfg/DFGSpeculativeJIT.cpp:
+        (JSC::DFG::SpeculativeJIT::compileNewArrayWithSpread):
+        * ftl/FTLLowerDFGToB3.cpp:
+        (JSC::FTL::DFG::LowerDFGToB3::compileNewArrayWithSpread):
+        * ftl/FTLOperations.cpp:
+        (JSC::FTL::operationMaterializeObjectInOSR):
+        * llint/LLIntSlowPaths.cpp:
+        * runtime/CommonSlowPaths.cpp:
+        (JSC::SLOW_PATH_DECL):
+        * runtime/JSGlobalObject.cpp:
+
</ins><span class="cx"> 2017-03-16  Filip Pizlo  &lt;fpizlo@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         FTL should support global and eval code
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGOperationscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp (214070 => 214071)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp        2017-03-16 21:49:30 UTC (rev 214070)
+++ trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp        2017-03-16 21:53:33 UTC (rev 214071)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2011, 2013-2016 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2011-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -1998,16 +1998,21 @@
</span><span class="cx">     auto scope = DECLARE_THROW_SCOPE(vm);
</span><span class="cx"> 
</span><span class="cx">     EncodedJSValue* values = static_cast&lt;EncodedJSValue*&gt;(buffer);
</span><del>-    unsigned length = 0;
</del><ins>+    Checked&lt;unsigned, RecordOverflow&gt; checkedLength = 0;
</ins><span class="cx">     for (unsigned i = 0; i &lt; numItems; i++) {
</span><span class="cx">         JSValue value = JSValue::decode(values[i]);
</span><span class="cx">         if (JSFixedArray* array = jsDynamicCast&lt;JSFixedArray*&gt;(vm, value))
</span><del>-            length += array-&gt;size();
</del><ins>+            checkedLength += array-&gt;size();
</ins><span class="cx">         else
</span><del>-            ++length;
</del><ins>+            ++checkedLength;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    if (UNLIKELY(checkedLength.hasOverflowed())) {
+        throwOutOfMemoryError(exec, scope);
+        return nullptr;
+    }
</ins><span class="cx"> 
</span><ins>+    unsigned length = checkedLength.unsafeGet();
</ins><span class="cx">     JSGlobalObject* globalObject = exec-&gt;lexicalGlobalObject();
</span><span class="cx">     Structure* structure = globalObject-&gt;arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGSpeculativeJITcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp (214070 => 214071)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp        2017-03-16 21:49:30 UTC (rev 214070)
+++ trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp        2017-03-16 21:53:33 UTC (rev 214071)
</span><span class="lines">@@ -7133,7 +7133,7 @@
</span><span class="cx">                     Edge use = m_jit.graph().varArgChild(node, i);
</span><span class="cx">                     SpeculateCellOperand fixedArray(this, use);
</span><span class="cx">                     GPRReg fixedArrayGPR = fixedArray.gpr();
</span><del>-                    m_jit.add32(MacroAssembler::Address(fixedArrayGPR, JSFixedArray::offsetOfSize()), lengthGPR);
</del><ins>+                    speculationCheck(Overflow, JSValueRegs(), nullptr, m_jit.branchAdd32(MacroAssembler::Overflow, MacroAssembler::Address(fixedArrayGPR, JSFixedArray::offsetOfSize()), lengthGPR));
</ins><span class="cx">                 }
</span><span class="cx">             }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLLowerDFGToB3cpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp (214070 => 214071)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp        2017-03-16 21:49:30 UTC (rev 214070)
+++ trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp        2017-03-16 21:53:33 UTC (rev 214071)
</span><span class="lines">@@ -4456,6 +4456,7 @@
</span><span class="cx">             for (unsigned i = 0; i &lt; m_node-&gt;numChildren(); ++i) {
</span><span class="cx">                 if (bitVector-&gt;get(i)) {
</span><span class="cx">                     Edge use = m_graph.varArgChild(m_node, i);
</span><ins>+                    CheckValue* lengthCheck = nullptr;
</ins><span class="cx">                     if (use-&gt;op() == PhantomSpread) {
</span><span class="cx">                         RELEASE_ASSERT(use-&gt;child1()-&gt;op() == PhantomCreateRest);
</span><span class="cx">                         InlineCallFrame* inlineCallFrame = use-&gt;child1()-&gt;origin.semantic.inlineCallFrame;
</span><span class="lines">@@ -4463,11 +4464,13 @@
</span><span class="cx">                         LValue spreadLength = cachedSpreadLengths.ensure(inlineCallFrame, [&amp;] () {
</span><span class="cx">                             return getSpreadLengthFromInlineCallFrame(inlineCallFrame, numberOfArgumentsToSkip);
</span><span class="cx">                         }).iterator-&gt;value;
</span><del>-                        length = m_out.add(length, spreadLength);
</del><ins>+                        lengthCheck = m_out.speculateAdd(length, spreadLength);
</ins><span class="cx">                     } else {
</span><span class="cx">                         LValue fixedArray = lowCell(use);
</span><del>-                        length = m_out.add(length, m_out.load32(fixedArray, m_heaps.JSFixedArray_size));
</del><ins>+                        lengthCheck = m_out.speculateAdd(length, m_out.load32(fixedArray, m_heaps.JSFixedArray_size));
</ins><span class="cx">                     }
</span><ins>+                    blessSpeculation(lengthCheck, Overflow, noValue(), nullptr, m_origin);
+                    length = lengthCheck;
</ins><span class="cx">                 }
</span><span class="cx">             }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLOperationscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLOperations.cpp (214070 => 214071)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLOperations.cpp        2017-03-16 21:49:30 UTC (rev 214070)
+++ trunk/Source/JavaScriptCore/ftl/FTLOperations.cpp        2017-03-16 21:53:33 UTC (rev 214071)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2014, 2015 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2014-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -438,7 +438,7 @@
</span><span class="cx">         JSGlobalObject* globalObject = codeBlock-&gt;globalObject();
</span><span class="cx">         Structure* structure = globalObject-&gt;arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous);
</span><span class="cx"> 
</span><del>-        unsigned arraySize = 0;
</del><ins>+        Checked&lt;unsigned, RecordOverflow&gt; checkedArraySize = 0;
</ins><span class="cx">         unsigned numProperties = 0;
</span><span class="cx">         for (unsigned i = materialization-&gt;properties().size(); i--;) {
</span><span class="cx">             const ExitPropertyValue&amp; property = materialization-&gt;properties()[i];
</span><span class="lines">@@ -446,12 +446,13 @@
</span><span class="cx">                 ++numProperties;
</span><span class="cx">                 JSValue value = JSValue::decode(values[i]);
</span><span class="cx">                 if (JSFixedArray* fixedArray = jsDynamicCast&lt;JSFixedArray*&gt;(vm, value))
</span><del>-                    arraySize += fixedArray-&gt;size();
</del><ins>+                    checkedArraySize += fixedArray-&gt;size();
</ins><span class="cx">                 else
</span><del>-                    arraySize += 1;
</del><ins>+                    checkedArraySize += 1;
</ins><span class="cx">             }
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        unsigned arraySize = checkedArraySize.unsafeGet(); // Crashes if overflowed.
</ins><span class="cx">         JSArray* result = JSArray::tryCreateForInitializationPrivate(vm, structure, arraySize);
</span><span class="cx">         RELEASE_ASSERT(result);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorellintLLIntSlowPathscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/llint/LLIntSlowPaths.cpp (214070 => 214071)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/llint/LLIntSlowPaths.cpp        2017-03-16 21:49:30 UTC (rev 214070)
+++ trunk/Source/JavaScriptCore/llint/LLIntSlowPaths.cpp        2017-03-16 21:53:33 UTC (rev 214071)
</span><span class="lines">@@ -47,7 +47,6 @@
</span><span class="cx"> #include &quot;JSAsyncFunction.h&quot;
</span><span class="cx"> #include &quot;JSCInlines.h&quot;
</span><span class="cx"> #include &quot;JSCJSValue.h&quot;
</span><del>-#include &quot;JSFixedArray.h&quot;
</del><span class="cx"> #include &quot;JSGeneratorFunction.h&quot;
</span><span class="cx"> #include &quot;JSGlobalObjectFunctions.h&quot;
</span><span class="cx"> #include &quot;JSLexicalEnvironment.h&quot;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeCommonSlowPathscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/CommonSlowPaths.cpp (214070 => 214071)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/CommonSlowPaths.cpp        2017-03-16 21:49:30 UTC (rev 214070)
+++ trunk/Source/JavaScriptCore/runtime/CommonSlowPaths.cpp        2017-03-16 21:53:33 UTC (rev 214071)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2011-2016 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2011-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -993,16 +993,19 @@
</span><span class="cx"> 
</span><span class="cx">     JSValue* values = bitwise_cast&lt;JSValue*&gt;(&amp;OP(2));
</span><span class="cx"> 
</span><del>-    unsigned arraySize = 0;
</del><ins>+    Checked&lt;unsigned, RecordOverflow&gt; checkedArraySize = 0;
</ins><span class="cx">     for (int i = 0; i &lt; numItems; i++) {
</span><span class="cx">         if (bitVector.get(i)) {
</span><span class="cx">             JSValue value = values[-i];
</span><span class="cx">             JSFixedArray* array = jsCast&lt;JSFixedArray*&gt;(value);
</span><del>-            arraySize += array-&gt;size();
</del><ins>+            checkedArraySize += array-&gt;size();
</ins><span class="cx">         } else
</span><del>-            arraySize += 1;
</del><ins>+            checkedArraySize += 1;
</ins><span class="cx">     }
</span><ins>+    if (UNLIKELY(checkedArraySize.hasOverflowed()))
+        THROW(createOutOfMemoryError(exec));
</ins><span class="cx"> 
</span><ins>+    unsigned arraySize = checkedArraySize.unsafeGet();
</ins><span class="cx">     JSGlobalObject* globalObject = exec-&gt;lexicalGlobalObject();
</span><span class="cx">     Structure* structure = globalObject-&gt;arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSGlobalObjectcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp (214070 => 214071)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp        2017-03-16 21:49:30 UTC (rev 214070)
+++ trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp        2017-03-16 21:53:33 UTC (rev 214071)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2007-2009, 2014-2017 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2007-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  * Copyright (C) 2008 Cameron Zwarich (cwzwarich@uwaterloo.ca)
</span><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="lines">@@ -80,7 +80,6 @@
</span><span class="cx"> #include &quot;JSDataViewPrototype.h&quot;
</span><span class="cx"> #include &quot;JSDollarVM.h&quot;
</span><span class="cx"> #include &quot;JSDollarVMPrototype.h&quot;
</span><del>-#include &quot;JSFixedArray.h&quot;
</del><span class="cx"> #include &quot;JSFunction.h&quot;
</span><span class="cx"> #include &quot;JSGeneratorFunction.h&quot;
</span><span class="cx"> #include &quot;JSGenericTypedArrayViewConstructorInlines.h&quot;
</span></span></pre>
</div>
</div>

</body>
</html>