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/. */
7 * Common code for the unified fuzzing interface
12 #include "FuzzingInterface.h"
17 MOZ_RUNINIT
static bool fuzzing_verbose
= !!getenv("MOZ_FUZZ_LOG");
18 void fuzzing_log(const char* aFmt
, ...) {
19 if (fuzzing_verbose
) {
22 vfprintf(stderr
, aFmt
, ap
);
27 LazyLogModule
gFuzzingLog("nsFuzzing");
30 } // namespace mozilla
33 __attribute__((weak
)) extern uint8_t* __afl_area_ptr
;
34 __attribute__((weak
)) extern uint32_t __afl_map_size
;
38 int afl_interface_raw(FuzzingTestFuncRaw testFunc
) {
39 char* testFilePtr
= getenv("MOZ_FUZZ_TESTFILE");
43 std::string
testFile(testFilePtr
);
44 while (__AFL_LOOP(1000)) {
46 is
.open(testFile
, std::ios::binary
);
47 is
.seekg(0, std::ios::end
);
48 size_t len
= is
.tellg();
49 is
.seekg(0, std::ios::beg
);
50 MOZ_RELEASE_ASSERT(len
>= 0);
55 buf
= reinterpret_cast<uint8_t*>(realloc(buf
, len
));
56 MOZ_RELEASE_ASSERT(buf
);
57 is
.read(reinterpret_cast<char*>(buf
), len
);
59 if (testFunc(buf
, len
)) {
60 // this pattern is from the driver for
61 // LLVMFuzzerTestOneInput in aflpp_driver.c
62 memset(__afl_area_ptr
, 0, __afl_map_size
);
63 __afl_area_ptr
[0] = 1;
67 buf
= __AFL_FUZZ_TESTCASE_BUF
;
68 while (__AFL_LOOP(1000)) {
69 size_t len
= __AFL_FUZZ_TESTCASE_LEN
;
70 if (testFunc(buf
, len
)) {
71 // this pattern is from the driver for
72 // LLVMFuzzerTestOneInput in aflpp_driver.c
73 memset(__afl_area_ptr
, 0, __afl_map_size
);
74 __afl_area_ptr
[0] = 1;