diff --git a/common.gypi b/common.gypi index f8588b823119..91efcb7cb87b 100644 --- a/common.gypi +++ b/common.gypi @@ -50,7 +50,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.11', + 'v8_embedder_string': '-node.12', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/src/json/json-stringifier.cc b/deps/v8/src/json/json-stringifier.cc index c7f692aed1f8..5bcf27a857b9 100644 --- a/deps/v8/src/json/json-stringifier.cc +++ b/deps/v8/src/json/json-stringifier.cc @@ -1845,13 +1845,15 @@ class OutBuffer { CopyChars(cur_, chars, length); cur_ += length; } - void EnsureCapacity(size_t size) { + V8_WARN_UNUSED_RESULT bool EnsureCapacity(size_t size) { #ifdef DEBUG current_requested_capacity_ = size; #endif - if (V8_LIKELY(size <= SegmentFreeChars())) return; + if (V8_LIKELY(size <= SegmentFreeChars())) return true; + if (V8_UNLIKELY(size > String::kMaxLength)) return false; Extend(size); DCHECK_GE(CurSegmentCapacity(), size); + return true; } size_t length() const { if (ZoneUsed()) { @@ -1968,7 +1970,8 @@ enum FastJsonStringifierResult { enum class FastJsonStringifierObjectKeyResult : uint8_t { kSuccess, kChangeEncoding, // Two-byte key in one-byte stringifier. - kSlow // Two-byte key (in two-byte stringifier) or requires escaping. + kSlowKey, // Two-byte key (in two-byte stringifier) or requires escaping. + kSlowPath // Extending the buffer failed. }; class ContinuationRecord { @@ -2245,14 +2248,18 @@ class FastJsonStringifier { V8_NOINLINE FastJsonStringifierResult HandleInterruptAndCheckCycle(); V8_NOINLINE bool CheckCycle(); - V8_INLINE void EnsureCapacity(size_t size) { buffer_.EnsureCapacity(size); } + V8_INLINE V8_PRESERVE_MOST bool EnsureCapacity(size_t size) { + return buffer_.EnsureCapacity(size); + } template V8_INLINE void AppendCharacterUnchecked(SrcChar c) { buffer_.AppendCharacter(c); } template V8_INLINE void AppendCharacter(SrcChar c) { - EnsureCapacity(1); + bool ok = EnsureCapacity(1); + DCHECK(ok); // Extending by 1 character will never fail. + USE(ok); AppendCharacterUnchecked(c); } template @@ -2269,7 +2276,10 @@ class FastJsonStringifier { // Note that the literal contains the zero char. constexpr size_t length = N - 1; static_assert(length > 0); - EnsureCapacity(length); + static_assert(length <= String::kMaxLength); + bool ok = EnsureCapacity(length); + DCHECK(ok); // Will never fail (static_asserted above). + USE(ok); AppendCStringLiteralUnchecked(literal); } @@ -2282,15 +2292,19 @@ class FastJsonStringifier { V8_INLINE void AppendStringUnchecked(std::string_view str) { AppendCStringUnchecked(str.data(), str.length()); } - V8_INLINE void AppendCString(const char* chars, size_t len) { - EnsureCapacity(len); + V8_INLINE V8_WARN_UNUSED_RESULT FastJsonStringifierResult + AppendCString(const char* chars, size_t len) { + if (V8_UNLIKELY(!EnsureCapacity(len))) return SLOW_PATH; AppendCStringUnchecked(chars, len); + return SUCCESS; } - V8_INLINE void AppendCString(const char* chars) { - AppendCString(chars, strlen(chars)); + V8_INLINE V8_WARN_UNUSED_RESULT FastJsonStringifierResult + AppendCString(const char* chars) { + return AppendCString(chars, strlen(chars)); } - V8_INLINE void AppendString(std::string_view str) { - AppendCString(str.data(), str.length()); + V8_INLINE V8_WARN_UNUSED_RESULT FastJsonStringifierResult + AppendString(std::string_view str) { + return AppendCString(str.data(), str.length()); } template @@ -2386,7 +2400,9 @@ void FastJsonStringifier::SerializeSmi(Tagged object) { char chars[kBufferSize]; base::Vector buffer(chars, kBufferSize); std::string_view str = IntToStringView(object.value(), buffer); - AppendString(str); + FastJsonStringifierResult result = AppendString(str); + DCHECK_EQ(result, SUCCESS); // Extending for a Smi value will never fail. + USE(result); } template @@ -2399,7 +2415,9 @@ void FastJsonStringifier::SerializeDouble(double number) { char chars[kBufferSize]; base::Vector buffer(chars, kBufferSize); std::string_view str = DoubleToStringView(number, buffer); - AppendString(str); + FastJsonStringifierResult result = AppendString(str); + DCHECK_EQ(result, SUCCESS); // Will never fail (max 100 byte extend). + USE(result); } template @@ -2507,7 +2525,9 @@ FastJsonStringifier::SerializeObjectKey( max_length = MaxEscapedStringLength(length); } max_length += 4 /* optional comma + 2x double quote + colon */; - EnsureCapacity(max_length); + if (V8_UNLIKELY(!EnsureCapacity(max_length))) { + return FastJsonStringifierObjectKeyResult::kSlowPath; + } SeparatorUnchecked(comma); AppendCharacterUnchecked('"'); FastJsonStringifierObjectKeyResult result; @@ -2523,7 +2543,7 @@ FastJsonStringifier::SerializeObjectKey( String::DoesNotContainEscapeCharacters(obj)); result = sizeof(StringChar) == 1 && !needs_escaping ? FastJsonStringifierObjectKeyResult::kSuccess - : FastJsonStringifierObjectKeyResult::kSlow; + : FastJsonStringifierObjectKeyResult::kSlowKey; } AppendCharacterUnchecked('"'); AppendCharacterUnchecked(':'); @@ -2547,7 +2567,9 @@ FastJsonStringifierResult FastJsonStringifier::SerializeString( chars = string->GetChars(); } const uint32_t length = string->length(); - EnsureCapacity(MaxEscapedStringLength(length) + 2 /* 2x double quote */); + size_t max_length = + MaxEscapedStringLength(length) + 2 /* 2x double quote */; + if (V8_UNLIKELY(!EnsureCapacity(max_length))) return SLOW_PATH; AppendCharacterUnchecked('"'); AppendString(chars, length, no_gc); AppendCharacterUnchecked('"'); @@ -2881,6 +2903,10 @@ FastJsonStringifierResult FastJsonStringifier::ResumeJSObject( if (V8_UNLIKELY(key_result != FastJsonStringifierObjectKeyResult::kSuccess)) { descriptors->set_fast_iterable(FastIterableState::kJsonSlow); + if (V8_UNLIKELY(key_result == + FastJsonStringifierObjectKeyResult::kSlowPath)) { + return SLOW_PATH; + } if constexpr (is_one_byte) { if (key_result == FastJsonStringifierObjectKeyResult::kChangeEncoding) { @@ -3060,7 +3086,9 @@ FastJsonStringifierResult FastJsonStringifier::SerializeFixedArrayElement( Tagged elements, uint32_t i, uint32_t length) { if constexpr (IsHoleyElementsKind(kind)) { if (elements->is_the_hole(isolate_, i)) { - EnsureCapacity(5 /* "null" + optional comma */); + if (V8_UNLIKELY(!EnsureCapacity(5 /* "null" + optional comma */))) { + return SLOW_PATH; + } SeparatorUnchecked(i > 0); AppendCStringLiteralUnchecked("null"); return SUCCESS; @@ -3132,6 +3160,10 @@ FastJsonStringifierResult FastJsonStringifier::ResumeFrom( cont.object_key(), cont.object_key_comma(), no_gc); USE(key_result); DCHECK_NE(key_result, FastJsonStringifierObjectKeyResult::kChangeEncoding); + if (V8_UNLIKELY(key_result == + FastJsonStringifierObjectKeyResult::kSlowPath)) { + return SLOW_PATH; + } // Resuming due to encoding change of an object key guarantees that there // are at least two other objects on the stack (the value for that key, and // the continuation record for the object the key is a member of). diff --git a/test/pummel/test-json-stringify-large-flat-string.js b/test/pummel/test-json-stringify-large-flat-string.js new file mode 100644 index 000000000000..0cd318e9776b --- /dev/null +++ b/test/pummel/test-json-stringify-large-flat-string.js @@ -0,0 +1,48 @@ +'use strict'; +const common = require('../common'); + +// JSON.stringify() of a large flat string used to abort the process with +// "Zone Allocation failed", because V8 reserved the worst-case escaped length +// (eight times the input) as one allocation. It has to return the result, or +// throw a RangeError when the result is longer than the maximum string length. +// Refs: https://github.com/nodejs/node/issues/66489 + +common.skipIf32Bits(); +if (!common.enoughTestMem) + common.skip('Insufficient memory for large string test'); + +const assert = require('assert'); + +// JSON.stringify() returns a flat string, and String.prototype.repeat() does +// not, so the inputs are made with JSON.stringify(). + +{ + // Two-byte string, 134217726 characters: 8 * length * 2 bytes is over 2 GiB. + const flat = JSON.stringify('€' + 'a'.repeat(134217723)); + assert.strictEqual(flat.length, 134217726); + assert.strictEqual(JSON.stringify(flat).length, 134217730); + assert.strictEqual(JSON.stringify({ payload: flat }).length, 134217742); + assert.strictEqual(JSON.stringify({ [flat]: 1 }).length, 134217734); +} + +{ + // One-byte string, 268435451 characters: 8 * length bytes is over 2 GiB. + const flat = JSON.stringify('a'.repeat(268435449)); + assert.strictEqual(flat.length, 268435451); + const result = JSON.stringify(flat); + assert.strictEqual(result.length, 268435455); + assert.strictEqual(result.slice(0, 4), '"\\"a'); + assert.strictEqual(result.slice(-4), 'a\\""'); + assert.strictEqual(JSON.stringify([flat]).length, 268435457); + assert.strictEqual(JSON.stringify({ [flat]: 1 }).length, 268435459); +} + +{ + // The result would have 600000006 characters. + const flat = JSON.stringify('"'.repeat(149999999)); + assert.strictEqual(flat.length, 300000000); + assert.throws(() => JSON.stringify(flat), { + name: 'RangeError', + message: 'Invalid string length', + }); +}