From 62a0229f16cdde7291dcfe8cc81847631281baef Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Mon, 27 Apr 2020 18:44:14 +0200 Subject: fuzz: Simplify definition of fuzzing targets and build them also with gcc Signed-off-by: Jakub Jelen Reviewed-by: Andreas Schneider --- tests/fuzz/CMakeLists.txt | 39 ++++++++++++++++++--------------------- tests/fuzz/fuzzer.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 tests/fuzz/fuzzer.c (limited to 'tests') diff --git a/tests/fuzz/CMakeLists.txt b/tests/fuzz/CMakeLists.txt index bfbf9c4e..5982e81c 100644 --- a/tests/fuzz/CMakeLists.txt +++ b/tests/fuzz/CMakeLists.txt @@ -1,26 +1,23 @@ project(fuzzing CXX) -if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - add_executable(ssh_client_fuzzer ssh_client_fuzzer.cpp) - target_link_libraries(ssh_client_fuzzer +macro(fuzzer name) + add_executable(${name} ${name}.cpp) + target_link_libraries(${name} PRIVATE ssh::static) - set_target_properties(ssh_client_fuzzer - PROPERTIES - COMPILE_FLAGS "-fsanitize=fuzzer" - LINK_FLAGS "-fsanitize=fuzzer") + if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + set_target_properties(${name} + PROPERTIES + COMPILE_FLAGS "-fsanitize=fuzzer" + LINK_FLAGS "-fsanitize=fuzzer") + # Run the fuzzer to make sure it works + add_test(${name} ${CMAKE_CURRENT_BINARY_DIR}/${name} -runs=1) + else() + target_sources(${name} PRIVATE fuzzer.c) + # Run the fuzzer to make sure it works + # add_test(${name} ${CMAKE_CURRENT_BINARY_DIR}/${name} EXAMPLE) + endif() +endmacro() - - add_executable(ssh_server_fuzzer ssh_server_fuzzer.cpp) - target_link_libraries(ssh_server_fuzzer - PRIVATE - ssh::static) - set_target_properties(ssh_server_fuzzer - PROPERTIES - COMPILE_FLAGS "-fsanitize=fuzzer" - LINK_FLAGS "-fsanitize=fuzzer") - - # Run the fuzzer to make sure it works - add_test(ssh_client_fuzzer ${CMAKE_CURRENT_BINARY_DIR}/ssh_client_fuzzer -runs=1) - add_test(ssh_server_fuzzer ${CMAKE_CURRENT_BINARY_DIR}/ssh_server_fuzzer -runs=1) -endif() +fuzzer(ssh_client_fuzzer) +fuzzer(ssh_server_fuzzer) diff --git a/tests/fuzz/fuzzer.c b/tests/fuzz/fuzzer.c new file mode 100644 index 00000000..4db6a2bc --- /dev/null +++ b/tests/fuzz/fuzzer.c @@ -0,0 +1,39 @@ +/* Simpler gnu89 version of StandaloneFuzzTargetMain.c from LLVM */ + +#include +#include +#include + +int LLVMFuzzerTestOneInput (const unsigned char *data, size_t size); +__attribute__((weak)) int LLVMFuzzerInitialize(int *argc, char ***argv); + +int +main (int argc, char **argv) +{ + FILE *f = NULL; + size_t n_read, len; + unsigned char *buf = NULL; + + if (argc < 2) { + return 1; + } + + if (LLVMFuzzerInitialize) { + LLVMFuzzerInitialize(&argc, &argv); + } + + f = fopen (argv[1], "r"); + assert (f); + fseek (f, 0, SEEK_END); + len = ftell (f); + fseek (f, 0, SEEK_SET); + buf = (unsigned char*) malloc (len); + n_read = fread (buf, 1, len, f); + fclose (f); + assert (n_read == len); + LLVMFuzzerTestOneInput (buf, len); + + free (buf); + printf ("Done!\n"); + return 0; +} -- cgit v1.2.3