Skip to content

Commit 7b1cfc5

Browse files
committed
[JSC] Overwritten length of sloppy arguments objects should be converted with ToLength
https://bugs.webkit.org/show_bug.cgi?id=328288 Reviewed by Yusuke Suzuki. DirectArguments::length() and ScopedArguments::length() convert an overwritten `length` with toUInt32, so -1 becomes 4294967295 and the following loops 2^32 times: (function () { arguments.length = -1; return Array.prototype.slice.call(arguments); })(1, 2, 3); A length of 2^32 or more is truncated in the same way. This patch uses toLength, as ClonedArguments::length() already does, and clamps the result in sizeOfVarargs(). Test: JSTests/stress/sloppy-arguments-overwritten-length-to-length.js * JSTests/stress/sloppy-arguments-overwritten-length-to-length.js: Added. (shouldBe): (scoped): (count): (apply): (reflectApply): (testNegative): (testLarge): * Source/JavaScriptCore/interpreter/Interpreter.cpp: (JSC::sizeOfVarargs): * Source/JavaScriptCore/runtime/DirectArguments.cpp: (JSC::DirectArguments::length const): * Source/JavaScriptCore/runtime/DirectArguments.h: * Source/JavaScriptCore/runtime/ScopedArguments.cpp: (JSC::ScopedArguments::length const): * Source/JavaScriptCore/runtime/ScopedArguments.h: Canonical link: https://commits.webkit.org/324407@main
1 parent 1af9d48 commit 7b1cfc5

6 files changed

Lines changed: 132 additions & 8 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
function shouldBe(actual, expected)
2+
{
3+
if (actual !== expected)
4+
throw new Error(`bad value: ${String(actual)}, expected: ${String(expected)}`);
5+
}
6+
7+
function shouldThrow(func, errorConstructor)
8+
{
9+
let error;
10+
try {
11+
func();
12+
} catch (e) {
13+
error = e;
14+
}
15+
if (!(error instanceof errorConstructor))
16+
throw new Error(`bad error: ${String(error)}`);
17+
}
18+
19+
function direct(length)
20+
{
21+
if (length !== undefined)
22+
arguments.length = length;
23+
return arguments;
24+
}
25+
26+
function scoped(length)
27+
{
28+
(function () { return length; })();
29+
if (length !== undefined)
30+
arguments.length = length;
31+
return arguments;
32+
}
33+
34+
function count()
35+
{
36+
return arguments.length;
37+
}
38+
39+
function apply(args)
40+
{
41+
return count.apply(null, args);
42+
}
43+
noInline(apply);
44+
45+
function reflectApply(args)
46+
{
47+
return Reflect.apply(count, null, args);
48+
}
49+
noInline(reflectApply);
50+
51+
function testNegative(create, length)
52+
{
53+
let pushed = create(length, 2, 3);
54+
shouldBe(Array.prototype.push.call(pushed, 4), 1);
55+
shouldBe(pushed[0], 4);
56+
shouldBe(pushed.length, 1);
57+
58+
let popped = create(length, 2, 3);
59+
shouldBe(Array.prototype.pop.call(popped), undefined);
60+
shouldBe(popped.length, 0);
61+
62+
let args = create(length, 2, 3);
63+
shouldBe(Array.prototype.slice.call(args).length, 0);
64+
shouldBe(Array.prototype.slice.call(args, 0, 2).length, 0);
65+
shouldBe(Array.prototype.indexOf.call(args, 2), -1);
66+
shouldBe(Array.prototype.indexOf.call(args, 4), -1);
67+
shouldBe(Array.prototype.lastIndexOf.call(args, 2), -1);
68+
shouldBe(Array.prototype.includes.call(args, 3), false);
69+
shouldBe(Array.prototype.join.call(args), "");
70+
shouldBe(Array.prototype.reverse.call(args), args);
71+
shouldBe(Array.prototype.fill.call(args, 0), args);
72+
shouldBe(Array.prototype.toReversed.call(args).length, 0);
73+
shouldBe(apply(args), 0);
74+
shouldBe(reflectApply(args), 0);
75+
shouldBe(args[1], 2);
76+
shouldBe(args[2], 3);
77+
shouldBe(args.length, length);
78+
}
79+
80+
function testLarge(create, length, expected)
81+
{
82+
let args = create(length, 2, 3);
83+
shouldBe(Array.prototype.indexOf.call(args, 3), 2);
84+
shouldBe(Array.prototype.includes.call(args, undefined), true);
85+
shouldBe(Array.prototype.slice.call(args, 1, 3).join(), "2,3");
86+
shouldThrow(() => Array.prototype.slice.call(args), RangeError);
87+
shouldThrow(() => apply(args), RangeError);
88+
shouldThrow(() => reflectApply(args), RangeError);
89+
shouldBe(args.length, length);
90+
91+
shouldBe(Array.prototype.pop.call(args), undefined);
92+
shouldBe(args.length, expected - 1);
93+
shouldBe(Array.prototype.push.call(args, 4), expected);
94+
shouldBe(args[expected - 1], 4);
95+
shouldBe(args.length, expected);
96+
}
97+
98+
for (let i = 0; i < testLoopCount; ++i) {
99+
shouldBe(apply(direct(undefined, 2, 3)), 3);
100+
shouldBe(apply(scoped(undefined, 2, 3)), 3);
101+
shouldBe(reflectApply(direct(undefined, 2, 3)), 3);
102+
shouldBe(reflectApply(scoped(undefined, 2, 3)), 3);
103+
}
104+
105+
for (let i = 0; i < 10; ++i) {
106+
for (let create of [direct, scoped]) {
107+
testNegative(create, -1);
108+
testNegative(create, -1.5);
109+
testNegative(create, -(2 ** 31));
110+
testNegative(create, -(2 ** 32) + 2);
111+
testNegative(create, -(2 ** 32) - 1);
112+
testNegative(create, -Infinity);
113+
testNegative(create, "-1");
114+
115+
testLarge(create, 2 ** 32, 2 ** 32);
116+
testLarge(create, 2 ** 32 + 1, 2 ** 32 + 1);
117+
testLarge(create, 2 ** 32 + 2.5, 2 ** 32 + 2);
118+
testLarge(create, 2 ** 53 - 1, 2 ** 53 - 1);
119+
testLarge(create, 2 ** 53 + 2, 2 ** 53 - 1);
120+
testLarge(create, Infinity, 2 ** 53 - 1);
121+
122+
shouldThrow(() => Array.prototype.push.call(create(Infinity, 2, 3), 4), TypeError);
123+
}
124+
}

