Skip to content

Commit e6f2e2c

Browse files
committed
[JSC] DFG strength reduction should not crash when a folded string is too long
https://bugs.webkit.org/show_bug.cgi?id=328022 Reviewed by Yusuke Suzuki. DFG strength reduction folds MakeRope, StrCat, MakeAtomString and StringReplace of constant strings with a StringBuilder, and gives up when hasOverflowed() is true. But the builders used the default policy, which crashes on overflow, so the compiler thread aborted where the interpreter throws a RangeError. This patch uses OverflowPolicy::RecordOverflow for them, and adds the missing check to StringReplace before it commits to the fold. Tests: JSTests/stress/dfg-strength-reduction-make-rope-overflow-three-strings.js JSTests/stress/dfg-strength-reduction-make-rope-overflow.js JSTests/stress/dfg-strength-reduction-string-replace-overflow.js * JSTests/stress/dfg-strength-reduction-make-rope-overflow-three-strings.js: Added. (shouldThrowRangeError): (test): * JSTests/stress/dfg-strength-reduction-make-rope-overflow.js: Added. (shouldThrowRangeError): (test): * JSTests/stress/dfg-strength-reduction-string-replace-overflow.js: Added. (shouldThrowRangeError): (test): * Source/JavaScriptCore/dfg/DFGStrengthReductionPhase.cpp: (JSC::DFG::StrengthReductionPhase::handleNode): Canonical link: https://commits.webkit.org/324184@main
1 parent b5a0eff commit e6f2e2c

4 files changed

