Diff
Modified: trunk/Source/_javascript_Core/ChangeLog (183824 => 183825)
--- trunk/Source/_javascript_Core/ChangeLog 2015-05-05 20:14:07 UTC (rev 183824)
+++ trunk/Source/_javascript_Core/ChangeLog 2015-05-05 20:42:44 UTC (rev 183825)
@@ -1,3 +1,26 @@
+2015-05-05 Filip Pizlo <[email protected]>
+
+ 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 <[email protected]>
Fix debug builds after r183812
Modified: trunk/Source/_javascript_Core/bytecode/CallVariant.cpp (183824 => 183825)
--- trunk/Source/_javascript_Core/bytecode/CallVariant.cpp 2015-05-05 20:14:07 UTC (rev 183824)
+++ trunk/Source/_javascript_Core/bytecode/CallVariant.cpp 2015-05-05 20:42:44 UTC (rev 183825)
@@ -27,6 +27,7 @@
#include "CallVariant.h"
#include "JSCInlines.h"
+#include <wtf/ListDump.h>
namespace JSC {
@@ -68,6 +69,19 @@
}
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;
}
Modified: trunk/Source/_javascript_Core/ftl/FTLLowerDFGToLLVM.cpp (183824 => 183825)
--- trunk/Source/_javascript_Core/ftl/FTLLowerDFGToLLVM.cpp 2015-05-05 20:14:07 UTC (rev 183824)
+++ trunk/Source/_javascript_Core/ftl/FTLLowerDFGToLLVM.cpp 2015-05-05 20:42:44 UTC (rev 183825)
@@ -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 @@
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 @@
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)));
Modified: trunk/Source/_javascript_Core/jit/BinarySwitch.cpp (183824 => 183825)
--- trunk/Source/_javascript_Core/jit/BinarySwitch.cpp 2015-05-05 20:14:07 UTC (rev 183824)
+++ trunk/Source/_javascript_Core/jit/BinarySwitch.cpp 2015-05-05 20:42:44 UTC (rev 183825)
@@ -46,7 +46,12 @@
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());
}
Modified: trunk/Source/_javascript_Core/jit/Repatch.cpp (183824 => 183825)
--- trunk/Source/_javascript_Core/jit/Repatch.cpp 2015-05-05 20:14:07 UTC (rev 183824)
+++ trunk/Source/_javascript_Core/jit/Repatch.cpp 2015-05-05 20:42:44 UTC (rev 183825)
@@ -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 @@
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 =
Added: trunk/Source/_javascript_Core/tests/stress/ftl-switch-string-slow-duplicate-cases.js (0 => 183825)
--- trunk/Source/_javascript_Core/tests/stress/ftl-switch-string-slow-duplicate-cases.js (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/ftl-switch-string-slow-duplicate-cases.js 2015-05-05 20:42:44 UTC (rev 183825)
@@ -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;
+}