‎Source/JavaScriptCore/interpreter/Interpreter.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ unsigned sizeOfVarargs(JSGlobalObject* globalObject, JSValue arguments, uint32_t
246246
unsigned length;
247247
switch (cell->type()) {
248248
case DirectArgumentsType:
249-
length = uncheckedDowncast<DirectArguments>(cell)->length(globalObject);
249+
length = clampToUnsigned(uncheckedDowncast<DirectArguments>(cell)->length(globalObject));
250250
break;
251251
case ScopedArgumentsType:
252-
length = uncheckedDowncast<ScopedArguments>(cell)->length(globalObject);
252+
length = clampToUnsigned(uncheckedDowncast<ScopedArguments>(cell)->length(globalObject));
253253
break;
254254
case ClonedArgumentsType:
255255
length = clampToUnsigned(uncheckedDowncast<ClonedArguments>(cell)->length(globalObject));

‎Source/JavaScriptCore/runtime/DirectArguments.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(DirectArguments);
3737

3838
const ClassInfo DirectArguments::s_info = { "Arguments"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(DirectArguments) };
3939

40-
uint32_t DirectArguments::length(JSGlobalObject* globalObject) const
40+
uint64_t DirectArguments::length(JSGlobalObject* globalObject) const
4141
{
4242
VM& vm = getVM(globalObject);
4343
auto scope = DECLARE_THROW_SCOPE(vm);
4444
if (m_mappedArguments) [[unlikely]] {
4545
JSValue value = get(globalObject, vm.propertyNames->length);
4646
RETURN_IF_EXCEPTION(scope, { });
47-
RELEASE_AND_RETURN(scope, value.toUInt32(globalObject));
47+
RELEASE_AND_RETURN(scope, value.toLength(globalObject));
4848
}
4949
return m_length;
5050
}

‎Source/JavaScriptCore/runtime/DirectArguments.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class DirectArguments final : public GenericArgumentsImpl<DirectArguments> {
7575
return m_length;
7676
}
7777

78-
uint32_t length(JSGlobalObject*) const;
78+
uint64_t length(JSGlobalObject*) const;
7979

8080
bool isMappedArgument(uint32_t i) const
8181
{

‎Source/JavaScriptCore/runtime/ScopedArguments.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ScopedArguments);
3838

3939
const ClassInfo ScopedArguments::s_info = { "Arguments"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(ScopedArguments) };
4040

41-
uint32_t ScopedArguments::length(JSGlobalObject* globalObject) const
41+
uint64_t ScopedArguments::length(JSGlobalObject* globalObject) const
4242
{
4343
VM& vm = getVM(globalObject);
4444
auto scope = DECLARE_THROW_SCOPE(vm);
4545
if (m_overrodeThings) [[unlikely]] {
4646
auto value = get(globalObject, vm.propertyNames->length);
4747
RETURN_IF_EXCEPTION(scope, 0);
48-
RELEASE_AND_RETURN(scope, value.toUInt32(globalObject));
48+
RELEASE_AND_RETURN(scope, value.toLength(globalObject));
4949
}
5050
return internalLength();
5151
}

‎Source/JavaScriptCore/runtime/ScopedArguments.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class ScopedArguments final : public GenericArgumentsImpl<ScopedArguments> {
7474
return m_totalLength;
7575
}
7676

77-
uint32_t length(JSGlobalObject*) const;
77+
uint64_t length(JSGlobalObject*) const;
7878

7979
bool isMappedArgument(uint32_t i) const
8080
{

0 commit comments

Comments
 (0)