Skip to content

Commit 484f541

Browse files
committed
add fuzzing poc
1 parent f25c80b commit 484f541

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

tests/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,14 @@ if(UNIX)
1717
endif(LINUX)
1818

1919
endif(UNIX)
20+
21+
if(FUZZ)
22+
add_executable(urltest-fuzz urltest-fuzz.cpp)
23+
# TODO add_executable(pcm-sensor-server-fuzz pcm-sensor-server-fuzz.cpp)
24+
set(FUZZER_OPTIONS "-fsanitize=fuzzer,address -fprofile-instr-generate -fcoverage-mapping")
25+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FUZZER_OPTIONS}")
26+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FUZZER_OPTIONS}")
27+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FUZZER_OPTIONS}")
28+
target_link_libraries(urltest-fuzz Threads::Threads PCM_STATIC)
29+
# TODO: target_link_libraries(pcm-sensor-server-fuzz Threads::Threads PCM_STATIC)
30+
endif()

tests/fuzz.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
CC=`which clang` CXX=`which clang++` cmake .. -DCMAKE_BUILD_TYPE=Debug -DFUZZ=1 && mkdir corpus &&
3+
make urltest-fuzz &&
4+
bin/tests/urltest-fuzz -max_total_time=30 corpus &&
5+
llvm-profdata merge -sparse default.profraw -o default.profdata &&
6+
llvm-cov report --summary-only ./bin/tests/urltest-fuzz -instr-profile=default.profdata
7+

tests/urltest-fuzz.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#define UNIT_TEST 1
2+
3+
#include "../src/pcm-sensor-server.cpp"
4+
5+
#undef UNIT_TEST
6+
7+
8+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
9+
{
10+
try
11+
{
12+
std::string buf(reinterpret_cast<const char*>(data), size);
13+
buf.push_back('\0');
14+
URL x = URL::parse(buf.c_str());
15+
}
16+
catch (std::runtime_error & )
17+
{
18+
// catch recognized malformed input (thrown as runtime_error in the URL::parse)
19+
// do not catch any other errors or exceptions to let them be reported
20+
// by libFuzzer
21+
}
22+
23+
return 0;
24+
}
25+

0 commit comments

Comments
 (0)