Lines changed: 111 additions & 16 deletions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ memoryHog!
2+
//@ skip if $addressBits <= 32
3+
//@ runDefault("--useConcurrentJIT=0")
4+
5+
function shouldThrowRangeError(func, ...args) {
6+
let error;
7+
try {
8+
func(...args);
9+
} catch (e) {
10+
error = e;
11+
}
12+
if (!(error instanceof RangeError))
13+
throw new Error("bad error: " + error);
14+
}
15+
16+
function test(kind, x) {
17+
let s = "ab";
18+
if (!kind)
19+
return s;
20+
s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s;
21+
s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s;
22+
s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s;
23+
switch (kind) {
24+
case 1:
25+
return `${s}${s}${s}`.length;
26+
case 2:
27+
return `${s}${s}${x}`.length;
28+
default:
29+
return `${x}${s}${s}`.length;
30+
}
31+
}
32+
noInline(test);
33+
34+
for (let i = 0; i < testLoopCount; ++i) {
35+
shouldThrowRangeError(test, 1, "x");
36+
shouldThrowRangeError(test, 2, "x");
37+
shouldThrowRangeError(test, 3, "x");
38+
test(0, "x");
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ memoryHog!
2+
//@ skip if $addressBits <= 32
3+
//@ runDefault("--useConcurrentJIT=0")
4+
5+
function shouldThrowRangeError(func) {
6+
let error;
7+
try {
8+
func();
9+
} catch (e) {
10+
error = e;
11+
}
12+
if (!(error instanceof RangeError))
13+
throw new Error("bad error: " + error);
14+
}
15+
16+
function test() {
17+
let s = "ab";
18+
s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s;
19+
s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s;
20+
s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s; s += s;
21+
return s.length;
22+
}
23+
noInline(test);
24+
25+
for (let i = 0; i < testLoopCount; ++i)
26+
shouldThrowRangeError(test);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ memoryHog!
2+
//@ skip if $addressBits <= 32
3+
//@ runDefault("--useConcurrentJIT=0")
4+
5+
function shouldThrowRangeError(func) {
6+
let error;
7+
try {
8+
func();
9+
} catch (e) {
10+
error = e;
11+
}
12+
if (!(error instanceof RangeError))
13+
throw new Error("bad error: " + error);
14+
}
15+
16+
function test() {
17+
let a = "a";
18+
a = a + a + "a"; a = a + a + "a"; a = a + a + "a"; a = a + a + "a"; a = a + a + "a"; a = a + a + "a"; a = a + a + "a";
19+
a = a + a + "a"; a = a + a + "a";
20+
let b = "bb";
21+
b += b; b += b; b += b; b += b; b += b; b += b; b += b; b += b; b += b; b += b;
22+
b += b; b += b; b += b; b += b; b += b; b += b; b += b; b += b; b += b; b += b;
23+
return (a + b).replace(/a/g, b).length;
24+
}
25+
noInline(test);
26+
27+
for (let i = 0; i < testLoopCount && !numberOfDFGCompiles(test); ++i)
28+
shouldThrowRangeError(test);

‎Source/JavaScriptCore/dfg/DFGStrengthReductionPhase.cpp‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,10 @@ class StrengthReductionPhase : public Phase {
651651
if (!string2)
652652
break;
653653

654-
StringBuilder builder;
654+
StringBuilder builder(OverflowPolicy::RecordOverflow);
655655
builder.append(string1);
656656
builder.append(string2);
657-
if (!builder.hasOverflowed()) {
657+
if (!builder.hasOverflowed()) [[likely]] {
658658
LazyJSValue value = LazyJSValue::newString(m_graph, builder.toString());
659659
auto* constant = m_insertionSet.insertNode(m_nodeIndex, SpecNone, LazyJSConstant, m_node->origin, OpInfo(m_graph.m_lazyJSValues.add(value)));
660660
m_node->child2().setNode(constant);
@@ -676,11 +676,11 @@ class StrengthReductionPhase : public Phase {
676676
if (!string1)
677677
break;
678678

679-
StringBuilder builder;
679+
StringBuilder builder(OverflowPolicy::RecordOverflow);
680680
builder.append(string0);
681681
builder.append(string1);
682682
if (!m_node->child3()) {
683-
if (!builder.hasOverflowed()) {
683+
if (!builder.hasOverflowed()) [[likely]] {
684684
convertToLazyJSValue(m_node, LazyJSValue::newString(m_graph, builder.toString()));
685685
m_changed = true;
686686
}
@@ -689,7 +689,7 @@ class StrengthReductionPhase : public Phase {
689689

690690
String string2 = m_node->child3()->tryGetString(m_graph);
691691
if (!string2) {
692-
if (!builder.hasOverflowed()) {
692+
if (!builder.hasOverflowed()) [[likely]] {
693693
LazyJSValue value = LazyJSValue::newString(m_graph, builder.toString());
694694
auto* constant = m_insertionSet.insertNode(m_nodeIndex, SpecNone, LazyJSConstant, m_node->origin, OpInfo(m_graph.m_lazyJSValues.add(value)));
695695
m_node->child1().setNode(constant);
@@ -701,7 +701,7 @@ class StrengthReductionPhase : public Phase {
701701
}
702702

703703
builder.append(string2);
704-
if (!builder.hasOverflowed()) {
704+
if (!builder.hasOverflowed()) [[likely]] {
705705
convertToLazyJSValue(m_node, LazyJSValue::newString(m_graph, builder.toString()));
706706
m_changed = true;
707707
}
@@ -1382,7 +1382,7 @@ class StrengthReductionPhase : public Phase {
13821382
break;
13831383
}
13841384

1385-
StringBuilder builder;
1385+
StringBuilder builder(OverflowPolicy::RecordOverflow);
13861386

13871387
unsigned lastIndex = 0;
13881388
unsigned startPosition = 0;
@@ -1413,11 +1413,8 @@ class StrengthReductionPhase : public Phase {
14131413
unsigned replLen = replace.length();
14141414
if (lastIndex < result.start || replLen) {
14151415
builder.appendSubstring(string, lastIndex, result.start - lastIndex);
1416-
if (replLen) {
1417-
StringBuilder replacement;
1418-
substituteBackreferences(replacement, replace, string, ovector.span().data(), regExp);
1419-
builder.append(replacement);
1420-
}
1416+
if (replLen)
1417+
substituteBackreferences(builder, replace, string, ovector.span().data(), regExp);
14211418
}
14221419

14231420
lastIndex = result.end;
@@ -1438,6 +1435,13 @@ class StrengthReductionPhase : public Phase {
14381435
if (!ok)
14391436
break;
14401437

1438+
bool isUnchanged = !lastIndex && builder.isEmpty();
1439+
if (!isUnchanged) {
1440+
builder.appendSubstring(string, lastIndex);
1441+
if (builder.hasOverflowed()) [[unlikely]]
1442+
break;
1443+
}
1444+
14411445
// We are committed at this point.
14421446
m_changed = true;
14431447

@@ -1457,12 +1461,10 @@ class StrengthReductionPhase : public Phase {
14571461
origin = origin.withInvalidExit();
14581462
}
14591463

1460-
if (!lastIndex && builder.isEmpty())
1464+
if (isUnchanged)
14611465
m_node->convertToIdentityOn(stringNode);
1462-
else {
1463-
builder.appendSubstring(string, lastIndex);
1466+
else
14641467
m_node->convertToLazyJSConstant(m_graph, LazyJSValue::newString(m_graph, builder.toString()));
1465-
}
14661468

14671469
m_node->origin = origin;
14681470
break;

0 commit comments

Comments
 (0)