diff options
author | Jakub Jelen <jjelen@redhat.com> | 2020-04-27 18:44:14 +0200 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2020-05-05 14:23:06 +0200 |
commit | 62a0229f16cdde7291dcfe8cc81847631281baef (patch) | |
tree | 330dc4b48cb4d494020d985dbda2e7dceed1f3b5 | |
parent | 5411e0821fdd460820460e9a35fdffb554dc9e24 (diff) | |
download | libssh-62a0229f16cdde7291dcfe8cc81847631281baef.tar.gz libssh-62a0229f16cdde7291dcfe8cc81847631281baef.tar.xz libssh-62a0229f16cdde7291dcfe8cc81847631281baef.zip |
fuzz: Simplify definition of fuzzing targets and build them also with gcc
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r-- | tests/fuzz/CMakeLists.txt | 39 | ||||
-rw-r--r-- | tests/fuzz/fuzzer.c | 39 |
2 files changed, 57 insertions, 21 deletions
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 <assert.h> +#include <stdio.h> +#include <stdlib.h> + +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; +} |