Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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 #####

Expand Down
68 changes: 50 additions & 18 deletions deps/v8/src/json/json-stringifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 <typename SrcChar>
V8_INLINE void AppendCharacterUnchecked(SrcChar c) {
buffer_.AppendCharacter(c);
}
template <typename SrcChar>
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 <size_t N>
Expand All @@ -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);
}

Expand All @@ -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 <typename SrcChar>
Expand Down Expand Up @@ -2386,7 +2400,9 @@ void FastJsonStringifier<Char>::SerializeSmi(Tagged<Smi> object) {
char chars[kBufferSize];
base::Vector<char> 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 <typename Char>
Expand All @@ -2399,7 +2415,9 @@ void FastJsonStringifier<Char>::SerializeDouble(double number) {
char chars[kBufferSize];
base::Vector<char> 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 <typename Char>
Expand Down Expand Up @@ -2507,7 +2525,9 @@ FastJsonStringifier<Char>::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;
Expand All @@ -2523,7 +2543,7 @@ FastJsonStringifier<Char>::SerializeObjectKey(
String::DoesNotContainEscapeCharacters(obj));
result = sizeof(StringChar) == 1 && !needs_escaping
? FastJsonStringifierObjectKeyResult::kSuccess
: FastJsonStringifierObjectKeyResult::kSlow;
: FastJsonStringifierObjectKeyResult::kSlowKey;
}
AppendCharacterUnchecked('"');
AppendCharacterUnchecked(':');
Expand All @@ -2547,7 +2567,9 @@ FastJsonStringifierResult FastJsonStringifier<Char>::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('"');
Expand Down Expand Up @@ -2881,6 +2903,10 @@ FastJsonStringifierResult FastJsonStringifier<Char>::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) {
Expand Down Expand Up @@ -3060,7 +3086,9 @@ FastJsonStringifierResult FastJsonStringifier<Char>::SerializeFixedArrayElement(
Tagged<T> 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;
Expand Down Expand Up @@ -3132,6 +3160,10 @@ FastJsonStringifierResult FastJsonStringifier<Char>::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).
Expand Down
48 changes: 48 additions & 0 deletions test/pummel/test-json-stringify-large-flat-string.js
Original file line number Diff line number Diff line change
@@ -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',
});
}
Loading