<!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>[183825] 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/183825">183825</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2015-05-05 13:42:44 -0700 (Tue, 05 May 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>FTL SwitchString slow case creates duplicate switch cases
https://bugs.webkit.org/show_bug.cgi?id=144634

Reviewed by Geoffrey Garen.
        
The problem of duplicate switches is sufficiently annoying that I fixed the issue and also
added mostly-debug-only asserts to catch such issues earlier.

* bytecode/CallVariant.cpp:
(JSC::variantListWithVariant): Assertion to prevent similar bugs.
* ftl/FTLLowerDFGToLLVM.cpp:
(JSC::FTL::LowerDFGToLLVM::switchStringRecurse): Assertion to prevent similar bugs.
(JSC::FTL::LowerDFGToLLVM::switchStringSlow): This is the bug.
* jit/BinarySwitch.cpp:
(JSC::BinarySwitch::BinarySwitch): Assertion to prevent similar bugs.
* jit/Repatch.cpp:
(JSC::linkPolymorphicCall): Assertion to prevent similar bugs.
* tests/stress/ftl-switch-string-slow-duplicate-cases.js: Added. This tests the FTL SwitchString bug. It was previously crashing every time.
(foo):
(cat):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorebytecodeCallVariantcpp">trunk/Source/JavaScriptCore/bytecode/CallVariant.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLLowerDFGToLLVMcpp">trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorejitBinarySwitchcpp">trunk/Source/JavaScriptCore/jit/BinarySwitch.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorejitRepatchcpp">trunk/Source/JavaScriptCore/jit/Repatch.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressftlswitchstringslowduplicatecasesjs">trunk/Source/JavaScriptCore/tests/stress/ftl-switch-string-slow-duplicate-cases.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (183824 => 183825)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-05-05 20:14:07 UTC (rev 183824)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-05-05 20:42:44 UTC (rev 183825)
</span><span class="lines">@@ -1,3 +1,26 @@
</span><ins>+2015-05-05  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
+        FTL SwitchString slow case creates duplicate switch cases
+        https://bugs.webkit.org/show_bug.cgi?id=144634
+
+        Reviewed by Geoffrey Garen.
+        
+        The problem of duplicate switches is sufficiently annoying that I fixed the issue and also
+        added mostly-debug-only asserts to catch such issues earlier.
+
+        * bytecode/CallVariant.cpp:
+        (JSC::variantListWithVariant): Assertion to prevent similar bugs.
+        * ftl/FTLLowerDFGToLLVM.cpp:
+        (JSC::FTL::LowerDFGToLLVM::switchStringRecurse): Assertion to prevent similar bugs.
+        (JSC::FTL::LowerDFGToLLVM::switchStringSlow): This is the bug.
+        * jit/BinarySwitch.cpp:
+        (JSC::BinarySwitch::BinarySwitch): Assertion to prevent similar bugs.
+        * jit/Repatch.cpp:
+        (JSC::linkPolymorphicCall): Assertion to prevent similar bugs.
+        * tests/stress/ftl-switch-string-slow-duplicate-cases.js: Added. This tests the FTL SwitchString bug. It was previously crashing every time.
+        (foo):
+        (cat):
+
</ins><span class="cx"> 2015-05-05  Basile Clement  &lt;basile_clement@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Fix debug builds after r183812
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorebytecodeCallVariantcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/bytecode/CallVariant.cpp (183824 => 183825)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/bytecode/CallVariant.cpp        2015-05-05 20:14:07 UTC (rev 183824)
+++ trunk/Source/JavaScriptCore/bytecode/CallVariant.cpp        2015-05-05 20:42:44 UTC (rev 183825)
</span><span class="lines">@@ -27,6 +27,7 @@
</span><span class="cx"> #include &quot;CallVariant.h&quot;
</span><span class="cx"> 
</span><span class="cx"> #include &quot;JSCInlines.h&quot;
</span><ins>+#include &lt;wtf/ListDump.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="cx"> 
</span><span class="lines">@@ -68,6 +69,19 @@
</span><span class="cx">     }
</span><span class="cx">     if (!!variantToAdd)
</span><span class="cx">         result.append(variantToAdd);
</span><ins>+    
+    if (!ASSERT_DISABLED) {
+        for (unsigned i = 0; i &lt; result.size(); ++i) {
+            for (unsigned j = i + 1; j &lt; result.size(); ++j) {
+                if (result[i] != result[j])
+                    continue;
+                
+                dataLog(&quot;variantListWithVariant(&quot;, listDump(list), &quot;, &quot;, variantToAdd, &quot;) failed: got duplicates in result: &quot;, listDump(result), &quot;\n&quot;);
+                RELEASE_ASSERT_NOT_REACHED();
+            }
+        }
+    }
+    
</ins><span class="cx">     return result;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLLowerDFGToLLVMcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp (183824 => 183825)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp        2015-05-05 20:14:07 UTC (rev 183824)
+++ trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp        2015-05-05 20:42:44 UTC (rev 183825)
</span><span class="lines">@@ -52,6 +52,7 @@
</span><span class="cx"> #include &lt;atomic&gt;
</span><span class="cx"> #include &lt;dlfcn.h&gt;
</span><span class="cx"> #include &lt;llvm/InitializeLLVM.h&gt;
</span><ins>+#include &lt;unordered_set&gt;
</ins><span class="cx"> #include &lt;wtf/ProcessID.h&gt;
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(FTL_NATIVE_CALL_INLINING)
</span><span class="lines">@@ -6476,6 +6477,8 @@
</span><span class="cx">         
</span><span class="cx">         Vector&lt;SwitchCase&gt; switchCases;
</span><span class="cx">         for (unsigned i = 0; i &lt; characterCases.size(); ++i) {
</span><ins>+            if (i)
+                DFG_ASSERT(m_graph, m_node, characterCases[i - 1].character &lt; characterCases[i].character);
</ins><span class="cx">             switchCases.append(SwitchCase(
</span><span class="cx">                 m_out.constInt8(characterCases[i].character), characterBlocks[i], Weight()));
</span><span class="cx">         }
</span><span class="lines">@@ -6506,12 +6509,47 @@
</span><span class="cx">         StringJumpTable&amp; table = codeBlock()-&gt;stringSwitchJumpTable(data-&gt;switchTableIndex);
</span><span class="cx">         
</span><span class="cx">         Vector&lt;SwitchCase&gt; cases;
</span><ins>+        std::unordered_set&lt;int32_t&gt; alreadyHandled; // These may be negative, or zero, or probably other stuff, too. We don't want to mess with HashSet's corner cases and we don't really care about throughput here.
</ins><span class="cx">         for (unsigned i = 0; i &lt; data-&gt;cases.size(); ++i) {
</span><ins>+            // FIXME: The fact that we're using the bytecode's switch table means that the
+            // following DFG IR transformation would be invalid.
+            //
+            // Original code:
+            //     switch (v) {
+            //     case &quot;foo&quot;:
+            //     case &quot;bar&quot;:
+            //         things();
+            //         break;
+            //     default:
+            //         break;
+            //     }
+            //
+            // New code:
+            //     switch (v) {
+            //     case &quot;foo&quot;:
+            //         instrumentFoo();
+            //         goto _things;
+            //     case &quot;bar&quot;:
+            //         instrumentBar();
+            //     _things:
+            //         things();
+            //         break;
+            //     default:
+            //         break;
+            //     }
+            //
+            // Luckily, we don't currently do any such transformation. But it's kind of silly that
+            // this is an issue.
+            // https://bugs.webkit.org/show_bug.cgi?id=144635
+            
</ins><span class="cx">             DFG::SwitchCase myCase = data-&gt;cases[i];
</span><span class="cx">             StringJumpTable::StringOffsetTable::iterator iter =
</span><span class="cx">                 table.offsetTable.find(myCase.value.stringImpl());
</span><span class="cx">             DFG_ASSERT(m_graph, m_node, iter != table.offsetTable.end());
</span><span class="cx">             
</span><ins>+            if (!alreadyHandled.insert(iter-&gt;value.branchOffset).second)
+                continue;
+
</ins><span class="cx">             cases.append(SwitchCase(
</span><span class="cx">                 m_out.constInt32(iter-&gt;value.branchOffset),
</span><span class="cx">                 lowBlock(myCase.target.block), Weight(myCase.target.count)));
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitBinarySwitchcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/BinarySwitch.cpp (183824 => 183825)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/BinarySwitch.cpp        2015-05-05 20:14:07 UTC (rev 183824)
+++ trunk/Source/JavaScriptCore/jit/BinarySwitch.cpp        2015-05-05 20:42:44 UTC (rev 183825)
</span><span class="lines">@@ -46,7 +46,12 @@
</span><span class="cx">     
</span><span class="cx">     for (unsigned i = 0; i &lt; cases.size(); ++i)
</span><span class="cx">         m_cases.append(Case(cases[i], i));
</span><ins>+    
</ins><span class="cx">     std::sort(m_cases.begin(), m_cases.end());
</span><ins>+    
+    for (unsigned i = 1; i &lt; m_cases.size(); ++i)
+        RELEASE_ASSERT(m_cases[i - 1] &lt; m_cases[i]);
+    
</ins><span class="cx">     build(0, false, m_cases.size());
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitRepatchcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/Repatch.cpp (183824 => 183825)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/Repatch.cpp        2015-05-05 20:14:07 UTC (rev 183824)
+++ trunk/Source/JavaScriptCore/jit/Repatch.cpp        2015-05-05 20:42:44 UTC (rev 183825)
</span><span class="lines">@@ -49,6 +49,7 @@
</span><span class="cx"> #include &quot;StructureRareDataInlines.h&quot;
</span><span class="cx"> #include &quot;StructureStubClearingWatchpoint.h&quot;
</span><span class="cx"> #include &quot;ThunkGenerators.h&quot;
</span><ins>+#include &lt;wtf/CommaPrinter.h&gt;
</ins><span class="cx"> #include &lt;wtf/ListDump.h&gt;
</span><span class="cx"> #include &lt;wtf/StringPrintStream.h&gt;
</span><span class="cx"> 
</span><span class="lines">@@ -1828,15 +1829,35 @@
</span><span class="cx">     if (callerCodeBlock-&gt;jitType() != JITCode::topTierJIT())
</span><span class="cx">         fastCounts = std::make_unique&lt;uint32_t[]&gt;(callCases.size());
</span><span class="cx">     
</span><del>-    for (size_t i = callCases.size(); i--;) {
</del><ins>+    for (size_t i = 0; i &lt; callCases.size(); ++i) {
</ins><span class="cx">         if (fastCounts)
</span><span class="cx">             fastCounts[i] = 0;
</span><span class="cx">         
</span><span class="cx">         CallVariant variant = callCases[i].variant();
</span><ins>+        int64_t newCaseValue;
</ins><span class="cx">         if (isClosureCall)
</span><del>-            caseValues[i] = bitwise_cast&lt;intptr_t&gt;(variant.executable());
</del><ins>+            newCaseValue = bitwise_cast&lt;intptr_t&gt;(variant.executable());
</ins><span class="cx">         else
</span><del>-            caseValues[i] = bitwise_cast&lt;intptr_t&gt;(variant.function());
</del><ins>+            newCaseValue = bitwise_cast&lt;intptr_t&gt;(variant.function());
+        
+        if (!ASSERT_DISABLED) {
+            for (size_t j = 0; j &lt; i; ++j) {
+                if (caseValues[j] != newCaseValue)
+                    continue;
+
+                dataLog(&quot;ERROR: Attempt to add duplicate case value.\n&quot;);
+                dataLog(&quot;Existing case values: &quot;);
+                CommaPrinter comma;
+                for (size_t k = 0; k &lt; i; ++k)
+                    dataLog(comma, caseValues[k]);
+                dataLog(&quot;\n&quot;);
+                dataLog(&quot;Attempting to add: &quot;, newCaseValue, &quot;\n&quot;);
+                dataLog(&quot;Variant list: &quot;, listDump(callCases), &quot;\n&quot;);
+                RELEASE_ASSERT_NOT_REACHED();
+            }
+        }
+        
+        caseValues[i] = newCaseValue;
</ins><span class="cx">     }
</span><span class="cx">     
</span><span class="cx">     GPRReg fastCountsBaseGPR =
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressftlswitchstringslowduplicatecasesjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/ftl-switch-string-slow-duplicate-cases.js (0 => 183825)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/ftl-switch-string-slow-duplicate-cases.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/ftl-switch-string-slow-duplicate-cases.js        2015-05-05 20:42:44 UTC (rev 183825)
</span><span class="lines">@@ -0,0 +1,31 @@
</span><ins>+function foo(s) {
+    switch (s) {
+    case &quot;ƑẦǏŁ&quot;:
+    case &quot;ÌŅ&quot;:
+    case &quot;ṤĻŐⱲ&quot;:
+    case &quot;ṔÄȚĦ&quot;:
+        return 42;
+    case &quot;due&quot;:
+    case &quot;to&quot;:
+    case &quot;16-bit&quot;:
+    case &quot;strings&quot;:
+        return 43;
+    default:
+        return 44;
+    }
+}
+
+noInline(foo);
+
+function cat(a, b) {
+    return a + b;
+}
+
+for (var i = 0; i &lt; 10000; ++i) {
+    var result = foo(cat(&quot;16-&quot;, &quot;bit&quot;));
+    if (result != 43)
+        throw &quot;Error: bad result (1): &quot; + result;
+    result = foo(&quot;ƑẦǏŁ&quot;);
+    if (result != 42)
+        throw &quot;Error: bad result (2): &quot; + result;
+}
</ins></span></pre>
</div>
</div>

</body>
</html>