| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
#include <cstdlib> |
| 7 |
|
| 8 |
#include "FuzzerRunner.h" |
| 9 |
#include "mozilla/Attributes.h" |
| 10 |
#include "prenv.h" |
| 11 |
|
| 12 |
#include "FuzzerTestHarness.h" |
| 13 |
|
| 14 |
namespace mozilla { |
| 15 |
|
| 16 |
// We use a static var 'fuzzerRunner' defined in nsAppRunner.cpp. |
| 17 |
// fuzzerRunner is initialized to nullptr but if this file is linked in, |
| 18 |
// then fuzzerRunner will be set here indicating that |
| 19 |
// we want to call into either LibFuzzer's main or the AFL entrypoint. |
| 20 |
MOZ_RUNINIT class _InitFuzzer { |
| 21 |
public: |
| 22 |
_InitFuzzer() { fuzzerRunner = new FuzzerRunner(); } |
| 23 |
void InitXPCOM() { mScopedXPCOM = new ScopedXPCOM("Fuzzer"); } |
| 24 |
void DeinitXPCOM() { |
| 25 |
if (mScopedXPCOM) delete mScopedXPCOM; |
| 26 |
mScopedXPCOM = nullptr; |
| 27 |
} |
| 28 |
|
| 29 |
private: |
| 30 |
ScopedXPCOM* mScopedXPCOM; |
| 31 |
} InitLibFuzzer; |
| 32 |
|
| 33 |
static void DeinitXPCOM() { InitLibFuzzer.DeinitXPCOM(); } |
| 34 |
|
| 35 |
int FuzzerRunner::Run(int* argc, char*** argv) { |
| 36 |
/* |
| 37 |
* libFuzzer uses exit() calls in several places instead of returning, |
| 38 |
* so the destructor of ScopedXPCOM is not called in some cases. |
| 39 |
* For fuzzing, this does not make a difference, but in debug builds |
| 40 |
* when running a single testcase, this causes an assertion when destroying |
| 41 |
* global linked lists. For this reason, we allocate ScopedXPCOM on the heap |
| 42 |
* using the global InitLibFuzzer class, combined with an atexit call to |
| 43 |
* destroy the ScopedXPCOM instance again. |
| 44 |
*/ |
| 45 |
InitLibFuzzer.InitXPCOM(); |
| 46 |
std::atexit(DeinitXPCOM); |
| 47 |
|
| 48 |
const char* fuzzerEnv = getenv("FUZZER"); |
| 49 |
|
| 50 |
if (!fuzzerEnv) { |
| 51 |
fprintf(stderr, |
| 52 |
"Must specify fuzzing target in FUZZER environment variable\n"); |
| 53 |
exit(1); |
| 54 |
} |
| 55 |
|
| 56 |
std::string moduleNameStr(fuzzerEnv); |
| 57 |
|
| 58 |
// FUZZER=list will display available fuzzing targets and exit. |
| 59 |
if (moduleNameStr == "list") { |
| 60 |
printf("FUZZER=list detected, listing targets...\n"); |
| 61 |
printf("===== Targets =====\n"); |
| 62 |
FuzzerRegistry::getInstance().printModuleNames(); |
| 63 |
printf("===== End of list =====\n"); |
| 64 |
exit(0); |
| 65 |
} |
| 66 |
|
| 67 |
FuzzerFunctions funcs = |
| 68 |
FuzzerRegistry::getInstance().getModuleFunctions(moduleNameStr); |
| 69 |
FuzzerInitFunc initFunc = funcs.first; |
| 70 |
FuzzerTestingFunc testingFunc = funcs.second; |
| 71 |
if (initFunc) { |
| 72 |
int ret = initFunc(argc, argv); |
| 73 |
if (ret) { |
| 74 |
fprintf(stderr, "Fuzzing Interface: Error: Initialize callback failed\n"); |
| 75 |
exit(1); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (!testingFunc) { |
| 80 |
fprintf(stderr, "Fuzzing Interface: Error: No testing callback found\n"); |
| 81 |
exit(1); |
| 82 |
} |
| 83 |
|
| 84 |
#ifdef LIBFUZZER |
| 85 |
int ret = mFuzzerDriver(argc, argv, testingFunc); |
| 86 |
#else |
| 87 |
// For AFL, testingFunc points to the entry function we need. |
| 88 |
int ret = testingFunc(NULL, 0); |
| 89 |
#endif |
| 90 |
|
| 91 |
InitLibFuzzer.DeinitXPCOM(); |
| 92 |
return ret; |
| 93 |
} |
| 94 |
|
| 95 |
#ifdef LIBFUZZER |
| 96 |
void FuzzerRunner::setParams(LibFuzzerDriver aDriver) { |
| 97 |
mFuzzerDriver = aDriver; |
| 98 |
} |
| 99 |
#endif |
| 100 |
|
| 101 |
} // namespace mozilla |