Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/tools/fuzzing/interface/harness/FuzzerRunner.cpp
Line
Count
Source (jump to first uncovered line)
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
class _InitFuzzer {
21
public:
22
3
  _InitFuzzer() {
23
3
    fuzzerRunner = new FuzzerRunner();
24
3
  }
25
} InitLibFuzzer;
26
27
3
int FuzzerRunner::Run(int* argc, char*** argv) {
28
3
  ScopedXPCOM xpcom("Fuzzer");
29
3
  const char* fuzzerEnv = getenv("FUZZER");
30
3
31
3
  if (!fuzzerEnv) {
32
0
    fuzzerEnv = getenv("LIBFUZZER");
33
0
    if (fuzzerEnv) {
34
0
      fprintf(stderr, "Fuzzer Interface: Warning: \
35
0
        Using deprecated LIBFUZZER variable, use FUZZER instead\n");
36
0
    } else {
37
0
      fprintf(stderr, "Must specify fuzzing target in FUZZER environment variable\n");
38
0
      return 1;
39
0
    }
40
3
  }
41
3
42
3
  std::string moduleNameStr(fuzzerEnv);
43
3
  FuzzerFunctions funcs = FuzzerRegistry::getInstance().getModuleFunctions(moduleNameStr);
44
3
  FuzzerInitFunc initFunc = funcs.first;
45
3
  FuzzerTestingFunc testingFunc = funcs.second;
46
3
  if (initFunc) {
47
2
    int ret = initFunc(argc, argv);
48
2
    if (ret) {
49
0
      fprintf(stderr, "Fuzzing Interface: Error: Initialize callback failed\n");
50
0
      return ret;
51
0
    }
52
3
  }
53
3
54
3
  if (!testingFunc) {
55
0
      fprintf(stderr, "Fuzzing Interface: Error: No testing callback found\n");
56
0
      return 1;
57
0
  }
58
3
59
3
#ifdef LIBFUZZER
60
3
  return mFuzzerDriver(argc, argv, testingFunc);
61
#else
62
  // For AFL, testingFunc points to the entry function we need.
63
  return testingFunc(NULL, 0);
64
#endif
65
}
66
67
#ifdef LIBFUZZER
68
3
void FuzzerRunner::setParams(LibFuzzerDriver aDriver) {
69
3
  mFuzzerDriver = aDriver;
70
3
}
71
#endif
72
73
} // namespace mozilla