1
1

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>
Этот коммит содержится в:
Jakub Jelen 2020-04-27 18:44:14 +02:00 коммит произвёл Andreas Schneider
родитель 5411e0821f
Коммит 62a0229f16
2 изменённых файлов: 57 добавлений и 21 удалений

Просмотреть файл

@ -1,26 +1,23 @@
project(fuzzing CXX) project(fuzzing CXX)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") macro(fuzzer name)
add_executable(ssh_client_fuzzer ssh_client_fuzzer.cpp) add_executable(${name} ${name}.cpp)
target_link_libraries(ssh_client_fuzzer target_link_libraries(${name}
PRIVATE PRIVATE
ssh::static) ssh::static)
set_target_properties(ssh_client_fuzzer if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
PROPERTIES set_target_properties(${name}
COMPILE_FLAGS "-fsanitize=fuzzer" PROPERTIES
LINK_FLAGS "-fsanitize=fuzzer") 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()
fuzzer(ssh_client_fuzzer)
add_executable(ssh_server_fuzzer ssh_server_fuzzer.cpp) fuzzer(ssh_server_fuzzer)
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()

39
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;
}