fuzzer: take optional port number from environment variable.

Makes it possible to run multiple fuzzer processes in parallel.
This commit is contained in:
Emil Mikulic 2022-10-02 12:24:17 +11:00
parent 762956f1a8
commit a88ecadafe
2 changed files with 15 additions and 2 deletions

View File

@ -6,14 +6,16 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#include <cstdlib>
#include <thread> #include <thread>
extern "C" int darkhttpd(int argc, const char** argv); extern "C" int darkhttpd(int argc, const char** argv);
extern "C" volatile int running; extern "C" volatile int running;
namespace { namespace {
int argc = 4; int argc = 6;
const char* argv[] = {"./a.out", "tmp.fuzz", "--log", "/dev/null"}; const char* argv[] = {"./a.out", "tmp.fuzz", "--log", "/dev/null",
"--port", "8080"};
std::thread* thr; std::thread* thr;
const char* host = "127.0.0.1"; const char* host = "127.0.0.1";
int port = 8080; int port = 8080;
@ -24,6 +26,14 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
static bool inited = false; static bool inited = false;
if (!inited) { if (!inited) {
thr = new std::thread([]() { darkhttpd(argc, argv); }); thr = new std::thread([]() { darkhttpd(argc, argv); });
// If PORT is set in the environment, use it as the port number.
char* port_str = getenv("PORT");
if (port_str) {
port = atoi(port_str);
argv[argc - 1] = port_str;
}
addrin.sin_family = AF_INET; addrin.sin_family = AF_INET;
addrin.sin_port = htons(port); addrin.sin_port = htons(port);
if (inet_aton(host, &addrin.sin_addr) == 0) err(1, "inet_aton"); if (inet_aton(host, &addrin.sin_addr) == 0) err(1, "inet_aton");

View File

@ -4,3 +4,6 @@ mkdir -p fuzz_socket_testcases
clang -c -Dmain=darkhttpd -g -O2 -fsanitize=fuzzer,address ../darkhttpd.c -o fuzz_darkhttpd.o clang -c -Dmain=darkhttpd -g -O2 -fsanitize=fuzzer,address ../darkhttpd.c -o fuzz_darkhttpd.o
clang++ -g -O2 -fsanitize=fuzzer,address fuzz_socket.cc fuzz_darkhttpd.o -o fuzz_socket clang++ -g -O2 -fsanitize=fuzzer,address fuzz_socket.cc fuzz_darkhttpd.o -o fuzz_socket
./fuzz_socket fuzz_socket_testcases -detect_leaks=0 -only_ascii=1 ./fuzz_socket fuzz_socket_testcases -detect_leaks=0 -only_ascii=1
# Or run multiple processes on different ports with e.g.:
# env PORT=9999 ./fuzz_socket fuzz_socket_testcases -detect_leaks=0 -only_ascii=1