FTL SwitchString slow case creates duplicate switch cases
authorfpizlo@apple.com <fpizlo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 May 2015 20:42:44 +0000 (20:42 +0000)
committerfpizlo@apple.com <fpizlo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 May 2015 20:42:44 +0000 (20:42 +0000)
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):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@183825 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/bytecode/CallVariant.cpp
Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp
Source/JavaScriptCore/jit/BinarySwitch.cpp
Source/JavaScriptCore/jit/Repatch.cpp
Source/JavaScriptCore/tests/stress/ftl-switch-string-slow-duplicate-cases.js [new file with mode: 0644]

index 747befe..499d7e7 100644 (file)
@@ -1,3 +1,26 @@
+2015-05-05  Filip Pizlo  <fpizlo@apple.com>
+
+        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):
+
 2015-05-05  Basile Clement  <basile_clement@apple.com>
 
         Fix debug builds after r183812
index d7b736a..9745dde 100644 (file)
@@ -27,6 +27,7 @@
 #include "CallVariant.h"
 
 #include "JSCInlines.h"
+#include <wtf/ListDump.h>
 
 namespace JSC {
 
@@ -68,6 +69,19 @@ CallVariantList variantListWithVariant(const CallVariantList& list, CallVariant
     }
     if (!!variantToAdd)
         result.append(variantToAdd);
+    
+    if (!ASSERT_DISABLED) {
+        for (unsigned i = 0; i < result.size(); ++i) {
+            for (unsigned j = i + 1; j < result.size(); ++j) {
+                if (result[i] != result[j])
+                    continue;
+                
+                dataLog("variantListWithVariant(", listDump(list), ", ", variantToAdd, ") failed: got duplicates in result: ", listDump(result), "\n");
+                RELEASE_ASSERT_NOT_REACHED();
+            }
+        }
+    }
+    
     return result;
 }
 
index e0d82ba..9150a46 100644 (file)
@@ -52,6 +52,7 @@
 #include <atomic>
 #include <dlfcn.h>
 #include <llvm/InitializeLLVM.h>
+#include <unordered_set>
 #include <wtf/ProcessID.h>
 
 #if ENABLE(FTL_NATIVE_CALL_INLINING)
@@ -6476,6 +6477,8 @@ private:
         
         Vector<SwitchCase> switchCases;
         for (unsigned i = 0; i < characterCases.size(); ++i) {
+            if (i)
+                DFG_ASSERT(m_graph, m_node, characterCases[i - 1].character < characterCases[i].character);
             switchCases.append(SwitchCase(
                 m_out.constInt8(characterCases[i].character), characterBlocks[i], Weight()));
         }
@@ -6506,12 +6509,47 @@ private:
         StringJumpTable& table = codeBlock()->stringSwitchJumpTable(data->switchTableIndex);
         
         Vector<SwitchCase> cases;
+        std::unordered_set<int32_t> 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.
         for (unsigned i = 0; i < data->cases.size(); ++i) {
+            // 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 "foo":
+            //     case "bar":
+            //         things();
+            //         break;
+            //     default:
+            //         break;
+            //     }
+            //
+            // New code:
+            //     switch (v) {
+            //     case "foo":
+            //         instrumentFoo();
+            //         goto _things;
+            //     case "bar":
+            //         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
+            
             DFG::SwitchCase myCase = data->cases[i];
             StringJumpTable::StringOffsetTable::iterator iter =
                 table.offsetTable.find(myCase.value.stringImpl());
             DFG_ASSERT(m_graph, m_node, iter != table.offsetTable.end());
             
+            if (!alreadyHandled.insert(iter->value.branchOffset).second)
+                continue;
+
             cases.append(SwitchCase(
                 m_out.constInt32(iter->value.branchOffset),
                 lowBlock(myCase.target.block), Weight(myCase.target.count)));
index a24d72c..866b278 100644 (file)
@@ -46,7 +46,12 @@ BinarySwitch::BinarySwitch(GPRReg value, const Vector<int64_t>& cases, Type type
     
     for (unsigned i = 0; i < cases.size(); ++i)
         m_cases.append(Case(cases[i], i));
+    
     std::sort(m_cases.begin(), m_cases.end());
+    
+    for (unsigned i = 1; i < m_cases.size(); ++i)
+        RELEASE_ASSERT(m_cases[i - 1] < m_cases[i]);
+    
     build(0, false, m_cases.size());
 }
 
index ee72550..970df75 100644 (file)
@@ -49,6 +49,7 @@
 #include "StructureRareDataInlines.h"
 #include "StructureStubClearingWatchpoint.h"
 #include "ThunkGenerators.h"
+#include <wtf/CommaPrinter.h>
 #include <wtf/ListDump.h>
 #include <wtf/StringPrintStream.h>
 
@@ -1828,15 +1829,35 @@ void linkPolymorphicCall(
     if (callerCodeBlock->jitType() != JITCode::topTierJIT())
         fastCounts = std::make_unique<uint32_t[]>(callCases.size());
     
-    for (size_t i = callCases.size(); i--;) {
+    for (size_t i = 0; i < callCases.size(); ++i) {
         if (fastCounts)
             fastCounts[i] = 0;
         
         CallVariant variant = callCases[i].variant();
+        int64_t newCaseValue;
         if (isClosureCall)
-            caseValues[i] = bitwise_cast<intptr_t>(variant.executable());
+            newCaseValue = bitwise_cast<intptr_t>(variant.executable());
         else
-            caseValues[i] = bitwise_cast<intptr_t>(variant.function());
+            newCaseValue = bitwise_cast<intptr_t>(variant.function());
+        
+        if (!ASSERT_DISABLED) {
+            for (size_t j = 0; j < i; ++j) {
+                if (caseValues[j] != newCaseValue)
+                    continue;
+
+                dataLog("ERROR: Attempt to add duplicate case value.\n");
+                dataLog("Existing case values: ");
+                CommaPrinter comma;
+                for (size_t k = 0; k < i; ++k)
+                    dataLog(comma, caseValues[k]);
+                dataLog("\n");
+                dataLog("Attempting to add: ", newCaseValue, "\n");
+                dataLog("Variant list: ", listDump(callCases), "\n");
+                RELEASE_ASSERT_NOT_REACHED();
+            }
+        }
+        
+        caseValues[i] = newCaseValue;
     }
     
     GPRReg fastCountsBaseGPR =
diff --git a/Source/JavaScriptCore/tests/stress/ftl-switch-string-slow-duplicate-cases.js b/Source/JavaScriptCore/tests/stress/ftl-switch-string-slow-duplicate-cases.js
new file mode 100644 (file)
index 0000000..ec9db4c
--- /dev/null
@@ -0,0 +1,31 @@
+function foo(s) {
+    switch (s) {
+    case "ƑẦǏŁ":
+    case "ÌŅ":
+    case "ṤĻŐⱲ":
+    case "ṔÄȚĦ":
+        return 42;
+    case "due":
+    case "to":
+    case "16-bit":
+    case "strings":
+        return 43;
+    default:
+        return 44;
+    }
+}
+
+noInline(foo);
+
+function cat(a, b) {
+    return a + b;
+}
+
+for (var i = 0; i < 10000; ++i) {
+    var result = foo(cat("16-", "bit"));
+    if (result != 43)
+        throw "Error: bad result (1): " + result;
+    result = foo("ƑẦǏŁ");
+    if (result != 42)
+        throw "Error: bad result (2): " + result;
+}