From e5723fe645b23a3df9ffc6dd1c66939005902823 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 11 Sep 2026 15:33:14 +0000 Subject: [PATCH 1/2] StringBuilder: cap a doubled capacity at the longest string of the buffer's character type StringBuilder::expandedCapacity() capped the doubled capacity at String::MaxLength (2147483647). A 16-bit StringImpl cannot be that long. StringImpl::isValidLength accepts at most 2147483635 characters, because the allocation size must fit in an unsigned. A builder that grows a 16-bit buffer, or converts an 8-bit buffer to 16-bit, from a capacity of 1073741818 characters or more asked StringImpl for a length it refuses. The builder then overflowed: CRASH() for the default policy, a recorded overflow for OverflowPolicy::RecordOverflow. The length the caller needed was valid. Only the extra capacity was not. For example unescape("%u1234".repeat(178956971)) threw "Out of memory" for a result of 178956971 characters. unescape() reserves an 8-bit buffer of the input length (1073741826), and the first %u1234 converts it to 16-bit. expandedCapacity() now takes the character type of the buffer it sizes and caps the doubled capacity at StringImpl::maxValidLength(), the limit that isValidLength checks. A required length above that limit still overflows, because expandedCapacity() never returns less than the required capacity. Two stress tests expected "Out of memory" for a 16-bit input of 2^30 characters. Both functions reserve an 8-bit buffer of the input length and convert it on the first 16-bit character, so they only failed because of this bug. Their results are valid strings. Both tests now limit the size of one allocation with --maxSingleAllocationSize, as js-fixed-array-out-of-memory.js does, and use an input that fits in the limit with a result that does not. They still cover the overflow branch of each function, and they need 1MB and not 2GB. * JSTests/stress/out-of-memory-in-globalFuncUnescape.js: * JSTests/stress/regexp-escape-oom.js: * JSTests/stress/unescape-upconvert-input-above-half-of-max-string-length.js: Added. * Source/WTF/wtf/text/StringBuilder.cpp: (WTF::StringBuilder::extendBufferForAppendingWithUpconvert): (WTF::StringBuilder::expandedCapacity): Moved to StringBuilderInternals.h. * Source/WTF/wtf/text/StringBuilder.h: * Source/WTF/wtf/text/StringBuilderInternals.h: (WTF::StringBuilder::expandedCapacity): (WTF::StringBuilder::extendBufferForAppendingSlowCase): * Source/WTF/wtf/text/StringImpl.h: (WTF::StringImpl::maxValidLength): Added. (WTF::StringImpl::isValidLength): * Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp: (WTF::StringTypeAdapter): Added. It makes a builder need a long buffer and writes nothing, so the tests below touch almost no memory. (TestWebKitAPI::canAllocateBothBuffers): (TestWebKitAPI::TEST(StringBuilderTest, UpconvertCapacityAboveHalfOfMaxLength)): (TestWebKitAPI::TEST(StringBuilderTest, Grow16BitCapacityAboveHalfOfMaxLength)): (TestWebKitAPI::TEST(StringBuilderTest, LengthAboveMaxLength16BitOverflows)): --- .../out-of-memory-in-globalFuncUnescape.js | 6 +- JSTests/stress/regexp-escape-oom.js | 6 +- ...t-input-above-half-of-max-string-length.js | 17 +++ Source/WTF/wtf/text/StringBuilder.cpp | 10 +- Source/WTF/wtf/text/StringBuilder.h | 2 +- Source/WTF/wtf/text/StringBuilderInternals.h | 10 +- Source/WTF/wtf/text/StringImpl.h | 12 +- .../TestWebKitAPI/Tests/WTF/StringBuilder.cpp | 125 ++++++++++++++++++ 8 files changed, 169 insertions(+), 19 deletions(-) create mode 100644 JSTests/stress/unescape-upconvert-input-above-half-of-max-string-length.js diff --git a/JSTests/stress/out-of-memory-in-globalFuncUnescape.js b/JSTests/stress/out-of-memory-in-globalFuncUnescape.js index 402c88f4bf188..9b0cc9d55cef8 100644 --- a/JSTests/stress/out-of-memory-in-globalFuncUnescape.js +++ b/JSTests/stress/out-of-memory-in-globalFuncUnescape.js @@ -1,9 +1,9 @@ -//@ memoryHog! -//@ if $buildType == "release" then runDefault else skip end +//@ if $buildType == "debug" then runDefault("--maxSingleAllocationSize=1048576") else skip end +// With this limit on the size of one allocation, the 8-bit input fits and the 16-bit result does not. var exception; try { - unescape('\u0100'.repeat(2**30)); + unescape('%u0100' + 'a'.repeat(600000)); } catch (e) { exception = e; } diff --git a/JSTests/stress/regexp-escape-oom.js b/JSTests/stress/regexp-escape-oom.js index 5d03c5dbb6b1c..4681899ba56c3 100644 --- a/JSTests/stress/regexp-escape-oom.js +++ b/JSTests/stress/regexp-escape-oom.js @@ -1,9 +1,9 @@ -//@ memoryHog! -//@ runDefault +//@ if $buildType == "debug" then runDefault("--maxSingleAllocationSize=1048576") else skip end +// With this limit on the size of one allocation, the input fits and the result does not. Each "^" becomes "\^". var exception; try { - RegExp.escape('퀀'.repeat?.(2 ** 30)); + RegExp.escape('퀀' + '^'.repeat?.(300000)); } catch (e) { exception = e; } diff --git a/JSTests/stress/unescape-upconvert-input-above-half-of-max-string-length.js b/JSTests/stress/unescape-upconvert-input-above-half-of-max-string-length.js new file mode 100644 index 0000000000000..925cd4d5b6a8c --- /dev/null +++ b/JSTests/stress/unescape-upconvert-input-above-half-of-max-string-length.js @@ -0,0 +1,17 @@ +//@ memoryHog! +//@ skip if $buildType == "debug" or $addressBits <= 32 +//@ runDefault + +// unescape() reserves an 8-bit StringBuilder buffer of the input length, and the first %uXXXX converts +// that buffer to 16-bit. Twice the input length is more than the longest 16-bit string here. The result +// is a sixth of the input length, so this must not throw an out of memory error. +const count = 178956971; +const input = "%u1234".repeat(count); +if (input.length <= 2 ** 30) + throw new Error("input is too short: " + input.length); + +const output = unescape(input); +if (output.length !== count) + throw new Error("bad length: " + output.length); +if (output.charCodeAt(0) !== 0x1234 || output.charCodeAt(count - 1) !== 0x1234) + throw new Error("bad content"); diff --git a/Source/WTF/wtf/text/StringBuilder.cpp b/Source/WTF/wtf/text/StringBuilder.cpp index e43c6a7b75f51..302798403e00c 100644 --- a/Source/WTF/wtf/text/StringBuilder.cpp +++ b/Source/WTF/wtf/text/StringBuilder.cpp @@ -32,14 +32,6 @@ namespace WTF { -static constexpr unsigned maxCapacity = String::MaxLength; - -unsigned StringBuilder::expandedCapacity(unsigned capacity, unsigned requiredCapacity) -{ - static constexpr unsigned minimumCapacity = 16; - return std::max(requiredCapacity, std::max(minimumCapacity, std::min(capacity * 2, maxCapacity))); -} - void StringBuilder::didOverflow() { if (m_shouldCrashOnOverflow) @@ -141,7 +133,7 @@ std::span StringBuilder::extendBufferForAppendingLatin1Characte std::span StringBuilder::extendBufferForAppendingWithUpconvert(unsigned requiredLength) { if (is8Bit()) { - allocateBuffer(span8(), expandedCapacity(capacity(), requiredLength)); + allocateBuffer(span8(), expandedCapacity(capacity(), requiredLength)); if (hasOverflowed()) [[unlikely]] return { }; return spanConstCast(m_buffer->span16().subspan(std::exchange(m_length, requiredLength))); diff --git a/Source/WTF/wtf/text/StringBuilder.h b/Source/WTF/wtf/text/StringBuilder.h index 358208b8e0602..8fa4c9794adde 100644 --- a/Source/WTF/wtf/text/StringBuilder.h +++ b/Source/WTF/wtf/text/StringBuilder.h @@ -102,7 +102,7 @@ class StringBuilder { WTF_EXPORT_PRIVATE bool NODELETE containsOnlyASCII() const; private: - static unsigned expandedCapacity(unsigned capacity, unsigned requiredCapacity); + template static unsigned expandedCapacity(unsigned capacity, unsigned requiredCapacity); template void allocateBuffer(std::span currentCharacters, unsigned requiredCapacity); template void reallocateBuffer(unsigned requiredCapacity); diff --git a/Source/WTF/wtf/text/StringBuilderInternals.h b/Source/WTF/wtf/text/StringBuilderInternals.h index 4f75f6f97b532..8dce11ea14d67 100644 --- a/Source/WTF/wtf/text/StringBuilderInternals.h +++ b/Source/WTF/wtf/text/StringBuilderInternals.h @@ -30,6 +30,14 @@ namespace WTF { +// CharacterType is the type of the buffer the returned capacity is for. +template unsigned StringBuilder::expandedCapacity(unsigned capacity, unsigned requiredCapacity) +{ + static constexpr unsigned minimumCapacity = 16; + static constexpr unsigned maxCapacity = StringImpl::maxValidLength(); + return std::max(requiredCapacity, std::max(minimumCapacity, std::min(capacity * 2, maxCapacity))); +} + // Allocate a new buffer, copying in currentCharacters (these may come from either m_string or m_buffer. template void StringBuilder::allocateBuffer(std::span currentCharactersToCopy, unsigned requiredCapacity) { @@ -85,7 +93,7 @@ template std::span StringBuilder::extendB { if (!requiredLength || hasOverflowed()) return { }; - reallocateBuffer(expandedCapacity(capacity(), requiredLength)); + reallocateBuffer(expandedCapacity(capacity(), requiredLength)); if (hasOverflowed()) [[unlikely]] return { }; return spanConstCast(m_buffer->span().subspan(std::exchange(m_length, requiredLength))); diff --git a/Source/WTF/wtf/text/StringImpl.h b/Source/WTF/wtf/text/StringImpl.h index cf4a02dac2ec3..985ec4c1ecc76 100644 --- a/Source/WTF/wtf/text/StringImpl.h +++ b/Source/WTF/wtf/text/StringImpl.h @@ -209,6 +209,8 @@ class StringImpl : private StringImplShape, public NoVirtualDestructorBase { // Prefer using isValidLength over MaxLength when the character type is known. template static constexpr bool isValidLength(size_t); + // The longest length isValidLength accepts. For char16_t it is less than MaxLength. + template static constexpr unsigned maxValidLength(); static constexpr unsigned MaxLength = StringImplShape::MaxLength; @@ -1261,10 +1263,16 @@ template inline size_t StringImpl::allocationSize(Checked ta } template -inline constexpr bool StringImpl::isValidLength(size_t length) +inline constexpr unsigned StringImpl::maxValidLength() { // In order to not overflow the unsigned length, the check for (std::numeric_limits::max() - sizeof(StringImpl)) is needed when sizeof(CharacterType) == 2. - constexpr size_t max = std::min(static_cast(MaxLength), (std::numeric_limits::max() - sizeof(StringImpl)) / sizeof(CharacterType)); + return static_cast(std::min(static_cast(MaxLength), (std::numeric_limits::max() - sizeof(StringImpl)) / sizeof(CharacterType))); +} + +template +inline constexpr bool StringImpl::isValidLength(size_t length) +{ + constexpr size_t max = maxValidLength(); return length <= max; } diff --git a/Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp b/Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp index e5011ac3e5bdb..c85f4fd4afbf4 100644 --- a/Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp +++ b/Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp @@ -35,11 +35,41 @@ #include "Helpers/Test.h" #include "Helpers/WTFTestUtilities.h" #include +#include #include #include namespace TestWebKitAPI { +// A run of 16-bit characters that a StringBuilder makes room for and that nobody writes. +// A test can make a builder need a very long buffer with it and touch none of the memory. +struct UnwrittenCharacters { + unsigned length; +}; + +} // namespace TestWebKitAPI + +namespace WTF { + +template<> class StringTypeAdapter { +public: + StringTypeAdapter(TestWebKitAPI::UnwrittenCharacters characters) + : m_length { characters.length } + { + } + + unsigned length() const { return m_length; } + bool is8Bit() const { return false; } + template void writeTo(std::span) const { } + +private: + unsigned m_length; +}; + +} // namespace WTF + +namespace TestWebKitAPI { + static String builderContent(const StringBuilder& builder) { // Not using builder.toString() or builder.toStringPreserveCapacity() because they all @@ -442,6 +472,101 @@ TEST(StringBuilderTest, ShouldShrinkToFit) EXPECT_FALSE(builder.shouldShrinkToFit()); } +// The longest 16-bit string is shorter than String::MaxLength, because the size of a StringImpl has to fit in an unsigned. +static constexpr unsigned maxLength16Bit = StringImpl::maxValidLength(); +static_assert(maxLength16Bit < String::MaxLength); +static_assert(StringImpl::isValidLength(maxLength16Bit) && !StringImpl::isValidLength(static_cast(maxLength16Bit) + 1)); +static_assert(StringImpl::maxValidLength() == String::MaxLength); + +// Twice this capacity is more than the longest 16-bit string. +static constexpr unsigned capacityAboveHalfOfMaxLength16Bit = maxLength16Bit / 2 + 1; +static_assert(capacityAboveHalfOfMaxLength16Bit * 2 > maxLength16Bit); + +// The tests that call this reserve up to 6GB of address space and write to almost none of it. They return early +// on a machine that cannot reserve that much. They do not call GTEST_SKIP(), because the TestWebKitAPI listener +// reports a skipped test as a failure. +template +static bool canAllocateBothBuffers(unsigned firstLength, unsigned secondLength) +{ + std::span firstCharacters; + std::span secondCharacters; + auto firstBuffer = StringImpl::tryCreateUninitialized(firstLength, firstCharacters); + auto secondBuffer = StringImpl::tryCreateUninitialized(secondLength, secondCharacters); + if (firstBuffer && secondBuffer) + return true; + dataLogLn("Cannot allocate the buffers for this test, so the test did not run."); + return false; +} + +TEST(StringBuilderTest, UpconvertCapacityAboveHalfOfMaxLength) +{ + constexpr unsigned capacity8Bit = capacityAboveHalfOfMaxLength16Bit; + if (!canAllocateBothBuffers(capacity8Bit, maxLength16Bit)) + return; + + StringBuilder builder(OverflowPolicy::RecordOverflow); + builder.reserveCapacity(capacity8Bit); + builder.append("ab"_s); + ASSERT_FALSE(builder.hasOverflowed()); + EXPECT_TRUE(builder.is8Bit()); + EXPECT_EQ(capacity8Bit, builder.capacity()); + + const char16_t nonLatin1 = 0x1234; + builder.append(nonLatin1); + ASSERT_FALSE(builder.hasOverflowed()); + EXPECT_FALSE(builder.is8Bit()); + EXPECT_GE(builder.capacity(), capacity8Bit); + EXPECT_LE(builder.capacity(), maxLength16Bit); + EXPECT_EQ(3U, builder.length()); + EXPECT_EQ('a', static_cast(builder[0])); + EXPECT_EQ('b', static_cast(builder[1])); + EXPECT_EQ(nonLatin1, builder[2]); +} + +TEST(StringBuilderTest, Grow16BitCapacityAboveHalfOfMaxLength) +{ + constexpr unsigned capacity = capacityAboveHalfOfMaxLength16Bit; + if (!canAllocateBothBuffers(capacity, maxLength16Bit)) + return; + + const char16_t nonLatin1 = 0x1234; + constexpr unsigned writtenLength = 16; + + StringBuilder builder(OverflowPolicy::RecordOverflow); + for (unsigned i = 0; i < writtenLength; ++i) + builder.append(nonLatin1); + builder.reserveCapacity(capacity); + ASSERT_FALSE(builder.hasOverflowed()); + EXPECT_FALSE(builder.is8Bit()); + EXPECT_EQ(capacity, builder.capacity()); + + // This string shares the buffer with the builder. A builder that is not the only owner of its buffer copies + // its characters to a new buffer. A builder that is the only owner reallocates, and that can copy all 2GB. + String sharesBuffer = builder.toStringPreserveCapacity(); + + builder.append(UnwrittenCharacters { capacity - writtenLength + 1 }); + ASSERT_FALSE(builder.hasOverflowed()); + EXPECT_EQ(capacity + 1, builder.length()); + EXPECT_GE(builder.capacity(), builder.length()); + EXPECT_LE(builder.capacity(), maxLength16Bit); + EXPECT_EQ(nonLatin1, builder[0]); + EXPECT_EQ(nonLatin1, builder[writtenLength - 1]); + EXPECT_EQ(writtenLength, sharesBuffer.length()); +} + +TEST(StringBuilderTest, LengthAboveMaxLength16BitOverflows) +{ + const char16_t nonLatin1 = 0x1234; + StringBuilder builder(OverflowPolicy::RecordOverflow); + builder.append(nonLatin1); + ASSERT_FALSE(builder.hasOverflowed()); + EXPECT_FALSE(builder.is8Bit()); + + // The longest 16-bit string plus one character. The builder has to refuse this length, whatever capacity it picks. + builder.append(UnwrittenCharacters { maxLength16Bit }); + EXPECT_TRUE(builder.hasOverflowed()); +} + TEST(StringBuilderTest, ToAtomString) { StringBuilder builder; From fc7c8b32badc4c658b3e8f5399020ecdf05768a5 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 12 Sep 2026 03:18:12 +0000 Subject: [PATCH 2/2] StringBuilder API test: check the contents of the string that shares the buffer After the builder moves to a new buffer, the string that shared the old buffer must keep its characters, not only its length. * Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp: (TestWebKitAPI::TEST(StringBuilderTest, Grow16BitCapacityAboveHalfOfMaxLength)): --- Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp b/Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp index c85f4fd4afbf4..f983b8e8822e2 100644 --- a/Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp +++ b/Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp @@ -552,6 +552,8 @@ TEST(StringBuilderTest, Grow16BitCapacityAboveHalfOfMaxLength) EXPECT_EQ(nonLatin1, builder[0]); EXPECT_EQ(nonLatin1, builder[writtenLength - 1]); EXPECT_EQ(writtenLength, sharesBuffer.length()); + EXPECT_EQ(nonLatin1, sharesBuffer[0]); + EXPECT_EQ(nonLatin1, sharesBuffer[writtenLength - 1]); } TEST(StringBuilderTest, LengthAboveMaxLength16BitOverflows)