clang -fsanitize=fuzzer OutOfProcessFuzzTarget.cpp -o oop-fuzz &&
clang -c -fsanitize-coverage=inline-8bit-counters SimpleTest.cpp &&
clang -c ../../lib/fuzzer/standalone/StandaloneFuzzTargetMain.c &&
clang -c SanCovDump.cpp &&
clang++ SanCovDump.o SimpleTest.o StandaloneFuzzTargetMain.o -o oop-target &&
rm -rf CORPUS && mkdir CORPUS && echo > CORPUS/seed &&
LIBFUZZER_OOP_TARGET="./oop-target > /dev/null 2>&1 " ./oop-fuzz CORPUS -jobs=42
*/
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
static const size_t kCountersSize = 1 << 20;
__attribute__((section(
"__libfuzzer_extra_counters"))) static uint8_t Counters[kCountersSize];
static std::string *Run, *IN, *COV;
void TearDown() {
unlink(COV->c_str());
unlink(IN->c_str());
}
bool Initialize() {
IN = new std::string("lf-oop-in-" + std::to_string(getpid()));
COV = new std::string("lf-oop-cov-" + std::to_string(getpid()));
const char *TargetEnv = getenv("LIBFUZZER_OOP_TARGET");
if (!TargetEnv) {
fprintf(stderr, "Please define LIBFUZZER_OOP_TARGET\n");
exit(1);
}
Run = new std::string("SANCOV_OUT=" + *COV + " " + TargetEnv + " " + *IN);
fprintf(stderr, "libFuzzer: OOP command: %s\n", Run->c_str());
atexit(TearDown);
return true;
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
static bool Inited = Initialize();
if (size == 0)
return 0;
if (FILE *f = fopen(IN->c_str(), "w")) {
fwrite(data, 1, size, f);
fclose(f);
}
system(Run->c_str());
if (FILE *f = fopen(COV->c_str(), "r")) {
fread(Counters, 1, kCountersSize, f);
fclose(f);
}
return 0;
}