| 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 |
*/ |
| 4 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 7 |
|
| 8 |
#include "mozilla/ScopeExit.h" |
| 9 |
|
| 10 |
#include "jsapi.h" |
| 11 |
|
| 12 |
#include "fuzz-tests/tests.h" |
| 13 |
#include "vm/Interpreter.h" |
| 14 |
|
| 15 |
#include "vm/JSContext-inl.h" |
| 16 |
|
| 17 |
using namespace JS; |
| 18 |
using namespace js; |
| 19 |
|
| 20 |
extern JS::PersistentRootedObject gGlobal; |
| 21 |
extern JSContext* gCx; |
| 22 |
|
| 23 |
static int testExampleInit(int* argc, char*** argv) { |
| 24 |
/* This function is called once at startup. You can use it to e.g. read |
| 25 |
environment variables to initialize additional options you might need. |
| 26 |
Note that `gCx` and `gGlobal` are pre-initialized by the harness. |
| 27 |
*/ |
| 28 |
return 0; |
| 29 |
} |
| 30 |
|
| 31 |
static int testExampleFuzz(const uint8_t* buf, size_t size) { |
| 32 |
/* If your code directly or indirectly allocates GC memory, then it makes |
| 33 |
sense to attempt and collect that after every iteration. This should detect |
| 34 |
GC issues as soon as possible (right after your iteration), rather than |
| 35 |
later when your code happens to trigger GC coincidentially. You can of |
| 36 |
course disable this code |
| 37 |
if it is not required in your use case, which will speed up fuzzing. */ |
| 38 |
auto gcGuard = mozilla::MakeScopeExit([&] { |
| 39 |
JS::PrepareForFullGC(gCx); |
| 40 |
JS::NonIncrementalGC(gCx, JS::GCOptions::Normal, JS::GCReason::API); |
| 41 |
}); |
| 42 |
|
| 43 |
/* Add code here that processes the given buffer. |
| 44 |
While doing so, you need to follow these rules: |
| 45 |
|
| 46 |
1. Do not modify or free the buffer. Make a copy if necessary. |
| 47 |
2. This function must always return 0. |
| 48 |
3. Do not crash or abort unless the condition constitutes a bug. |
| 49 |
4. You may use the `gGlobal` and `gCx` variables, they are pre-initialized. |
| 50 |
5. Try to keep the effects of this function contained, such that future |
| 51 |
calls to this function are not affected. Otherwise you end up with |
| 52 |
non-reproducible testcases and coverage measurements will be incorrect. |
| 53 |
*/ |
| 54 |
|
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
MOZ_FUZZING_INTERFACE_RAW(testExampleInit, /* init function */ |
| 59 |
testExampleFuzz, /* fuzzing function */ |
| 60 |
Example /* module name */ |
| 61 |
); |