summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testFrontendErrors.cpp
blob: 47acf559d79e9e28daa6c3d7e068231a080a7dcb (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
/* -*- 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 "mozilla/RefPtr.h"  // RefPtr
#include "mozilla/Utf8.h"    // mozilla::Utf8Unit

#include <string.h>  // strcmp

#include "frontend/FrontendContext.h"  // js::FrontendContext
#include "js/AllocPolicy.h"            // js::ReportOutOfMemory
#include "js/CompileOptions.h"  // JS::PrefableCompileOptions, JS::CompileOptions
#include "js/Exception.h"  // JS_IsExceptionPending, JS_IsThrowingOutOfMemory, JS_GetPendingException, JS_ClearPendingException, JS_ErrorFromException
#include "js/experimental/CompileScript.h"  // JS::NewFrontendContext, JS::DestroyFrontendContext, JS::SetNativeStackQuota, JS::CompileGlobalScriptToStencil, JS::ConvertFrontendErrorsToRuntimeErrors, JS::HadFrontendErrors, JS::HadFrontendOverRecursed, JS::HadFrontendOutOfMemory, JS::HadFrontendAllocationOverflow, JS::GetFrontendWarningCount, JS::GetFrontendWarningAt
#include "js/friend/ErrorMessages.h"        // JSMSG_*
#include "js/friend/StackLimits.h"          // js::ReportOverRecursed
#include "js/RootingAPI.h"                  // JS::Rooted
#include "js/SourceText.h"                  // JS::SourceText
#include "js/Stack.h"                       // JS::NativeStackSize
#include "js/Warnings.h"                    // JS::SetWarningReporter
#include "jsapi-tests/tests.h"
#include "util/NativeStack.h"  // js::GetNativeStackBase
#include "vm/ErrorObject.h"    // js::ErrorObject
#include "vm/JSContext.h"      // JSContext
#include "vm/JSObject.h"       // JSObject
#include "vm/NativeObject.h"   // js::NativeObject
#include "vm/Runtime.h"        // js::ReportAllocationOverflow

using namespace JS;

BEGIN_TEST(testFrontendErrors_error) {
  JS::FrontendContext* fc = JS::NewFrontendContext();
  CHECK(fc);

  static constexpr JS::NativeStackSize stackSize = 128 * sizeof(size_t) * 1024;

  JS::SetNativeStackQuota(fc, stackSize);

  JS::PrefableCompileOptions prefableOptions;
  JS::CompileOptions options(prefableOptions);
  const char* filename = "testFrontendErrors_error.js";
  options.setFile(filename);

  CHECK(!JS::HadFrontendErrors(fc));

  {
    const char source[] = "syntax error";

    JS::SourceText<mozilla::Utf8Unit> srcBuf;
    CHECK(
        srcBuf.init(fc, source, strlen(source), JS::SourceOwnership::Borrowed));
    RefPtr<JS::Stencil> stencil =
        JS::CompileGlobalScriptToStencil(fc, options, srcBuf);
    CHECK(!stencil);
  }

  CHECK(JS::HadFrontendErrors(fc));
  CHECK(!JS::HadFrontendOverRecursed(fc));
  CHECK(!JS::HadFrontendOutOfMemory(fc));
  CHECK(!JS::HadFrontendAllocationOverflow(fc));
  CHECK(JS::GetFrontendWarningCount(fc) == 0);

  {
    const JSErrorReport* report = JS::GetFrontendErrorReport(fc, options);
    CHECK(report);

    CHECK(report->errorNumber == JSMSG_UNEXPECTED_TOKEN_NO_EXPECT);
    // FrontendContext's error report borrows the filename.
    CHECK(report->filename.c_str() == filename);
  }

  CHECK(!JS_IsExceptionPending(cx));

  JS::SetWarningReporter(cx, warningReporter);

  bool result = JS::ConvertFrontendErrorsToRuntimeErrors(cx, fc, options);
  CHECK(result);

  CHECK(JS_IsExceptionPending(cx));
  CHECK(!warningReporterCalled);
  CHECK(!JS_IsThrowingOutOfMemory(cx));

  {
    JS::Rooted<JS::Value> exception(cx);
    CHECK(JS_GetPendingException(cx, &exception));

    CHECK(exception.isObject());
    JS::Rooted<JSObject*> exceptionObj(cx, &exception.toObject());

    const JSErrorReport* report = JS_ErrorFromException(cx, exceptionObj);
    CHECK(report);

    CHECK(report->errorNumber == JSMSG_UNEXPECTED_TOKEN_NO_EXPECT);
    // Runtime's error report doesn't borrow the filename.
    CHECK(report->filename.c_str() != filename);
    CHECK(strcmp(report->filename.c_str(), filename) == 0);
  }

  JS_ClearPendingException(cx);

  JS::DestroyFrontendContext(fc);

  return true;
}

static bool warningReporterCalled;

static void warningReporter(JSContext* cx, JSErrorReport* report) {
  warningReporterCalled = true;
}
END_TEST(testFrontendErrors_error)

/* static */ bool cls_testFrontendErrors_error::warningReporterCalled = false;

BEGIN_TEST(testFrontendErrors_warning) {
  JS::FrontendContext* fc = JS::NewFrontendContext();
  CHECK(fc);

  static constexpr JS::NativeStackSize stackSize = 128 * sizeof(size_t) * 1024;

  JS::SetNativeStackQuota(fc, stackSize);

  JS::PrefableCompileOptions prefableOptions;
  JS::CompileOptions options(prefableOptions);
  options.setFile(filename);

  {
    const char source[] = "function f() { return; f(); }";

    JS::SourceText<mozilla::Utf8Unit> srcBuf;
    CHECK(
        srcBuf.init(fc, source, strlen(source), JS::SourceOwnership::Borrowed));
    RefPtr<JS::Stencil> stencil =
        JS::CompileGlobalScriptToStencil(fc, options, srcBuf);
    CHECK(stencil);
  }

  CHECK(!JS::HadFrontendErrors(fc));
  CHECK(!JS::HadFrontendOverRecursed(fc));
  CHECK(!JS::HadFrontendOutOfMemory(fc));
  CHECK(!JS::HadFrontendAllocationOverflow(fc));
  CHECK(JS::GetFrontendWarningCount(fc) == 1);

  {
    const JSErrorReport* report = JS::GetFrontendWarningAt(fc, 0, options);
    CHECK(report);

    CHECK(report->errorNumber == JSMSG_STMT_AFTER_RETURN);
    // FrontendContext's error report borrows the filename.
    CHECK(report->filename.c_str() == filename);
  }

  CHECK(!JS_IsExceptionPending(cx));

  JS::SetWarningReporter(cx, warningReporter);

  bool result = JS::ConvertFrontendErrorsToRuntimeErrors(cx, fc, options);
  CHECK(result);

  CHECK(!JS_IsExceptionPending(cx));
  CHECK(warningReporterCalled);
  CHECK(!JS_IsThrowingOutOfMemory(cx));

  CHECK(errorNumberMatches);
  CHECK(filenameMatches);

  JS::DestroyFrontendContext(fc);

  return true;
}

static const char* filename;
static bool warningReporterCalled;
static bool errorNumberMatches;
static bool filenameMatches;

static void warningReporter(JSContext* cx, JSErrorReport* report) {
  warningReporterCalled = true;

  errorNumberMatches = report->errorNumber == JSMSG_STMT_AFTER_RETURN;
  filenameMatches = report->filename.c_str() == filename;
}
END_TEST(testFrontendErrors_warning)

/* static */ const char* cls_testFrontendErrors_warning::filename =
    "testFrontendErrors_warning.js";
/* static */ bool cls_testFrontendErrors_warning::warningReporterCalled = false;
/* static */ bool cls_testFrontendErrors_warning::errorNumberMatches = false;
/* static */ bool cls_testFrontendErrors_warning::filenameMatches = false;

BEGIN_TEST(testFrontendErrors_oom) {
  JS::FrontendContext* fc = JS::NewFrontendContext();
  CHECK(fc);

  JS::PrefableCompileOptions prefableOptions;
  JS::CompileOptions options(prefableOptions);

  CHECK(!JS::HadFrontendErrors(fc));

  js::ReportOutOfMemory(fc);

  CHECK(JS::HadFrontendErrors(fc));
  CHECK(!JS::HadFrontendOverRecursed(fc));
  CHECK(JS::HadFrontendOutOfMemory(fc));
  CHECK(!JS::HadFrontendAllocationOverflow(fc));
  CHECK(JS::GetFrontendWarningCount(fc) == 0);

  CHECK(!JS_IsExceptionPending(cx));

  JS::SetWarningReporter(cx, warningReporter);

  bool result = JS::ConvertFrontendErrorsToRuntimeErrors(cx, fc, options);
  CHECK(!result);

  CHECK(JS_IsExceptionPending(cx));
  CHECK(!warningReporterCalled);
  CHECK(JS_IsThrowingOutOfMemory(cx));

  JS_ClearPendingException(cx);

  JS::DestroyFrontendContext(fc);

  return true;
}

static bool warningReporterCalled;

static void warningReporter(JSContext* cx, JSErrorReport* report) {
  warningReporterCalled = true;
}
END_TEST(testFrontendErrors_oom)

/* static */ bool cls_testFrontendErrors_oom::warningReporterCalled = false;

BEGIN_TEST(testFrontendErrors_overRecursed) {
  JS::FrontendContext* fc = JS::NewFrontendContext();
  CHECK(fc);

  JS::PrefableCompileOptions prefableOptions;
  JS::CompileOptions options(prefableOptions);

  CHECK(!JS::HadFrontendErrors(fc));

  js::ReportOverRecursed(fc);

  CHECK(JS::HadFrontendErrors(fc));
  CHECK(JS::HadFrontendOverRecursed(fc));
  CHECK(!JS::HadFrontendOutOfMemory(fc));
  CHECK(!JS::HadFrontendAllocationOverflow(fc));
  CHECK(JS::GetFrontendWarningCount(fc) == 0);

  CHECK(!JS_IsExceptionPending(cx));

  JS::SetWarningReporter(cx, warningReporter);

  bool result = JS::ConvertFrontendErrorsToRuntimeErrors(cx, fc, options);
  CHECK(result);

  CHECK(JS_IsExceptionPending(cx));
  CHECK(!warningReporterCalled);
  CHECK(!JS_IsThrowingOutOfMemory(cx));

  {
    JS::Rooted<JS::Value> exception(cx);
    CHECK(JS_GetPendingException(cx, &exception));

    CHECK(exception.isObject());
    JS::Rooted<JSObject*> exceptionObj(cx, &exception.toObject());

    const JSErrorReport* report = JS_ErrorFromException(cx, exceptionObj);
    CHECK(report);

    CHECK(report->errorNumber == JSMSG_OVER_RECURSED);
  }

  JS_ClearPendingException(cx);

  JS::DestroyFrontendContext(fc);

  return true;
}

static bool warningReporterCalled;

static void warningReporter(JSContext* cx, JSErrorReport* report) {
  warningReporterCalled = true;
}
END_TEST(testFrontendErrors_overRecursed)

/* static */ bool cls_testFrontendErrors_overRecursed::warningReporterCalled =
    false;

BEGIN_TEST(testFrontendErrors_allocationOverflow) {
  JS::FrontendContext* fc = JS::NewFrontendContext();
  CHECK(fc);

  JS::PrefableCompileOptions prefableOptions;
  JS::CompileOptions options(prefableOptions);

  CHECK(!JS::HadFrontendErrors(fc));

  js::ReportAllocationOverflow(fc);

  CHECK(JS::HadFrontendErrors(fc));
  CHECK(!JS::HadFrontendOverRecursed(fc));
  CHECK(!JS::HadFrontendOutOfMemory(fc));
  CHECK(JS::HadFrontendAllocationOverflow(fc));
  CHECK(JS::GetFrontendWarningCount(fc) == 0);

  CHECK(!JS_IsExceptionPending(cx));

  JS::SetWarningReporter(cx, warningReporter);

  bool result = JS::ConvertFrontendErrorsToRuntimeErrors(cx, fc, options);
  CHECK(result);

  CHECK(JS_IsExceptionPending(cx));
  CHECK(!warningReporterCalled);
  CHECK(!JS_IsThrowingOutOfMemory(cx));

  {
    JS::Rooted<JS::Value> exception(cx);
    CHECK(JS_GetPendingException(cx, &exception));

    CHECK(exception.isObject());
    JS::Rooted<JSObject*> exceptionObj(cx, &exception.toObject());

    const JSErrorReport* report = JS_ErrorFromException(cx, exceptionObj);
    CHECK(report);

    CHECK(report->errorNumber == JSMSG_ALLOC_OVERFLOW);
  }

  JS_ClearPendingException(cx);

  JS::DestroyFrontendContext(fc);

  return true;
}

static bool warningReporterCalled;

static void warningReporter(JSContext* cx, JSErrorReport* report) {
  warningReporterCalled = true;
}
END_TEST(testFrontendErrors_allocationOverflow)

/* static */ bool
    cls_testFrontendErrors_allocationOverflow::warningReporterCalled = false;