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..f983b8e8822e2 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,103 @@ 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()); + EXPECT_EQ(nonLatin1, sharesBuffer[0]); + EXPECT_EQ(nonLatin1, sharesBuffer[writtenLength - 1]); +} + +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;