Debian Codegraph @

js/src/util/StringBuilder.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef util_StringBuilder_h
8 #define util_StringBuilder_h
9
10 #include "mozilla/CheckedInt.h"
11 #include "mozilla/MaybeOneOf.h"
12 #include "mozilla/Utf8.h"
13
14 #include "frontend/FrontendContext.h"
15 #include "js/Vector.h"
16 #include "vm/Runtime.h"
17 #include "vm/StringType.h"
18
19 namespace js {
20
21 class FrontendContext;
22
23 namespace frontend {
24 class ParserAtomsTable;
25 class TaggedParserAtomIndex;
26 } // namespace frontend
27
28 namespace detail {
29
30 // GrowEltsAggressively will multiply the space by a factor of 8 on overflow, to
31 // avoid very expensive memcpys for large strings (eg giant toJSON output for
32 // sessionstore.js). Drop back to the normal expansion policy once the buffer
33 // hits 128MB.
34 static constexpr size_t AggressiveLimit = 128 << 20;
35
36 template <size_t EltSize>
37 inline size_t GrowEltsAggressively(size_t aOldElts, size_t aIncr) {
38 mozilla::CheckedInt<size_t> required =
39 mozilla::CheckedInt<size_t>(aOldElts) + aIncr;
40 if (!(required * 2).isValid()) {
41 return 0;
42 }
43 required = mozilla::RoundUpPow2(required.value());
44 required *= 8;
45 if (!(required * EltSize).isValid() || required.value() > AggressiveLimit) {
46 // Fall back to doubling behavior if the aggressive growth fails or gets too
47 // big.
48 return mozilla::detail::GrowEltsByDoubling<EltSize>(aOldElts, aIncr);
49 }
50 return required.value();
51 };
52
53 } // namespace detail
54
55 class StringBuilderAllocPolicy {
56 TempAllocPolicy impl_;
57 const arena_id_t& arenaId_;
58
59 public:
60 StringBuilderAllocPolicy(FrontendContext* fc, const arena_id_t& arenaId)
61 : impl_(fc), arenaId_(arenaId) {}
62
63 StringBuilderAllocPolicy(JSContext* cx, const arena_id_t& arenaId)
64 : impl_(cx), arenaId_(arenaId) {}
65
66 template <typename T>
67 T* maybe_pod_malloc(size_t numElems) {
68 return impl_.maybe_pod_arena_malloc<T>(arenaId_, numElems);
69 }
70 template <typename T>
71 T* maybe_pod_calloc(size_t numElems) {
72 return impl_.maybe_pod_arena_calloc<T>(arenaId_, numElems);
73 }
74 template <typename T>
75 T* maybe_pod_realloc(T* p, size_t oldSize, size_t newSize) {
76 return impl_.maybe_pod_arena_realloc<T>(arenaId_, p, oldSize, newSize);
77 }
78 template <typename T>
79 T* pod_malloc(size_t numElems) {
80 return impl_.pod_arena_malloc<T>(arenaId_, numElems);
81 }
82 template <typename T>
83 T* pod_calloc(size_t numElems) {
84 return impl_.pod_arena_calloc<T>(arenaId_, numElems);
85 }
86 template <typename T>
87 T* pod_realloc(T* p, size_t oldSize, size_t newSize) {
88 return impl_.pod_arena_realloc<T>(arenaId_, p, oldSize, newSize);
89 }
90 template <typename T>
91 void free_(T* p, size_t numElems = 0) {
92 impl_.free_(p, numElems);
93 }
94 void reportAllocOverflow() const { impl_.reportAllocOverflow(); }
95 bool checkSimulatedOOM() const { return impl_.checkSimulatedOOM(); }
96
97 // See ComputeGrowth in mfbt/Vector.h.
98 template <size_t EltSize>
99 static size_t computeGrowth(size_t aOldElts, size_t aIncr) {
100 return detail::GrowEltsAggressively<EltSize>(aOldElts, aIncr);
101 }
102 };
103
104 /*
105 * String builder that eagerly checks for over-allocation past the maximum
106 * string length.
107 *
108 * Any operation which would exceed the maximum string length causes an
109 * exception report on the context and results in a failed return value.
110 *
111 * Well-sized extractions (which waste no more than 1/4 of their char
112 * buffer space) are guaranteed for strings built by this interface.
113 * See |extractWellSized|.
114 */
115 class StringBuilder {
116 protected:
117 template <typename CharT>
118 using BufferType =
119 Vector<CharT, 80 / sizeof(CharT), StringBuilderAllocPolicy>;
120
121 /*
122 * The Vector's buffer may be either stolen or copied, so we need to use
123 * TempAllocPolicy and account for the memory manually when stealing.
124 */
125 using Latin1CharBuffer = BufferType<Latin1Char>;
126 using TwoByteCharBuffer = BufferType<char16_t>;
127
128 JSContext* maybeCx_ = nullptr;
129
130 // cb starts out as a Latin1CharBuffer. When a TwoByte char is appended,
131 // inflateChars() constructs a TwoByteCharBuffer and copies the Latin1 chars.
132 //
133 // Note that this buffer can include extra zero bytes for the string buffer
134 // header. See numHeaderChars_ below.
135 mozilla::MaybeOneOf<Latin1CharBuffer, TwoByteCharBuffer> cb;
136
137 // Number of reserve()'d chars, see inflateChars. Does not include
138 // numHeaderChars_.
139 size_t reservedExclHeader_ = 0;
140
141 // The number of '\0' characters prepended by JSStringBuilder to reserve space
142 // for the mozilla::StringBuffer header. This is always non-zero for
143 // JSStringBuilder and zero otherwise.
144 //
145 // Note that this is an implementation detail: public methods such as |begin|
146 // and |length| return the actual string contents/length without these extra
147 // characters.
148 uint8_t numHeaderChars_ = 0;
149
150 StringBuilder(const StringBuilder& other) = delete;
151 void operator=(const StringBuilder& other) = delete;
152
153 // Returns the number of characters to prepend to reserve enough space for the
154 // mozilla::StringBuffer header.
155 template <typename CharT>
156 static constexpr size_t numHeaderChars() {
157 static_assert(sizeof(mozilla::StringBuffer) % sizeof(CharT) == 0);
158 return sizeof(mozilla::StringBuffer) / sizeof(CharT);
159 }
160
161 template <typename CharT>
162 MOZ_ALWAYS_INLINE bool isCharType() const {
163 return cb.constructed<BufferType<CharT>>();
164 }
165
166 MOZ_ALWAYS_INLINE bool isLatin1() const { return isCharType<Latin1Char>(); }
167
168 MOZ_ALWAYS_INLINE bool isTwoByte() const { return isCharType<char16_t>(); }
169
170 template <typename CharT>
171 MOZ_ALWAYS_INLINE BufferType<CharT>& chars() {
172 MOZ_ASSERT(isCharType<CharT>());
173 return cb.ref<BufferType<CharT>>();
174 }
175
176 template <typename CharT>
177 MOZ_ALWAYS_INLINE const BufferType<CharT>& chars() const {
178 MOZ_ASSERT(isCharType<CharT>());
179 return cb.ref<BufferType<CharT>>();
180 }
181
182 MOZ_ALWAYS_INLINE TwoByteCharBuffer& twoByteChars() {
183 return chars<char16_t>();
184 }
185
186 MOZ_ALWAYS_INLINE const TwoByteCharBuffer& twoByteChars() const {
187 return chars<char16_t>();
188 }
189
190 MOZ_ALWAYS_INLINE Latin1CharBuffer& latin1Chars() {
191 return chars<Latin1Char>();
192 }
193
194 MOZ_ALWAYS_INLINE const Latin1CharBuffer& latin1Chars() const {
195 return chars<Latin1Char>();
196 }
197
198 [[nodiscard]] bool inflateChars();
199
200 template <typename CharT>
201 JSLinearString* finishStringInternal(JSContext* cx, gc::Heap heap);
202
203 public:
204 explicit StringBuilder(JSContext* cx,
205 const arena_id_t& arenaId = js::MallocArena)
206 : maybeCx_(cx) {
207 MOZ_ASSERT(cx);
208 cb.construct<Latin1CharBuffer>(StringBuilderAllocPolicy{cx, arenaId});
209 }
210
211 // This constructor should only be used if the methods related to the
212 // following are not used, because they require a JSContext:
213 // * JSString
214 // * JSAtom
215 // * mozilla::Utf8Unit
216 explicit StringBuilder(FrontendContext* fc,
217 const arena_id_t& arenaId = js::MallocArena) {
218 MOZ_ASSERT(fc);
219 cb.construct<Latin1CharBuffer>(StringBuilderAllocPolicy{fc, arenaId});
220 }
221
222 void clear() { shrinkTo(0); }
223
224 [[nodiscard]] bool reserve(size_t len) {
225 auto lenWithHeader = mozilla::CheckedInt<size_t>(len) + numHeaderChars_;
226 if (MOZ_UNLIKELY(!lenWithHeader.isValid())) {
227 ReportAllocationOverflow(maybeCx_);
228 return false;
229 }
230 if (len > reservedExclHeader_) {
231 reservedExclHeader_ = len;
232 }
233 return isLatin1() ? latin1Chars().reserve(lenWithHeader.value())
234 : twoByteChars().reserve(lenWithHeader.value());
235 }
236 [[nodiscard]] bool growByUninitialized(size_t incr) {
237 return isLatin1() ? latin1Chars().growByUninitialized(incr)
238 : twoByteChars().growByUninitialized(incr);
239 }
240 void shrinkTo(size_t newLength) {
241 // Note: this can't overflow because the new length must be <= the current
242 // length.
243 newLength += numHeaderChars_;
244 if (isLatin1()) {
245 latin1Chars().shrinkTo(newLength);
246 } else {
247 twoByteChars().shrinkTo(newLength);
248 }
249 }
250 bool empty() const { return length() == 0; }
251 size_t length() const {
252 size_t len = isLatin1() ? latin1Chars().length() : twoByteChars().length();
253 MOZ_ASSERT(len >= numHeaderChars_);
254 return len - numHeaderChars_;
255 }
256 char16_t getChar(size_t idx) const {
257 idx += numHeaderChars_;
258 return isLatin1() ? latin1Chars()[idx] : twoByteChars()[idx];
259 }
260
261 [[nodiscard]] bool ensureTwoByteChars() {
262 return isTwoByte() || inflateChars();
263 }
264
265 [[nodiscard]] bool append(const char16_t c) {
266 if (isLatin1()) {
267 if (c <= JSString::MAX_LATIN1_CHAR) {
268 return latin1Chars().append(Latin1Char(c));
269 }
270 if (!inflateChars()) {
271 return false;
272 }
273 }
274 return twoByteChars().append(c);
275 }
276 [[nodiscard]] bool append(Latin1Char c) {
277 return isLatin1() ? latin1Chars().append(c) : twoByteChars().append(c);
278 }
279 [[nodiscard]] bool append(char c) { return append(Latin1Char(c)); }
280
281 [[nodiscard]] inline bool append(const char16_t* begin, const char16_t* end);
282
283 [[nodiscard]] bool append(const char16_t* chars, size_t len) {
284 return append(chars, chars + len);
285 }
286
287 [[nodiscard]] bool append(const Latin1Char* begin, const Latin1Char* end) {
288 return isLatin1() ? latin1Chars().append(begin, end)
289 : twoByteChars().append(begin, end);
290 }
291 [[nodiscard]] bool append(const Latin1Char* chars, size_t len) {
292 return append(chars, chars + len);
293 }
294
295 /**
296 * Interpret the provided count of UTF-8 code units as UTF-8, and append
297 * the represented code points to this. If the code units contain invalid
298 * UTF-8, leave the internal buffer in a consistent but unspecified state,
299 * report an error, and return false.
300 */
301 [[nodiscard]] bool append(const mozilla::Utf8Unit* units, size_t len);
302
303 [[nodiscard]] bool append(const JS::ConstCharPtr chars, size_t len) {
304 return append(chars.get(), chars.get() + len);
305 }
306 [[nodiscard]] bool appendN(Latin1Char c, size_t n) {
307 return isLatin1() ? latin1Chars().appendN(c, n)
308 : twoByteChars().appendN(c, n);
309 }
310
311 [[nodiscard]] inline bool append(JSString* str);
312 [[nodiscard]] inline bool append(const JSLinearString* str);
313 [[nodiscard]] inline bool appendSubstring(JSString* base, size_t off,
314 size_t len);
315 [[nodiscard]] inline bool appendSubstring(const JSLinearString* base,
316 size_t off, size_t len);
317 [[nodiscard]] bool append(const frontend::ParserAtomsTable& parserAtoms,
318 frontend::TaggedParserAtomIndex atom);
319
320 [[nodiscard]] bool append(const char* chars, size_t len) {
321 return append(reinterpret_cast<const Latin1Char*>(chars), len);
322 }
323
324 template <size_t ArrayLength>
325 [[nodiscard]] bool append(const char (&array)[ArrayLength]) {
326 return append(array, ArrayLength - 1); /* No trailing '\0'. */
327 }
328
329 /* Infallible variants usable when the corresponding space is reserved. */
330 void infallibleAppend(Latin1Char c) {
331 if (isLatin1()) {
332 latin1Chars().infallibleAppend(c);
333 } else {
334 twoByteChars().infallibleAppend(c);
335 }
336 }
337 void infallibleAppend(char c) { infallibleAppend(Latin1Char(c)); }
338 void infallibleAppend(const Latin1Char* chars, size_t len) {
339 if (isLatin1()) {
340 latin1Chars().infallibleAppend(chars, len);
341 } else {
342 twoByteChars().infallibleAppend(chars, len);
343 }
344 }
345 void infallibleAppend(const char* chars, size_t len) {
346 infallibleAppend(reinterpret_cast<const Latin1Char*>(chars), len);
347 }
348
349 void infallibleAppendSubstring(const JSLinearString* base, size_t off,
350 size_t len);
351
352 /*
353 * Because inflation is fallible, these methods should only be used after
354 * calling ensureTwoByteChars().
355 */
356 void infallibleAppend(const char16_t* chars, size_t len) {
357 twoByteChars().infallibleAppend(chars, len);
358 }
359 void infallibleAppend(char16_t c) { twoByteChars().infallibleAppend(c); }
360
361 bool isUnderlyingBufferLatin1() const { return isLatin1(); }
362
363 template <typename CharT>
364 CharT* begin() {
365 MOZ_ASSERT(chars<CharT>().length() >= numHeaderChars_);
366 return chars<CharT>().begin() + numHeaderChars_;
367 }
368
369 template <typename CharT>
370 CharT* end() {
371 return chars<CharT>().end();
372 }
373
374 template <typename CharT>
375 const CharT* begin() const {
376 MOZ_ASSERT(chars<CharT>().length() >= numHeaderChars_);
377 return chars<CharT>().begin() + numHeaderChars_;
378 }
379
380 template <typename CharT>
381 const CharT* end() const {
382 return chars<CharT>().end();
383 }
384
385 char16_t* rawTwoByteBegin() { return begin<char16_t>(); }
386 char16_t* rawTwoByteEnd() { return end<char16_t>(); }
387 const char16_t* rawTwoByteBegin() const { return begin<char16_t>(); }
388 const char16_t* rawTwoByteEnd() const { return end<char16_t>(); }
389
390 Latin1Char* rawLatin1Begin() { return begin<Latin1Char>(); }
391 Latin1Char* rawLatin1End() { return end<Latin1Char>(); }
392 const Latin1Char* rawLatin1Begin() const { return begin<Latin1Char>(); }
393 const Latin1Char* rawLatin1End() const { return end<Latin1Char>(); }
394
395 /* Identical to finishString() except that an atom is created. */
396 JSAtom* finishAtom();
397 frontend::TaggedParserAtomIndex finishParserAtom(
398 frontend::ParserAtomsTable& parserAtoms, FrontendContext* fc);
399
400 /*
401 * Creates a raw string from the characters in this buffer. The string is
402 * exactly the characters in this buffer (inflated to TwoByte), it is *not*
403 * null-terminated unless the last appended character was '\0'.
404 */
405 char16_t* stealChars();
406 };
407
408 // Like StringBuilder, but uses StringBufferArena for the characters.
409 class JSStringBuilder : public StringBuilder {
410 public:
411 explicit JSStringBuilder(JSContext* cx)
412 : StringBuilder(cx, js::StringBufferArena) {
413 // Reserve space for the mozilla::StringBuffer header.
414 numHeaderChars_ = numHeaderChars<Latin1Char>();
415 MOZ_ALWAYS_TRUE(latin1Chars().appendN('\0', numHeaderChars_));
416 }
417
418 /*
419 * Creates a string from the characters in this buffer, then (regardless
420 * whether string creation succeeded or failed) empties the buffer.
421 *
422 * Returns nullptr if string creation failed.
423 */
424 JSLinearString* finishString(gc::Heap heap = gc::Heap::Default);
425 };
426
427 inline bool StringBuilder::append(const char16_t* begin, const char16_t* end) {
428 MOZ_ASSERT(begin <= end);
429 if (isLatin1()) {
430 while (true) {
431 if (begin >= end) {
432 return true;
433 }
434 if (*begin > JSString::MAX_LATIN1_CHAR) {
435 break;
436 }
437 if (!latin1Chars().append(*begin)) {
438 return false;
439 }
440 ++begin;
441 }
442 if (!inflateChars()) {
443 return false;
444 }
445 }
446 return twoByteChars().append(begin, end);
447 }
448
449 inline bool StringBuilder::append(const JSLinearString* str) {
450 JS::AutoCheckCannotGC nogc;
451 if (isLatin1()) {
452 if (str->hasLatin1Chars()) {
453 return latin1Chars().append(str->latin1Chars(nogc), str->length());
454 }
455 if (!inflateChars()) {
456 return false;
457 }
458 }
459 return str->hasLatin1Chars()
460 ? twoByteChars().append(str->latin1Chars(nogc), str->length())
461 : twoByteChars().append(str->twoByteChars(nogc), str->length());
462 }
463
464 inline void StringBuilder::infallibleAppendSubstring(const JSLinearString* base,
465 size_t off, size_t len) {
466 MOZ_ASSERT(off + len <= base->length());
467 MOZ_ASSERT_IF(base->hasTwoByteChars(), isTwoByte());
468
469 JS::AutoCheckCannotGC nogc;
470 if (base->hasLatin1Chars()) {
471 infallibleAppend(base->latin1Chars(nogc) + off, len);
472 } else {
473 infallibleAppend(base->twoByteChars(nogc) + off, len);
474 }
475 }
476
477 inline bool StringBuilder::appendSubstring(const JSLinearString* base,
478 size_t off, size_t len) {
479 MOZ_ASSERT(off + len <= base->length());
480
481 JS::AutoCheckCannotGC nogc;
482 if (isLatin1()) {
483 if (base->hasLatin1Chars()) {
484 return latin1Chars().append(base->latin1Chars(nogc) + off, len);
485 }
486 if (!inflateChars()) {
487 return false;
488 }
489 }
490 return base->hasLatin1Chars()
491 ? twoByteChars().append(base->latin1Chars(nogc) + off, len)
492 : twoByteChars().append(base->twoByteChars(nogc) + off, len);
493 }
494
495 inline bool StringBuilder::appendSubstring(JSString* base, size_t off,
496 size_t len) {
497 MOZ_ASSERT(maybeCx_);
498
499 JSLinearString* linear = base->ensureLinear(maybeCx_);
500 if (!linear) {
501 return false;
502 }
503
504 return appendSubstring(linear, off, len);
505 }
506
507 inline bool StringBuilder::append(JSString* str) {
508 MOZ_ASSERT(maybeCx_);
509
510 JSLinearString* linear = str->ensureLinear(maybeCx_);
511 if (!linear) {
512 return false;
513 }
514
515 return append(linear);
516 }
517
518 /* ES5 9.8 ToString, appending the result to the string builder. */
519 extern bool ValueToStringBuilderSlow(JSContext* cx, const Value& v,
520 StringBuilder& sb);
521
522 inline bool ValueToStringBuilder(JSContext* cx, const Value& v,
523 StringBuilder& sb) {
524 if (v.isString()) {
525 return sb.append(v.toString());
526 }
527
528 return ValueToStringBuilderSlow(cx, v, sb);
529 }
530
531 /* ES5 9.8 ToString for booleans, appending the result to the string builder. */
532 inline bool BooleanToStringBuilder(bool b, StringBuilder& sb) {
533 return b ? sb.append("true") : sb.append("false");
534 }
535
536 } /* namespace js */
537
538 #endif /* util_StringBuilder_h */