Skip to content

Commit de4efbb

Browse files
committed
[JSC] BigInt64 / BigUint64 toNativeFromValueWithoutCoercion should check content
https://bugs.webkit.org/show_bug.cgi?id=328083 rdar://190436212 Reviewed by Sosuke Suzuki. toNativeFromValueWithoutCoercion should not do coercion. Thus, when converting to BigInt64 / BigUint64, we need to ensure that BigInt is within this value range. Test: JSTests/stress/bigint-typed-array-search-compares-exact-values.js * JSTests/stress/bigint-typed-array-search-compares-exact-values.js: Added. (check): * Source/JavaScriptCore/runtime/JSBigInt.cpp: (JSC::JSBigInt::tryToBigUInt64): (JSC::JSBigInt::tryToBigInt64): * Source/JavaScriptCore/runtime/JSBigInt.h: (JSC::JSBigInt::tryAbsoluteValueAsUInt64 const): * Source/JavaScriptCore/runtime/JSBigIntInlines.h: (JSC::JSBigInt::tryExtractDouble): * Source/JavaScriptCore/runtime/ToNativeFromValue.h: (JSC::toNativeFromValueWithoutCoercion): * Source/JavaScriptCore/wasm/js/JSWebAssemblyHelpers.h: (JSC::toNonWrappingUint64): Canonical link: https://commits.webkit.org/324313@main
1 parent b40ec02 commit de4efbb

6 files changed

Lines changed: 80 additions & 28 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// includes, indexOf, and lastIndexOf on BigInt typed arrays compare exact values, so a BigInt
2+
// outside the element type's range is never found.
3+
4+
function check(actual, expected, what) {
5+
if (actual !== expected)
6+
throw new Error(what + ": expected " + expected + " but got " + actual);
7+
}
8+
9+
const unsigned = new BigUint64Array([1n, 2n ** 64n - 1n, 0n]);
10+
check(unsigned.includes(-1n), false, "BigUint64Array includes -1n");
11+
check(unsigned.indexOf(-1n), -1, "BigUint64Array indexOf -1n");
12+
check(unsigned.lastIndexOf(-1n), -1, "BigUint64Array lastIndexOf -1n");
13+
check(unsigned.indexOf(2n ** 64n), -1, "BigUint64Array indexOf 2n ** 64n");
14+
check(unsigned.indexOf(2n ** 64n - 1n), 1, "BigUint64Array indexOf its maximum");
15+
check(unsigned.indexOf(0n), 2, "BigUint64Array indexOf 0n");
16+
17+
const signed = new BigInt64Array([-1n, 5n, -(2n ** 63n), 2n ** 63n - 1n]);
18+
check(signed.includes(2n ** 64n - 1n), false, "BigInt64Array includes 2n ** 64n - 1n");
19+
check(signed.indexOf(5n + 2n ** 64n), -1, "BigInt64Array indexOf 5n + 2n ** 64n");
20+
check(signed.indexOf(2n ** 63n), -1, "BigInt64Array indexOf 2n ** 63n");
21+
check(signed.indexOf(-(2n ** 63n) - 1n), -1, "BigInt64Array indexOf below its minimum");
22+
check(signed.indexOf(-(2n ** 63n)), 2, "BigInt64Array indexOf its minimum");
23+
check(signed.indexOf(2n ** 63n - 1n), 3, "BigInt64Array indexOf its maximum");
24+
check(signed.lastIndexOf(-1n), 0, "BigInt64Array lastIndexOf -1n");

‎Source/JavaScriptCore/runtime/JSBigInt.cpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6436,6 +6436,40 @@ uint64_t JSBigInt::toBigUInt64Heap(JSBigInt* bigInt)
64366436
return ~(value - 1); // To avoid undefined behavior, we compute two's compliment by hand in C while this is simply `-value`.
64376437
}
64386438

