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/. */
7 #include "fuzz-tests/tests.h"
11 #include "js/AllocPolicy.h"
12 #include "js/GlobalObject.h"
13 #include "js/Initialization.h"
15 #include "js/RealmOptions.h"
16 #include "js/RootingAPI.h"
18 #include "vm/JSContext.h"
21 # include "FuzzerDefs.h"
24 using namespace mozilla
;
26 JS::PersistentRootedObject gGlobal
;
27 JSContext
* gCx
= nullptr;
29 static const JSClass
* getGlobalClass() {
30 static const JSClass c
= {
33 &JS::DefaultGlobalClassOps
,
38 static JSObject
* jsfuzz_createGlobal(JSContext
* cx
, JSPrincipals
* principals
) {
39 /* Create the global object. */
40 JS::RealmOptions options
;
41 options
.creationOptions().setSharedMemoryAndAtomicsEnabled(true);
42 return JS_NewGlobalObject(cx
, getGlobalClass(), principals
,
43 JS::FireOnNewGlobalHook
, options
);
46 static bool jsfuzz_init(JSContext
** cx
, JS::PersistentRootedObject
* global
) {
47 *cx
= JS_NewContext(8L * 1024 * 1024);
52 const size_t MAX_STACK_SIZE
= 500000;
54 JS_SetNativeStackQuota(*cx
, MAX_STACK_SIZE
);
56 js::UseInternalJobQueues(*cx
);
57 if (!JS::InitSelfHostedCode(*cx
)) {
61 *global
= jsfuzz_createGlobal(*cx
, nullptr);
65 JS::EnterRealm(*cx
, *global
);
69 static void jsfuzz_uninit(JSContext
* cx
) {
71 JS::LeaveRealm(cx
, nullptr);
72 JS_DestroyContext(cx
);
77 int main(int argc
, char* argv
[]) {
78 // Override prefs for fuzz-tests.
79 JS::Prefs::setAtStartup_weakrefs(true);
80 JS::Prefs::setAtStartup_experimental_weakrefs_expose_cleanupSome(true);
83 fprintf(stderr
, "Error: Call to jsfuzz_init() failed\n");
87 if (!jsfuzz_init(&gCx
, &gGlobal
)) {
88 fprintf(stderr
, "Error: Call to jsfuzz_init() failed\n");
92 const char* fuzzerEnv
= getenv("FUZZER");
95 "Must specify fuzzing target in FUZZER environment variable\n");
99 std::string
moduleNameStr(getenv("FUZZER"));
101 FuzzerFunctions funcs
=
102 FuzzerRegistry::getInstance().getModuleFunctions(moduleNameStr
);
103 FuzzerInitFunc initFunc
= funcs
.first
;
104 FuzzerTestingFunc testingFunc
= funcs
.second
;
106 int ret
= initFunc(&argc
, &argv
);
108 fprintf(stderr
, "Fuzzing Interface: Error: Initialize callback failed\n");
114 fprintf(stderr
, "Fuzzing Interface: Error: No testing callback found\n");
119 fuzzer::FuzzerDriver(&argc
, &argv
, testingFunc
);
121 testingFunc(nullptr, 0);