1*597e407cSKostya Serebryany // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2*597e407cSKostya Serebryany // See https://llvm.org/LICENSE.txt for license information.
3*597e407cSKostya Serebryany // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4*597e407cSKostya Serebryany
5*597e407cSKostya Serebryany // This is a fuzz target for running out-of-process fuzzing for a
6*597e407cSKostya Serebryany // binary specified via environment variable LIBFUZZER_OOP_TARGET.
7*597e407cSKostya Serebryany // libFuzzer is not designed for out-of-process fuzzing and so this
8*597e407cSKostya Serebryany // ad-hoc rig lacks many of the in-process libFuzzer features, and is slow,
9*597e407cSKostya Serebryany // but it does provide the basic functionality, which is to run the target
10*597e407cSKostya Serebryany // many times in parallel, feeding in the mutants, and expanding the corpus.
11*597e407cSKostya Serebryany // Use this only for very slow targets (slower than ~ 10 exec/s)
12*597e407cSKostya Serebryany // that you can't convert to conventional libFuzzer fuzz targets.
13*597e407cSKostya Serebryany //
14*597e407cSKostya Serebryany // The target binary (which could be a shell script, or anything),
15*597e407cSKostya Serebryany // consumes one file as an input and produces the file with coverage counters
16*597e407cSKostya Serebryany // as the output (output path is passed via SANCOV_OUT).
17*597e407cSKostya Serebryany // One way to produce a valid binary target is to build it with
18*597e407cSKostya Serebryany // -fsanitize-coverage=inline-8bit-counters and link it with SanCovDump.cpp,
19*597e407cSKostya Serebryany // found in the same directory.
20*597e407cSKostya Serebryany //
21*597e407cSKostya Serebryany // Example usage:
22*597e407cSKostya Serebryany /*
23*597e407cSKostya Serebryany clang -fsanitize=fuzzer OutOfProcessFuzzTarget.cpp -o oop-fuzz &&
24*597e407cSKostya Serebryany clang -c -fsanitize-coverage=inline-8bit-counters SimpleTest.cpp &&
25*597e407cSKostya Serebryany clang -c ../../lib/fuzzer/standalone/StandaloneFuzzTargetMain.c &&
26*597e407cSKostya Serebryany clang -c SanCovDump.cpp &&
27*597e407cSKostya Serebryany clang++ SanCovDump.o SimpleTest.o StandaloneFuzzTargetMain.o -o oop-target &&
28*597e407cSKostya Serebryany rm -rf CORPUS && mkdir CORPUS && echo > CORPUS/seed &&
29*597e407cSKostya Serebryany LIBFUZZER_OOP_TARGET="./oop-target > /dev/null 2>&1 " ./oop-fuzz CORPUS -jobs=42
30*597e407cSKostya Serebryany
31*597e407cSKostya Serebryany */
32*597e407cSKostya Serebryany #include <fcntl.h>
33*597e407cSKostya Serebryany #include <stdint.h>
34*597e407cSKostya Serebryany #include <stdio.h>
35*597e407cSKostya Serebryany #include <sys/stat.h>
36*597e407cSKostya Serebryany #include <sys/types.h>
37*597e407cSKostya Serebryany #include <unistd.h>
38*597e407cSKostya Serebryany
39*597e407cSKostya Serebryany #include <string>
40*597e407cSKostya Serebryany
41*597e407cSKostya Serebryany // An arbitrary large number.
42*597e407cSKostya Serebryany // If your target is so large that it has more than this number of coverage
43*597e407cSKostya Serebryany // edges, you may want to increase this number to match your binary,
44*597e407cSKostya Serebryany // otherwise part of the coverage will be lost.
45*597e407cSKostya Serebryany // For small targets there is no reason to reduce this number.
46*597e407cSKostya Serebryany static const size_t kCountersSize = 1 << 20;
47*597e407cSKostya Serebryany
48*597e407cSKostya Serebryany __attribute__((section(
49*597e407cSKostya Serebryany "__libfuzzer_extra_counters"))) static uint8_t Counters[kCountersSize];
50*597e407cSKostya Serebryany
51*597e407cSKostya Serebryany static std::string *Run, *IN, *COV;
52*597e407cSKostya Serebryany
TearDown()53*597e407cSKostya Serebryany void TearDown() {
54*597e407cSKostya Serebryany unlink(COV->c_str());
55*597e407cSKostya Serebryany unlink(IN->c_str());
56*597e407cSKostya Serebryany }
57*597e407cSKostya Serebryany
Initialize()58*597e407cSKostya Serebryany bool Initialize() {
59*597e407cSKostya Serebryany IN = new std::string("lf-oop-in-" + std::to_string(getpid()));
60*597e407cSKostya Serebryany COV = new std::string("lf-oop-cov-" + std::to_string(getpid()));
61*597e407cSKostya Serebryany const char *TargetEnv = getenv("LIBFUZZER_OOP_TARGET");
62*597e407cSKostya Serebryany if (!TargetEnv) {
63*597e407cSKostya Serebryany fprintf(stderr, "Please define LIBFUZZER_OOP_TARGET\n");
64*597e407cSKostya Serebryany exit(1);
65*597e407cSKostya Serebryany }
66*597e407cSKostya Serebryany Run = new std::string("SANCOV_OUT=" + *COV + " " + TargetEnv + " " + *IN);
67*597e407cSKostya Serebryany fprintf(stderr, "libFuzzer: OOP command: %s\n", Run->c_str());
68*597e407cSKostya Serebryany atexit(TearDown);
69*597e407cSKostya Serebryany return true;
70*597e407cSKostya Serebryany }
71*597e407cSKostya Serebryany
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72*597e407cSKostya Serebryany extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
73*597e407cSKostya Serebryany static bool Inited = Initialize();
74*597e407cSKostya Serebryany if (size == 0)
75*597e407cSKostya Serebryany return 0;
76*597e407cSKostya Serebryany if (FILE *f = fopen(IN->c_str(), "w")) {
77*597e407cSKostya Serebryany fwrite(data, 1, size, f);
78*597e407cSKostya Serebryany fclose(f);
79*597e407cSKostya Serebryany }
80*597e407cSKostya Serebryany system(Run->c_str());
81*597e407cSKostya Serebryany if (FILE *f = fopen(COV->c_str(), "r")) {
82*597e407cSKostya Serebryany fread(Counters, 1, kCountersSize, f);
83*597e407cSKostya Serebryany fclose(f);
84*597e407cSKostya Serebryany }
85*597e407cSKostya Serebryany return 0;
86*597e407cSKostya Serebryany }
87