summaryrefslogtreecommitdiffstats
path: root/js/src/vm/Printer.cpp
blob: 1cfdca01a41d21726e7e236ba33faa79e81ba116 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "js/Printer.h"

#include "mozilla/PodOperations.h"
#include "mozilla/Printf.h"
#include "mozilla/RangedPtr.h"

#include <stdarg.h>
#include <stdio.h>

#include "ds/LifoAlloc.h"
#include "js/CharacterEncoding.h"
#include "util/Memory.h"
#include "util/Text.h"
#include "util/WindowsWrapper.h"
#include "vm/StringType.h"

using mozilla::PodCopy;

namespace {

class GenericPrinterPrintfTarget : public mozilla::PrintfTarget {
 public:
  explicit GenericPrinterPrintfTarget(js::GenericPrinter& p) : printer(p) {}

  bool append(const char* sp, size_t len) override {
    return printer.put(sp, len);
  }

 private:
  js::GenericPrinter& printer;
};

}  // namespace

namespace js {

void GenericPrinter::reportOutOfMemory() {
  if (hadOOM_) {
    return;
  }
  hadOOM_ = true;
}

bool GenericPrinter::hadOutOfMemory() const { return hadOOM_; }

bool GenericPrinter::printf(const char* fmt, ...) {
  va_list va;
  va_start(va, fmt);
  bool r = vprintf(fmt, va);
  va_end(va);
  return r;
}

bool GenericPrinter::vprintf(const char* fmt, va_list ap) {
  // Simple shortcut to avoid allocating strings.
  if (strchr(fmt, '%') == nullptr) {
    return put(fmt);
  }

  GenericPrinterPrintfTarget printer(*this);
  if (!printer.vprint(fmt, ap)) {
    reportOutOfMemory();
    return false;
  }
  return true;
}

const size_t Sprinter::DefaultSize = 64;

bool Sprinter::realloc_(size_t newSize) {
  MOZ_ASSERT(newSize > (size_t)offset);
  char* newBuf = (char*)js_realloc(base, newSize);
  if (!newBuf) {
    reportOutOfMemory();
    return false;
  }
  base = newBuf;
  size = newSize;
  base[size - 1] = '\0';
  return true;
}

Sprinter::Sprinter(JSContext* maybeCx, bool shouldReportOOM)
    : maybeCx(maybeCx),
#ifdef DEBUG
      initialized(false),
#endif
      shouldReportOOM(maybeCx && shouldReportOOM),
      base(nullptr),
      size(0),
      offset(0) {
}

Sprinter::~Sprinter() {
#ifdef DEBUG
  if (initialized) {
    checkInvariants();
  }
#endif
  js_free(base);
}

bool Sprinter::init() {
  MOZ_ASSERT(!initialized);
  base = js_pod_malloc<char>(DefaultSize);
  if (!base) {
    reportOutOfMemory();
    return false;
  }
#ifdef DEBUG
  initialized = true;
#endif
  *base = '\0';
  size = DefaultSize;
  base[size - 1] = '\0';
  return true;
}

void Sprinter::checkInvariants() const {
  MOZ_ASSERT(initialized);
  MOZ_ASSERT((size_t)offset < size);
  MOZ_ASSERT(base[size - 1] == '\0');
}

UniqueChars Sprinter::release() {
  checkInvariants();
  if (hadOOM_) {
    return nullptr;
  }

  char* str = base;
  base = nullptr;
  offset = size = 0;
#ifdef DEBUG
  initialized = false;
#endif
  return UniqueChars(str);
}

char* Sprinter::stringAt(ptrdiff_t off) const {
  MOZ_ASSERT(off >= 0 && (size_t)off < size);
  return base + off;
}

char& Sprinter::operator[](size_t off) {
  MOZ_ASSERT(off < size);
  return *(base + off);
}

char* Sprinter::reserve(size_t len) {
  InvariantChecker ic(this);

  while (len + 1 > size - offset) { /* Include trailing \0 */
    if (!realloc_(size * 2)) {
      return nullptr;
    }
  }

  char* sb = base + offset;
  offset += len;
  return sb;
}

bool Sprinter::put(const char* s, size_t len) {
  InvariantChecker ic(this);

  const char* oldBase = base;
  const char* oldEnd = base + size;

  char* bp = reserve(len);
  if (!bp) {
    return false;
  }

  /* s is within the buffer already */
  if (s >= oldBase && s < oldEnd) {
    /* buffer was realloc'ed */
    if (base != oldBase) {
      s = stringAt(s - oldBase); /* this is where it lives now */
    }
    memmove(bp, s, len);
  } else {
    js_memcpy(bp, s, len);
  }

  bp[len] = '\0';
  return true;
}

bool Sprinter::putString(JSString* s) {
  MOZ_ASSERT(maybeCx);
  InvariantChecker ic(this);

  JSLinearString* linear = s->ensureLinear(maybeCx);
  if (!linear) {
    return false;
  }

  size_t length = JS::GetDeflatedUTF8StringLength(linear);

  char* buffer = reserve(length);
  if (!buffer) {
    return false;
  }

  mozilla::DebugOnly<size_t> written =
      JS::DeflateStringToUTF8Buffer(linear, mozilla::Span(buffer, length));
  MOZ_ASSERT(written == length);

  buffer[length] = '\0';
  return true;
}

ptrdiff_t Sprinter::getOffset() const { return offset; }

void Sprinter::reportOutOfMemory() {
  if (hadOOM_) {
    return;
  }
  if (maybeCx && shouldReportOOM) {
    ReportOutOfMemory(maybeCx);
  }
  hadOOM_ = true;
}

bool Sprinter::jsprintf(const char* format, ...) {
  va_list ap;
  va_start(ap, format);

  bool r = vprintf(format, ap);
  va_end(ap);

  return r;
}

const char js_EscapeMap[] = {
    // clang-format off
    '\b', 'b',
    '\f', 'f',
    '\n', 'n',
    '\r', 'r',
    '\t', 't',
    '\v', 'v',
    '"',  '"',
    '\'', '\'',
    '\\', '\\',
    '\0'
    // clang-format on
};

static const char JSONEscapeMap[] = {
    // clang-format off
    '\b', 'b',
    '\f', 'f',
    '\n', 'n',
    '\r', 'r',
    '\t', 't',
    '"',  '"',
    '\\', '\\',
    '\0'
    // clang-format on
};

template <QuoteTarget target, typename CharT>
JS_PUBLIC_API bool QuoteString(Sprinter* sp,
                               const mozilla::Range<const CharT> chars,
                               char quote) {
  MOZ_ASSERT_IF(target == QuoteTarget::JSON, quote == '\0');

  using CharPtr = mozilla::RangedPtr<const CharT>;

  const char* escapeMap =
      (target == QuoteTarget::String) ? js_EscapeMap : JSONEscapeMap;

  if (quote) {
    if (!sp->putChar(quote)) {
      return false;
    }
  }

  const CharPtr end = chars.end();

  /* Loop control variables: end points at end of string sentinel. */
  for (CharPtr t = chars.begin(); t < end; ++t) {
    /* Move t forward from s past un-quote-worthy characters. */
    const CharPtr s = t;
    char16_t c = *t;
    while (c < 127 && c != '\\') {
      if (target == QuoteTarget::String) {
        if (!IsAsciiPrintable(c) || c == quote || c == '\t') {
          break;
        }
      } else {
        if (c < ' ' || c == '"') {
          break;
        }
      }

      ++t;
      if (t == end) {
        break;
      }
      c = *t;
    }

    {
      ptrdiff_t len = t - s;
      ptrdiff_t base = sp->getOffset();
      if (!sp->reserve(len)) {
        return false;
      }

      for (ptrdiff_t i = 0; i < len; ++i) {
        (*sp)[base + i] = char(s[i]);
      }
      (*sp)[base + len] = '\0';
    }

    if (t == end) {
      break;
    }

    /* Use escapeMap, \u, or \x only if necessary. */
    const char* escape;
    if (!(c >> 8) && c != 0 &&
        (escape = strchr(escapeMap, int(c))) != nullptr) {
      if (!sp->jsprintf("\\%c", escape[1])) {
        return false;
      }
    } else {
      /*
       * Use \x only if the high byte is 0 and we're in a quoted string,
       * because ECMA-262 allows only \u, not \x, in Unicode identifiers
       * (see bug 621814).
       */
      if (!sp->jsprintf((quote && !(c >> 8)) ? "\\x%02X" : "\\u%04X", c)) {
        return false;
      }
    }
  }

  /* Sprint the closing quote and return the quoted string. */
  if (quote) {
    if (!sp->putChar(quote)) {
      return false;
    }
  }

  return true;
}

template JS_PUBLIC_API bool QuoteString<QuoteTarget::String, Latin1Char>(
    Sprinter* sp, const mozilla::Range<const Latin1Char> chars, char quote);

template JS_PUBLIC_API bool QuoteString<QuoteTarget::String, char16_t>(
    Sprinter* sp, const mozilla::Range<const char16_t> chars, char quote);

template JS_PUBLIC_API bool QuoteString<QuoteTarget::JSON, Latin1Char>(
    Sprinter* sp, const mozilla::Range<const Latin1Char> chars, char quote);

template JS_PUBLIC_API bool QuoteString<QuoteTarget::JSON, char16_t>(
    Sprinter* sp, const mozilla::Range<const char16_t> chars, char quote);

JS_PUBLIC_API bool QuoteString(Sprinter* sp, JSString* str,
                               char quote /*= '\0' */) {
  MOZ_ASSERT(sp->maybeCx);
  JSLinearString* linear = str->ensureLinear(sp->maybeCx);
  if (!linear) {
    return false;
  }

  JS::AutoCheckCannotGC nogc;
  return linear->hasLatin1Chars() ? QuoteString<QuoteTarget::String>(
                                        sp, linear->latin1Range(nogc), quote)
                                  : QuoteString<QuoteTarget::String>(
                                        sp, linear->twoByteRange(nogc), quote);
}

JS_PUBLIC_API UniqueChars QuoteString(JSContext* cx, JSString* str,
                                      char quote /* = '\0' */) {
  Sprinter sprinter(cx);
  if (!sprinter.init()) {
    return nullptr;
  }
  if (!QuoteString(&sprinter, str, quote)) {
    return nullptr;
  }
  return sprinter.release();
}

JS_PUBLIC_API bool JSONQuoteString(Sprinter* sp, JSString* str) {
  MOZ_ASSERT(sp->maybeCx);
  JSLinearString* linear = str->ensureLinear(sp->maybeCx);
  if (!linear) {
    return false;
  }

  JS::AutoCheckCannotGC nogc;
  return linear->hasLatin1Chars() ? QuoteString<QuoteTarget::JSON>(
                                        sp, linear->latin1Range(nogc), '\0')
                                  : QuoteString<QuoteTarget::JSON>(
                                        sp, linear->twoByteRange(nogc), '\0');
}

Fprinter::Fprinter(FILE* fp) : file_(nullptr), init_(false) { init(fp); }

#ifdef DEBUG
Fprinter::~Fprinter() { MOZ_ASSERT_IF(init_, !file_); }
#endif

bool Fprinter::init(const char* path) {
  MOZ_ASSERT(!file_);
  file_ = fopen(path, "w");
  if (!file_) {
    return false;
  }
  init_ = true;
  return true;
}

void Fprinter::init(FILE* fp) {
  MOZ_ASSERT(!file_);
  file_ = fp;
  init_ = false;
}

void Fprinter::flush() {
  MOZ_ASSERT(file_);
  fflush(file_);
}

void Fprinter::finish() {
  MOZ_ASSERT(file_);
  if (init_) {
    fclose(file_);
  }
  file_ = nullptr;
}

bool Fprinter::put(const char* s, size_t len) {
  MOZ_ASSERT(file_);
  int i = fwrite(s, /*size=*/1, /*nitems=*/len, file_);
  if (size_t(i) != len) {
    reportOutOfMemory();
    return false;
  }
#ifdef XP_WIN
  if ((file_ == stderr) && (IsDebuggerPresent())) {
    UniqueChars buf = DuplicateString(s, len);
    if (!buf) {
      reportOutOfMemory();
      return false;
    }
    OutputDebugStringA(buf.get());
  }
#endif
  return true;
}

LSprinter::LSprinter(LifoAlloc* lifoAlloc)
    : alloc_(lifoAlloc), head_(nullptr), tail_(nullptr), unused_(0) {}

LSprinter::~LSprinter() {
  // This LSprinter might be allocated as part of the same LifoAlloc, so we
  // should not expect the destructor to be called.
}

void LSprinter::exportInto(GenericPrinter& out) const {
  if (!head_) {
    return;
  }

  for (Chunk* it = head_; it != tail_; it = it->next) {
    out.put(it->chars(), it->length);
  }
  out.put(tail_->chars(), tail_->length - unused_);
}

void LSprinter::clear() {
  head_ = nullptr;
  tail_ = nullptr;
  unused_ = 0;
  hadOOM_ = false;
}

bool LSprinter::put(const char* s, size_t len) {
  // Compute how much data will fit in the current chunk.
  size_t existingSpaceWrite = 0;
  size_t overflow = len;
  if (unused_ > 0 && tail_) {
    existingSpaceWrite = std::min(unused_, len);
    overflow = len - existingSpaceWrite;
  }

  // If necessary, allocate a new chunk for overflow data.
  size_t allocLength = 0;
  Chunk* last = nullptr;
  if (overflow > 0) {
    allocLength =
        AlignBytes(sizeof(Chunk) + overflow, js::detail::LIFO_ALLOC_ALIGN);

    LifoAlloc::AutoFallibleScope fallibleAllocator(alloc_);
    last = reinterpret_cast<Chunk*>(alloc_->alloc(allocLength));
    if (!last) {
      reportOutOfMemory();
      return false;
    }
  }

  // All fallible operations complete: now fill up existing space, then
  // overflow space in any new chunk.
  MOZ_ASSERT(existingSpaceWrite + overflow == len);

  if (existingSpaceWrite > 0) {
    PodCopy(tail_->end() - unused_, s, existingSpaceWrite);
    unused_ -= existingSpaceWrite;
    s += existingSpaceWrite;
  }

  if (overflow > 0) {
    if (tail_ && reinterpret_cast<char*>(last) == tail_->end()) {
      // tail_ and last are consecutive in memory.  LifoAlloc has no
      // metadata and is just a bump allocator, so we can cheat by
      // appending the newly-allocated space to tail_.
      unused_ = allocLength;
      tail_->length += allocLength;
    } else {
      // Remove the size of the header from the allocated length.
      size_t availableSpace = allocLength - sizeof(Chunk);
      last->next = nullptr;
      last->length = availableSpace;

      unused_ = availableSpace;
      if (!head_) {
        head_ = last;
      } else {
        tail_->next = last;
      }

      tail_ = last;
    }

    PodCopy(tail_->end() - unused_, s, overflow);

    MOZ_ASSERT(unused_ >= overflow);
    unused_ -= overflow;
  }

  MOZ_ASSERT(len <= INT_MAX);
  return true;
}

}  // namespace js