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"
14 #include "js/RootingAPI.h"
16 #include "vm/JSContext.h"
19 # include "FuzzerDefs.h"
22 using namespace mozilla
;
24 JS::PersistentRootedObject gGlobal
;
25 JSContext
* gCx
= nullptr;
27 static const JSClass
* getGlobalClass() {
28 static const JSClass c
= {"global", JSCLASS_GLOBAL_FLAGS
,
29 &JS::DefaultGlobalClassOps
};
33 static JSObject
* jsfuzz_createGlobal(JSContext
* cx
, JSPrincipals
* principals
) {
34 /* Create the global object. */
35 JS::RealmOptions options
;
36 options
.creationOptions()
37 .setWeakRefsEnabled(JS::WeakRefSpecifier::EnabledWithCleanupSome
)
38 .setSharedMemoryAndAtomicsEnabled(true);
39 return JS_NewGlobalObject(cx
, getGlobalClass(), principals
,
40 JS::FireOnNewGlobalHook
, options
);
43 static bool jsfuzz_init(JSContext
** cx
, JS::PersistentRootedObject
* global
) {
44 *cx
= JS_NewContext(8L * 1024 * 1024);
49 const size_t MAX_STACK_SIZE
= 500000;
51 JS_SetNativeStackQuota(*cx
, MAX_STACK_SIZE
);
53 js::UseInternalJobQueues(*cx
);
54 if (!JS::InitSelfHostedCode(*cx
)) {
58 *global
= jsfuzz_createGlobal(*cx
, nullptr);
62 JS::EnterRealm(*cx
, *global
);
66 static void jsfuzz_uninit(JSContext
* cx
) {
68 JS::LeaveRealm(cx
, nullptr);
69 JS_DestroyContext(cx
);
74 int main(int argc
, char* argv
[]) {
76 fprintf(stderr
, "Error: Call to jsfuzz_init() failed\n");
80 if (!jsfuzz_init(&gCx
, &gGlobal
)) {
81 fprintf(stderr
, "Error: Call to jsfuzz_init() failed\n");
85 const char* fuzzerEnv
= getenv("FUZZER");
88 "Must specify fuzzing target in FUZZER environment variable\n");
92 std::string
moduleNameStr(getenv("FUZZER"));
94 FuzzerFunctions funcs
=
95 FuzzerRegistry::getInstance().getModuleFunctions(moduleNameStr
);
96 FuzzerInitFunc initFunc
= funcs
.first
;
97 FuzzerTestingFunc testingFunc
= funcs
.second
;
99 int ret
= initFunc(&argc
, &argv
);
101 fprintf(stderr
, "Fuzzing Interface: Error: Initialize callback failed\n");
107 fprintf(stderr
, "Fuzzing Interface: Error: No testing callback found\n");
112 fuzzer::FuzzerDriver(&argc
, &argv
, testingFunc
);
114 testingFunc(nullptr, 0);