From a88ecadafe08802b1faa1b6c7d2f2d38f03f542f Mon Sep 17 00:00:00 2001 From: Emil Mikulic Date: Sun, 2 Oct 2022 12:24:17 +1100 Subject: [PATCH] fuzzer: take optional port number from environment variable. Makes it possible to run multiple fuzzer processes in parallel. --- devel/fuzz_socket.cc | 14 ++++++++++++-- devel/fuzz_socket.sh | 3 +++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/devel/fuzz_socket.cc b/devel/fuzz_socket.cc index 211822c..979d20e 100644 --- a/devel/fuzz_socket.cc +++ b/devel/fuzz_socket.cc @@ -6,14 +6,16 @@ #include #include +#include #include extern "C" int darkhttpd(int argc, const char** argv); extern "C" volatile int running; namespace { -int argc = 4; -const char* argv[] = {"./a.out", "tmp.fuzz", "--log", "/dev/null"}; +int argc = 6; +const char* argv[] = {"./a.out", "tmp.fuzz", "--log", "/dev/null", + "--port", "8080"}; std::thread* thr; const char* host = "127.0.0.1"; int port = 8080; @@ -24,6 +26,14 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { static bool inited = false; if (!inited) { 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_port = htons(port); if (inet_aton(host, &addrin.sin_addr) == 0) err(1, "inet_aton"); diff --git a/devel/fuzz_socket.sh b/devel/fuzz_socket.sh index bec9efb..5ae3338 100755 --- a/devel/fuzz_socket.sh +++ b/devel/fuzz_socket.sh @@ -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++ -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 + +# Or run multiple processes on different ports with e.g.: +# env PORT=9999 ./fuzz_socket fuzz_socket_testcases -detect_leaks=0 -only_ascii=1