summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testCompileUtf8.cpp
blob: 8799e7020196963314a0e3d9ecf42c12928dc856 (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
/* 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 "mozilla/ArrayUtils.h"
#include "mozilla/TextUtils.h"
#include "mozilla/Utf8.h"

#include <cstring>

#include "js/CharacterEncoding.h"
#include "js/CompilationAndEvaluation.h"  // JS::Compile
#include "js/Exception.h"
#include "js/friend/ErrorMessages.h"  // JSMSG_*
#include "js/SourceText.h"
#include "jsapi-tests/tests.h"
#include "util/Text.h"
#include "vm/ErrorReporting.h"

using mozilla::ArrayEqual;
using mozilla::IsAsciiHexDigit;
using mozilla::Utf8Unit;

BEGIN_TEST(testUtf8BadBytes) {
  static const char badLeadingUnit[] = "var x = \x80";
  CHECK(testBadUtf8(
      badLeadingUnit, JSMSG_BAD_LEADING_UTF8_UNIT,
      [this](JS::ConstUTF8CharsZ message) {
        const char* chars = message.c_str();
        CHECK(startsWith(chars, "0x80"));
        CHECK(isBadLeadUnitMessage(chars));
        return true;
      },
      "0x80"));

  static const char badSecondInTwoByte[] = "var x = \xDF\x20";
  CHECK(testBadUtf8(
      badSecondInTwoByte, JSMSG_BAD_TRAILING_UTF8_UNIT,
      [this](JS::ConstUTF8CharsZ message) {
        const char* chars = message.c_str();
        CHECK(isBadTrailingBytesMessage(chars));
        CHECK(contains(chars, "0x20"));
        return true;
      },
      "0xDF 0x20"));

  static const char badSecondInThreeByte[] = "var x = \xEF\x17\xA7";
  CHECK(testBadUtf8(
      badSecondInThreeByte, JSMSG_BAD_TRAILING_UTF8_UNIT,
      [this](JS::ConstUTF8CharsZ message) {
        const char* chars = message.c_str();
        CHECK(isBadTrailingBytesMessage(chars));
        CHECK(contains(chars, "0x17"));
        return true;
      },
      // Validating stops with the first invalid code unit and
      // shouldn't go beyond that.
      "0xEF 0x17"));

  static const char lengthTwoTooShort[] = "var x = \xDF";
  CHECK(testBadUtf8(
      lengthTwoTooShort, JSMSG_NOT_ENOUGH_CODE_UNITS,
      [this](JS::ConstUTF8CharsZ message) {
        const char* chars = message.c_str();
        CHECK(isNotEnoughUnitsMessage(chars));
        CHECK(contains(chars, "0xDF"));
        CHECK(contains(chars, " 1 byte, but 0 bytes were present"));
        return true;
      },
      "0xDF"));

  static const char forbiddenHighSurrogate[] = "var x = \xED\xA2\x87";
  CHECK(testBadUtf8(
      forbiddenHighSurrogate, JSMSG_FORBIDDEN_UTF8_CODE_POINT,
      [this](JS::ConstUTF8CharsZ message) {
        const char* chars = message.c_str();
        CHECK(isSurrogateMessage(chars));
        CHECK(contains(chars, "0xD887"));
        return true;
      },
      "0xED 0xA2 0x87"));

  static const char forbiddenLowSurrogate[] = "var x = \xED\xB7\xAF";
  CHECK(testBadUtf8(
      forbiddenLowSurrogate, JSMSG_FORBIDDEN_UTF8_CODE_POINT,
      [this](JS::ConstUTF8CharsZ message) {
        const char* chars = message.c_str();
        CHECK(isSurrogateMessage(chars));
        CHECK(contains(chars, "0xDDEF"));
        return true;
      },
      "0xED 0xB7 0xAF"));

  static const char oneTooBig[] = "var x = \xF4\x90\x80\x80";
  CHECK(testBadUtf8(
      oneTooBig, JSMSG_FORBIDDEN_UTF8_CODE_POINT,
      [this](JS::ConstUTF8CharsZ message) {
        const char* chars = message.c_str();
        CHECK(isTooBigMessage(chars));
        CHECK(contains(chars, "0x110000"));
        return true;
      },
      "0xF4 0x90 0x80 0x80"));

  static const char notShortestFormZero[] = "var x = \xC0\x80";
  CHECK(testBadUtf8(
      notShortestFormZero, JSMSG_FORBIDDEN_UTF8_CODE_POINT,
      [this](JS::ConstUTF8CharsZ message) {
        const char* chars = message.c_str();
        CHECK(isNotShortestFormMessage(chars));
        CHECK(startsWith(chars, "0x0 isn't "));
        return true;
      },
      "0xC0 0x80"));

  static const char notShortestFormNonzero[] = "var x = \xE0\x87\x80";
  CHECK(testBadUtf8(
      notShortestFormNonzero, JSMSG_FORBIDDEN_UTF8_CODE_POINT,
      [this](JS::ConstUTF8CharsZ message) {
        const char* chars = message.c_str();
        CHECK(isNotShortestFormMessage(chars));
        CHECK(startsWith(chars, "0x1C0 isn't "));
        return true;
      },
      "0xE0 0x87 0x80"));

  return true;
}

static constexpr size_t LengthOfByte = js_strlen("0xFF");

static bool startsWithByte(const char* str) {
  return str[0] == '0' && str[1] == 'x' && IsAsciiHexDigit(str[2]) &&
         IsAsciiHexDigit(str[3]);
}

static bool startsWith(const char* str, const char* prefix) {
  return std::strncmp(prefix, str, strlen(prefix)) == 0;
}

static bool contains(const char* str, const char* substr) {
  return std::strstr(str, substr) != nullptr;
}

static bool equals(const char* str, const char* expected) {
  return std::strcmp(str, expected) == 0;
}

static bool isBadLeadUnitMessage(const char* str) {
  return startsWithByte(str) &&
         equals(str + LengthOfByte,
                " byte doesn't begin a valid UTF-8 code point");
}

static bool isBadTrailingBytesMessage(const char* str) {
  return startsWith(str, "bad trailing UTF-8 byte ");
}

static bool isNotEnoughUnitsMessage(const char* str) {
  return startsWithByte(str) &&
         startsWith(str + LengthOfByte, " byte in UTF-8 must be followed by ");
}

static bool isForbiddenCodePointMessage(const char* str) {
  return contains(str, "isn't a valid code point because");
}

static bool isSurrogateMessage(const char* str) {
  return isForbiddenCodePointMessage(str) &&
         contains(str, " it's a UTF-16 surrogate");
}

static bool isTooBigMessage(const char* str) {
  return isForbiddenCodePointMessage(str) &&
         contains(str, "the maximum code point is U+10FFFF");
}

static bool isNotShortestFormMessage(const char* str) {
  return isForbiddenCodePointMessage(str) &&
         contains(str, "it wasn't encoded in shortest possible form");
}

template <size_t N, typename TestMessage>
bool testBadUtf8(const char (&chars)[N], unsigned errorNumber,
                 TestMessage testMessage, const char* badBytes) {
  JS::Rooted<JSScript*> script(cx);
  {
    JS::CompileOptions options(cx);

    JS::SourceText<mozilla::Utf8Unit> srcBuf;
    CHECK(srcBuf.init(cx, chars, N - 1, JS::SourceOwnership::Borrowed));

    script = JS::Compile(cx, options, srcBuf);
    CHECK(!script);
  }

  JS::ExceptionStack exnStack(cx);
  CHECK(JS::StealPendingExceptionStack(cx, &exnStack));

  JS::ErrorReportBuilder report(cx);
  CHECK(report.init(cx, exnStack, JS::ErrorReportBuilder::WithSideEffects));

  const auto* errorReport = report.report();

  CHECK(errorReport->errorNumber == errorNumber);

  CHECK(testMessage(errorReport->message()));

  {
    const auto& notes = errorReport->notes;
    CHECK(notes != nullptr);

    auto iter = notes->begin();
    CHECK(iter != notes->end());

    const char* noteMessage = (*iter)->message().c_str();

    // The prefix ought always be the same.
    static constexpr char expectedPrefix[] =
        "the code units comprising this invalid code point were: ";
    constexpr size_t expectedPrefixLen = js_strlen(expectedPrefix);

    CHECK(startsWith(noteMessage, expectedPrefix));

    // The end of the prefix is the bad bytes.
    CHECK(equals(noteMessage + expectedPrefixLen, badBytes));

    ++iter;
    CHECK(iter == notes->end());
  }

  static constexpr char16_t expectedContext[] = u"var x = ";
  constexpr size_t expectedContextLen = js_strlen(expectedContext);

  const char16_t* lineOfContext = errorReport->linebuf();
  size_t lineOfContextLength = errorReport->linebufLength();

  CHECK(lineOfContext[lineOfContextLength] == '\0');
  CHECK(lineOfContextLength == expectedContextLen);

  CHECK(std::memcmp(lineOfContext, expectedContext,
                    expectedContextLen * sizeof(char16_t)) == 0);

  return true;
}
END_TEST(testUtf8BadBytes)

BEGIN_TEST(testMultiUnitUtf8InWindow) {
  static const char firstInWindowIsMultiUnit[] =
      "\xCF\x80\xCF\x80 = 6.283185307; @ bad starts HERE:\x80\xFF\xFF";
  CHECK(testContext(firstInWindowIsMultiUnit,
                    u"ππ = 6.283185307; @ bad starts HERE:"));

  static const char atTokenOffsetIsMulti[] = "var z = 💯";
  CHECK(testContext(atTokenOffsetIsMulti, u"var z = 💯"));

  static const char afterTokenOffsetIsMulti[] = "var z = @💯💯💯X";
  CHECK(testContext(afterTokenOffsetIsMulti, u"var z = @💯💯💯X"));

  static const char atEndIsMulti[] = "var z = @@💯💯💯";
  CHECK(testContext(atEndIsMulti, u"var z = @@💯💯💯"));

  return true;
}

template <size_t N, size_t ContextLenWithNull>
bool testContext(const char (&chars)[N],
                 const char16_t (&expectedContext)[ContextLenWithNull]) {
  JS::Rooted<JSScript*> script(cx);
  {
    JS::CompileOptions options(cx);

    JS::SourceText<mozilla::Utf8Unit> srcBuf;
    CHECK(srcBuf.init(cx, chars, N - 1, JS::SourceOwnership::Borrowed));

    script = JS::Compile(cx, options, srcBuf);
    CHECK(!script);
  }

  JS::ExceptionStack exnStack(cx);
  CHECK(JS::StealPendingExceptionStack(cx, &exnStack));

  JS::ErrorReportBuilder report(cx);
  CHECK(report.init(cx, exnStack, JS::ErrorReportBuilder::WithSideEffects));

  const auto* errorReport = report.report();

  CHECK(errorReport->errorNumber == JSMSG_ILLEGAL_CHARACTER);

  const char16_t* lineOfContext = errorReport->linebuf();
  size_t lineOfContextLength = errorReport->linebufLength();

  CHECK(lineOfContext[lineOfContextLength] == '\0');
  CHECK(lineOfContextLength == ContextLenWithNull - 1);

  CHECK(ArrayEqual(lineOfContext, expectedContext, ContextLenWithNull));

  return true;
}
END_TEST(testMultiUnitUtf8InWindow)