6439+
std::optional<uint64_t> JSBigInt::tryToBigUInt64(JSValue bigInt)
6440+
{
6441+
ASSERT(bigInt.isBigInt());
6442+
#if USE(BIGINT32)
6443+
if (bigInt.isBigInt32()) {
6444+
int32_t value = bigInt.bigInt32AsInt32();
6445+
if (value < 0)
6446+
return std::nullopt;
6447+
return static_cast<uint64_t>(value);
6448+
}
6449+
#endif
6450+
JSBigInt* heapBigInt = bigInt.asHeapBigInt();
6451+
if (heapBigInt->sign())
6452+
return std::nullopt;
6453+
return heapBigInt->tryAbsoluteValueAsUInt64();
6454+
}
6455+
6456+
std::optional<int64_t> JSBigInt::tryToBigInt64(JSValue bigInt)
6457+
{
6458+
ASSERT(bigInt.isBigInt());
6459+
#if USE(BIGINT32)
6460+
if (bigInt.isBigInt32())
6461+
return static_cast<int64_t>(bigInt.bigInt32AsInt32());
6462+
#endif
6463+
JSBigInt* heapBigInt = bigInt.asHeapBigInt();
6464+
auto magnitude = heapBigInt->tryAbsoluteValueAsUInt64();
6465+
if (!magnitude)
6466+
return std::nullopt;
6467+
uint64_t maxMagnitude = static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) + (heapBigInt->sign() ? 1 : 0);
6468+
if (*magnitude > maxMagnitude)
6469+
return std::nullopt;
6470+
return static_cast<int64_t>(toBigUInt64Heap(heapBigInt));
6471+
}
6472+
64396473
static ALWAYS_INLINE unsigned LEGACY_NODELETE computeHash(JSBigInt::Digit* digits, unsigned length, bool sign)
64406474
{
64416475
Hasher hasher;

‎Source/JavaScriptCore/runtime/JSBigInt.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ class JSBigInt final : public JSCell {
452452

453453
inline static uint64_t toBigUInt64(JSValue); // Defined in JSBigIntInlines.h
454454
inline static int64_t toBigInt64(JSValue); // Defined in JSBigIntInlines.h
455+
static std::optional<uint64_t> tryToBigUInt64(JSValue);
456+
static std::optional<int64_t> tryToBigInt64(JSValue);
455457

456458
Digit digit(unsigned) const;
457459
void setDigit(unsigned, Digit); // Use only when initializing.
@@ -681,6 +683,7 @@ class JSBigInt final : public JSCell {
681683
static ImplResult truncateAndSubFromPowerOfTwo(JSGlobalObject*, unsigned, BigIntImpl, bool resultSign);
682684

683685
JS_EXPORT_PRIVATE static uint64_t LEGACY_NODELETE toBigUInt64Heap(JSBigInt*);
686+
inline std::optional<uint64_t> tryAbsoluteValueAsUInt64() const;
684687

685688
inline Digit* dataStorage() { return std::bit_cast<Digit*>(std::bit_cast<uint8_t*>(this) + offsetOfData()); }
686689
inline const Digit* dataStorage() const { return std::bit_cast<const Digit*>(std::bit_cast<const uint8_t*>(this) + offsetOfData()); }
@@ -710,6 +713,16 @@ inline void JSBigInt::setDigit(unsigned n, Digit value)
710713
dataStorage()[n] = value;
711714
}
712715

716+
inline std::optional<uint64_t> JSBigInt::tryAbsoluteValueAsUInt64() const
717+
{
718+
if (length() > 64 / digitBits)
719+
return std::nullopt;
720+
uint64_t value = 0;
721+
for (unsigned index = 0; index < length(); ++index)
722+
value |= static_cast<uint64_t>(digit(index)) << (index * digitBits);
723+
return value;
724+
}
725+
713726
ALWAYS_INLINE JSBigInt::ComparisonResult invertBigIntCompareResult(JSBigInt::ComparisonResult comparisonResult)
714727
{
715728
switch (comparisonResult) {

‎Source/JavaScriptCore/runtime/JSBigIntInlines.h‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,10 @@ ALWAYS_INLINE std::optional<double> JSBigInt::tryExtractDouble(JSValue value)
7575

7676
ASSERT(value.isHeapBigInt());
7777
JSBigInt* bigInt = value.asHeapBigInt();
78-
if (!bigInt->length())
79-
return 0;
80-
81-
uint64_t integer = 0;
82-
if constexpr (sizeof(Digit) == 8) {
83-
if (bigInt->length() != 1)
84-
return std::nullopt;
85-
integer = bigInt->digit(0);
86-
} else {
87-
ASSERT(sizeof(Digit) == 4);
88-
if (bigInt->length() > 2)
89-
return std::nullopt;
90-
integer = bigInt->digit(0);
91-
if (bigInt->length() == 2)
92-
integer |= (static_cast<uint64_t>(bigInt->digit(1)) << 32);
93-
}
94-
95-
if (integer <= maxSafeIntegerAsUInt64())
96-
return (bigInt->sign()) ? -static_cast<double>(integer) : static_cast<double>(integer);
97-
98-
return std::nullopt;
78+
auto integer = bigInt->tryAbsoluteValueAsUInt64();
79+
if (!integer || *integer > maxSafeIntegerAsUInt64())
80+
return std::nullopt;
81+
return bigInt->sign() ? -static_cast<double>(*integer) : static_cast<double>(*integer);
9982
}
10083

10184
ALWAYS_INLINE JSValue JSBigInt::makeHeapBigIntOrBigInt32(JSGlobalObject* globalObject, double value)

‎Source/JavaScriptCore/runtime/ToNativeFromValue.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ std::optional<typename Adaptor::Type> toNativeFromValueWithoutCoercion(JSValue v
6363
if constexpr (std::is_same_v<Adaptor, BigInt64Adaptor> || std::is_same_v<Adaptor, BigUint64Adaptor>) {
6464
if (!value.isBigInt())
6565
return std::nullopt;
66+
// Without coercion, a BigInt outside the element type's range matches no element. It must not wrap around.
6667
if constexpr (std::is_same_v<Adaptor, BigInt64Adaptor>)
67-
return JSBigInt::toBigInt64(value);
68+
return JSBigInt::tryToBigInt64(value);
6869
else
69-
return JSBigInt::toBigUInt64(value);
70+
return JSBigInt::tryToBigUInt64(value);
7071
} else {
7172
if (!value.isNumber())
7273
return std::nullopt;

‎Source/JavaScriptCore/wasm/js/JSWebAssemblyHelpers.h‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,8 @@ ALWAYS_INLINE uint64_t toNonWrappingUint64(JSGlobalObject* globalObject, JSValue
7979
JSValue bigInt = value.toBigInt(globalObject);
8080
RETURN_IF_EXCEPTION(throwScope, { });
8181

82-
auto lo = JSBigInt::compare(bigInt, static_cast<uint64_t>(0));
83-
auto high = JSBigInt::compare(bigInt, std::numeric_limits<uint64_t>::max());
84-
bool validRange = lo != JSBigInt::ComparisonResult::LessThan && high != JSBigInt::ComparisonResult::GreaterThan;
85-
if (validRange && bigInt)
86-
RELEASE_AND_RETURN(throwScope, bigInt.toBigUInt64(globalObject));
82+
if (auto result = JSBigInt::tryToBigUInt64(bigInt))
83+
return *result;
8784

8885
constexpr auto message = "Expect an integer argument in the range: [0, 2^64 - 1]"_s;
8986
if (errorType == ErrorType::RangeError)

0 commit comments

Comments
 (0)