From 27a901c235f7c7665a0a4f9329091d2516631521 Mon Sep 17 00:00:00 2001 From: Max schwenk Date: Thu, 12 Nov 2026 15:19:30 -0500 Subject: [PATCH 1/2] deps: V8: cherry-pick 8a1da7c84be7 Original commit message: [json] Stringify: Bailout to slow-path if a string could exceed max len For strings that might require escaping, we calculate a pessimistic max length. This allows us to avoid any bounds checks when writing to the buffer. However this pessimistic max length can be greater than the allowed string max length (and even greater than INT_MAX), which could cause zone allocation to fail. To avoid zone allocation failure, we fall-back to the slow path if the pessimistic max length exceeds the allowed string max length. Fixed: 568816246 Change-Id: I496c34084bce56faad740ad4e139373d6d792552 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8529766 Reviewed-by: Igor Sheludko Commit-Queue: Patrick Thier Cr-Commit-Position: refs/heads/main@{#110411} Refs: https://github.com/v8/v8/commit/8a1da7c84be7bd6940d6d48a44220ee27f7d3ac3 Fixes: https://github.com/nodejs/node/issues/66489 --- common.gypi | 2 +- deps/v8/src/json/json-stringifier.cc | 68 ++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 19 deletions(-) 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). From 769c44a0bc33eb4b91622114d23f4e07271e7d71 Mon Sep 17 00:00:00 2001 From: Max schwenk Date: Thu, 12 Nov 2026 15:19:30 -0500 Subject: [PATCH 2/2] test: add JSON.stringify large flat string test JSON.stringify() of a flat string of 134217726 two-byte or 268435451 one-byte characters aborted the process with "Zone Allocation failed". Check that it returns the result, also for an object value and a property key, and that a result over the maximum string length throws a RangeError. Refs: https://github.com/nodejs/node/issues/66489 --- .../test-json-stringify-large-flat-string.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/pummel/test-json-stringify-large-flat-string.js 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', + }); +}