<!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>[184781] 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/184781">184781</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2015-05-22 11:48:03 -0700 (Fri, 22 May 2015)</dd>
</dl>
<h3>Log Message</h3>
<pre>Arguments elimination phase mishandles arity check failure in its reduction of LoadVarargs to GetStack/PutStacks
https://bugs.webkit.org/show_bug.cgi?id=145298
Reviewed by Geoffrey Garen.
* dfg/DFGArgumentsEliminationPhase.cpp: Fix the bug. I restructured the loop to make it more obvious that we're initializing everything that we're supposed to initialize.
* dfg/DFGNode.h: Add a comment to clarify something I was confused about while writing this code.
* dfg/DFGPutStackSinkingPhase.cpp: Hacking on PutStacks made me think deep thoughts, and I added some FIXMEs.
* tests/stress/fold-load-varargs-arity-check-fail-barely.js: Added. This test crashes or fails before this patch.
* tests/stress/fold-load-varargs-arity-check-fail.js: Added. This is even more sure to crash or fail.
* tests/stress/simplify-varargs-mandatory-minimum-smaller-than-limit.js: Added. Not sure if we had coverage for this case before.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGArgumentsEliminationPhasecpp">trunk/Source/JavaScriptCore/dfg/DFGArgumentsEliminationPhase.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGNodeh">trunk/Source/JavaScriptCore/dfg/DFGNode.h</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGPutStackSinkingPhasecpp">trunk/Source/JavaScriptCore/dfg/DFGPutStackSinkingPhase.cpp</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressfoldloadvarargsaritycheckfailbarelyjs">trunk/Source/JavaScriptCore/tests/stress/fold-load-varargs-arity-check-fail-barely.js</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstressfoldloadvarargsaritycheckfailjs">trunk/Source/JavaScriptCore/tests/stress/fold-load-varargs-arity-check-fail.js</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstresssimplifyvarargsmandatoryminimumsmallerthanlimitjs">trunk/Source/JavaScriptCore/tests/stress/simplify-varargs-mandatory-minimum-smaller-than-limit.js</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (184780 => 184781)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-05-22 18:43:42 UTC (rev 184780)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-05-22 18:48:03 UTC (rev 184781)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2015-05-21 Filip Pizlo <fpizlo@apple.com>
+
+ Arguments elimination phase mishandles arity check failure in its reduction of LoadVarargs to GetStack/PutStacks
+ https://bugs.webkit.org/show_bug.cgi?id=145298
+
+ Reviewed by Geoffrey Garen.
+
+ * dfg/DFGArgumentsEliminationPhase.cpp: Fix the bug. I restructured the loop to make it more obvious that we're initializing everything that we're supposed to initialize.
+ * dfg/DFGNode.h: Add a comment to clarify something I was confused about while writing this code.
+ * dfg/DFGPutStackSinkingPhase.cpp: Hacking on PutStacks made me think deep thoughts, and I added some FIXMEs.
+ * tests/stress/fold-load-varargs-arity-check-fail-barely.js: Added. This test crashes or fails before this patch.
+ * tests/stress/fold-load-varargs-arity-check-fail.js: Added. This is even more sure to crash or fail.
+ * tests/stress/simplify-varargs-mandatory-minimum-smaller-than-limit.js: Added. Not sure if we had coverage for this case before.
+
</ins><span class="cx"> 2015-05-22 Basile Clement <basile_clement@apple.com>
</span><span class="cx">
</span><span class="cx"> Allow DFGClobberize to return non-node constants that must be later created
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGArgumentsEliminationPhasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGArgumentsEliminationPhase.cpp (184780 => 184781)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGArgumentsEliminationPhase.cpp        2015-05-22 18:43:42 UTC (rev 184780)
+++ trunk/Source/JavaScriptCore/dfg/DFGArgumentsEliminationPhase.cpp        2015-05-22 18:48:03 UTC (rev 184781)
</span><span class="lines">@@ -496,24 +496,52 @@
</span><span class="cx"> OpInfo(m_graph.m_stackAccessData.add(varargsData->count, FlushedInt32)),
</span><span class="cx"> Edge(argumentCount, Int32Use));
</span><span class="cx">
</span><del>- for (unsigned i = 1 + varargsData->offset; i < inlineCallFrame->arguments.size(); ++i) {
- StackAccessData* data = m_graph.m_stackAccessData.add(
- virtualRegisterForArgument(i) + inlineCallFrame->stackOffset,
- FlushedJSValue);
</del><ins>+ DFG_ASSERT(m_graph, node, varargsData->limit - 1 >= varargsData->mandatoryMinimum);
+ // Define our limit to not include "this", since that's a bit easier to reason about.
+ unsigned limit = varargsData->limit - 1;
+ Node* undefined = nullptr;
+ for (unsigned storeIndex = 0; storeIndex < limit; ++storeIndex) {
+ // First determine if we have an element we can load, and load it if
+ // possible.
</ins><span class="cx">
</span><del>- Node* value = insertionSet.insertNode(
- nodeIndex, SpecNone, GetStack, node->origin, OpInfo(data));
</del><ins>+ unsigned loadIndex = storeIndex + varargsData->offset;
</ins><span class="cx">
</span><del>- VirtualRegister reg = varargsData->start + i - 1 - varargsData->offset;
</del><ins>+ Node* value;
+ if (loadIndex + 1 < inlineCallFrame->arguments.size()) {
+ VirtualRegister reg =
+ virtualRegisterForArgument(loadIndex + 1) +
+ inlineCallFrame->stackOffset;
+ StackAccessData* data = m_graph.m_stackAccessData.add(
+ reg, FlushedJSValue);
+
+ value = insertionSet.insertNode(
+ nodeIndex, SpecNone, GetStack, node->origin, OpInfo(data));
+ } else {
+ // Check if this an element that we must initialize.
+ if (storeIndex >= varargsData->mandatoryMinimum) {
+ // It's not. We're done.
+ break;
+ }
+
+ if (!undefined) {
+ undefined = insertionSet.insertConstant(
+ nodeIndex, node->origin, jsUndefined());
+ }
+ value = undefined;
+ }
</ins><span class="cx">
</span><ins>+ // Now that we have a value, store it.
+
+ VirtualRegister reg = varargsData->start + storeIndex;
+ StackAccessData* data =
+ m_graph.m_stackAccessData.add(reg, FlushedJSValue);
+
</ins><span class="cx"> insertionSet.insertNode(
</span><span class="cx"> nodeIndex, SpecNone, MovHint, node->origin, OpInfo(reg.offset()),
</span><span class="cx"> Edge(value));
</span><del>-
- data = m_graph.m_stackAccessData.add(reg, FlushedJSValue);
-
</del><span class="cx"> insertionSet.insertNode(
</span><del>- nodeIndex, SpecNone, PutStack, node->origin, OpInfo(data), Edge(value));
</del><ins>+ nodeIndex, SpecNone, PutStack, node->origin, OpInfo(data),
+ Edge(value));
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> node->remove();
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGNodeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGNode.h (184780 => 184781)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGNode.h        2015-05-22 18:43:42 UTC (rev 184780)
+++ trunk/Source/JavaScriptCore/dfg/DFGNode.h        2015-05-22 18:48:03 UTC (rev 184781)
</span><span class="lines">@@ -190,7 +190,7 @@
</span><span class="cx"> };
</span><span class="cx">
</span><span class="cx"> struct LoadVarargsData {
</span><del>- VirtualRegister start; // Local for the first element.
</del><ins>+ VirtualRegister start; // Local for the first element. This is the first actual argument, not this.
</ins><span class="cx"> VirtualRegister count; // Local for the count.
</span><span class="cx"> VirtualRegister machineStart;
</span><span class="cx"> VirtualRegister machineCount;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGPutStackSinkingPhasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGPutStackSinkingPhase.cpp (184780 => 184781)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGPutStackSinkingPhase.cpp        2015-05-22 18:43:42 UTC (rev 184780)
+++ trunk/Source/JavaScriptCore/dfg/DFGPutStackSinkingPhase.cpp        2015-05-22 18:48:03 UTC (rev 184781)
</span><span class="lines">@@ -64,6 +64,13 @@
</span><span class="cx"> // after SSA conversion, we have almost a guarantee that the Phi graph we produce here would
</span><span class="cx"> // be trivially redundant to the one we already have.
</span><span class="cx">
</span><ins>+ // FIXME: This phase doesn't adequately use KillStacks. KillStack can be viewed as a def.
+ // This is mostly inconsequential; it would be a bug to have a local live at a KillStack.
+ // More important is that KillStack should swallow any deferral. After a KillStack, the
+ // local should behave like a TOP deferral because it would be invalid for anyone to trust
+ // the stack. It's not clear to me if this is important or not.
+ // https://bugs.webkit.org/show_bug.cgi?id=145296
+
</ins><span class="cx"> if (verbose) {
</span><span class="cx"> dataLog("Graph before PutStack sinking:\n");
</span><span class="cx"> m_graph.dump();
</span><span class="lines">@@ -107,6 +114,10 @@
</span><span class="cx"> live.operand(operand) = true;
</span><span class="cx"> };
</span><span class="cx">
</span><ins>+ // FIXME: This might mishandle LoadVarargs and ForwardVarargs. It might make us
+ // think that the locals being written are stack-live here. They aren't. This
+ // should be harmless since we overwrite them anyway, but still, it's sloppy.
+ // https://bugs.webkit.org/show_bug.cgi?id=145295
</ins><span class="cx"> preciseLocalClobberize(
</span><span class="cx"> m_graph, node, escapeHandler, escapeHandler,
</span><span class="cx"> [&] (VirtualRegister operand, LazyNode source) {
</span><span class="lines">@@ -487,7 +498,7 @@
</span><span class="cx"> insertionSet.execute(block);
</span><span class="cx"> }
</span><span class="cx">
</span><del>- // Finally eliminate the sunken PutStacks by turning them into Phantoms. This keeps whatever
</del><ins>+ // Finally eliminate the sunken PutStacks by turning them into Checks. This keeps whatever
</ins><span class="cx"> // type check they were doing. Also prepend KillStacks to them to ensure that we know that
</span><span class="cx"> // the relevant value was *not* stored to the stack.
</span><span class="cx"> for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressfoldloadvarargsaritycheckfailbarelyjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/fold-load-varargs-arity-check-fail-barely.js (0 => 184781)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/fold-load-varargs-arity-check-fail-barely.js         (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/fold-load-varargs-arity-check-fail-barely.js        2015-05-22 18:48:03 UTC (rev 184781)
</span><span class="lines">@@ -0,0 +1,28 @@
</span><ins>+function foo(a, b) {
+ return [a, b];
+}
+
+function bar() {
+ return foo.apply(this, arguments);
+}
+
+function baz() {
+ return bar(42);
+}
+
+noInline(baz);
+
+for (var i = 0; i < 10000; ++i) {
+ var result = baz();
+ if (!(result instanceof Array))
+ throw "Error: result is not an array.";
+ if (result.length != 2)
+ throw "Error: result doesn't have length 4.";
+ if (result[0] != 42)
+ throw "Error: first element is not 42: " + result[0];
+ for (var j = 1; j < 2; ++j) {
+ if (result[j] !== void 0)
+ throw "Error: element " + j + " is not undefined: " + result[j];
+ }
+}
+
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressfoldloadvarargsaritycheckfailjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/fold-load-varargs-arity-check-fail.js (0 => 184781)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/fold-load-varargs-arity-check-fail.js         (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/fold-load-varargs-arity-check-fail.js        2015-05-22 18:48:03 UTC (rev 184781)
</span><span class="lines">@@ -0,0 +1,29 @@
</span><ins>+function foo(a, b, c, d) {
+ return [a, b, c, d];
+}
+
+function bar() {
+ return foo.apply(this, arguments);
+}
+
+function baz() {
+ return bar(42);
+}
+
+noInline(baz);
+
+for (var i = 0; i < 10000; ++i) {
+ var result = baz();
+ if (!(result instanceof Array))
+ throw "Error: result is not an array.";
+ if (result.length != 4)
+ throw "Error: result doesn't have length 4.";
+ if (result[0] != 42)
+ throw "Error: first element is not 42: " + result[0];
+ for (var j = 1; j < 4; ++j) {
+ if (result[j] !== void 0)
+ throw "Error: element " + j + " is not undefined: " + result[j];
+ }
+}
+
+
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstresssimplifyvarargsmandatoryminimumsmallerthanlimitjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/simplify-varargs-mandatory-minimum-smaller-than-limit.js (0 => 184781)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/simplify-varargs-mandatory-minimum-smaller-than-limit.js         (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/simplify-varargs-mandatory-minimum-smaller-than-limit.js        2015-05-22 18:48:03 UTC (rev 184781)
</span><span class="lines">@@ -0,0 +1,15 @@
</span><ins>+function foo() { return 42; }
+
+function bar() { return foo.apply(this, arguments); }
+
+function baz() { return bar(1, 2, 3); }
+
+noInline(baz);
+
+for (var i = 0; i < 10000; ++i) {
+ var result = baz();
+ if (result != 42)
+ throw "Error: bad result: " + result;
+}
+
+
</ins></span></pre>
</div>
</div>
</body>
</html>