Skip to content

Commit ea56bf5

Browse files
jakobkummerowCommit Bot
authored and
Commit Bot
committed
[ptr-compr] Bump max string length to ~2**29
Without pointer compression, the max string length on 64-bit platforms used to be 2**30 (minus header). With pointer-compression, this was accidentally lowered to 2**28 (which is the historical limit for 32-bit platforms). This CL bumps the limit on 64-bit platforms to 2**29, which is the maximum we can support given that any heap object's size in bytes must fit into a Smi (which are now 31-bit on all 64-bit platforms, with or without pointer compression). Change-Id: I263544317d9e6137f6b6a044784a21f41a2761b0 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2030916 Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Reviewed-by: Igor Sheludko <ishell@chromium.org> Commit-Queue: Jakob Kummerow <jkummerow@chromium.org> Cr-Commit-Position: refs/heads/master@{#66083}
1 parent 4eac274 commit ea56bf5

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

include/v8.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -2983,9 +2983,8 @@ enum class NewStringType {
29832983
*/
29842984
class V8_EXPORT String : public Name {
29852985
public:
2986-
static constexpr int kMaxLength = internal::kApiTaggedSize == 4
2987-
? (1 << 28) - 16
2988-
: internal::kSmiMaxValue / 2 - 24;
2986+
static constexpr int kMaxLength =
2987+
internal::kApiSystemPointerSize == 4 ? (1 << 28) - 16 : (1 << 29) - 24;
29892988

29902989
enum Encoding {
29912990
UNKNOWN_ENCODING = 0x1,

src/objects/string.h

+20-8
Original file line numberDiff line numberDiff line change
@@ -363,16 +363,28 @@ class String : public TorqueGeneratedString<String, Name> {
363363
static const uc32 kMaxCodePoint = 0x10ffff;
364364

365365
// Maximal string length.
366-
// The max length is different on 32 and 64 bit platforms. Max length for a
367-
// 32-bit platform is ~268.4M chars. On 64-bit platforms, max length is
368-
// ~1.073B chars. The limit on 64-bit is so that SeqTwoByteString::kMaxSize
369-
// can fit in a 32bit int: 2^31 - 1 is the max positive int, minus one bit as
370-
// each char needs two bytes, subtract 24 bytes for the string header size.
371-
366+
// The max length is different on 32 and 64 bit platforms. Max length for
367+
// 32-bit platforms is ~268.4M chars. On 64-bit platforms, max length is
368+
// ~536.8M chars.
372369
// See include/v8.h for the definition.
373370
static const int kMaxLength = v8::String::kMaxLength;
374-
static_assert(kMaxLength <= (Smi::kMaxValue / 2 - kHeaderSize),
375-
"Unexpected max String length");
371+
// There are several defining limits imposed by our current implementation:
372+
// - any string's length must fit into a Smi.
373+
static_assert(kMaxLength <= kSmiMaxValue,
374+
"String length must fit into a Smi");
375+
// - adding two string lengths must still fit into a 32-bit int without
376+
// overflow
377+
static_assert(kMaxLength * 2 <= kMaxInt,
378+
"String::kMaxLength * 2 must fit into an int32");
379+
// - any heap object's size in bytes must be able to fit into a Smi, because
380+
// its space on the heap might be filled with a Filler; for strings this
381+
// means SeqTwoByteString::kMaxSize must be able to fit into a Smi.
382+
static_assert(kMaxLength * 2 + kHeaderSize <= kSmiMaxValue,
383+
"String object size in bytes must fit into a Smi");
384+
// - any heap object's size in bytes must be able to fit into an int, because
385+
// that's what our object handling code uses almost everywhere.
386+
static_assert(kMaxLength * 2 + kHeaderSize <= kMaxInt,
387+
"String object size in bytes must fit into an int");
376388

377389
// Max length for computing hash. For strings longer than this limit the
378390
// string length is used as the hash value.

0 commit comments

Comments
 (0)