Skip to content

Commit 597e407

Browse files
committed
[libFuzzer] tests/examples for using libFuzzer for out-of-process targets
[libFuzzer] tests/examples for using libFuzzer for out-of-process targets Reviewed By: kostik Differential Revision: https://reviews.llvm.org/D107498
1 parent 041b525 commit 597e407

File tree

3 files changed

+138
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)