diff options
Diffstat (limited to 'libs/zip/fuzz')
| -rw-r--r-- | libs/zip/fuzz/CMakeLists.txt | 24 | ||||
| -rw-r--r-- | libs/zip/fuzz/build.sh | 4 | ||||
| -rw-r--r-- | libs/zip/fuzz/fuzz_entry.c | 38 | ||||
| -rw-r--r-- | libs/zip/fuzz/fuzz_stream.c | 40 |
4 files changed, 106 insertions, 0 deletions
diff --git a/libs/zip/fuzz/CMakeLists.txt b/libs/zip/fuzz/CMakeLists.txt new file mode 100644 index 0000000..ab5d8aa --- /dev/null +++ b/libs/zip/fuzz/CMakeLists.txt @@ -0,0 +1,24 @@ +# Utilized by OSSFuzz to build the harness(es) for continuous fuzz-testing +# OSSFuzz defines the following environment variables, that this target relies upon: +# CXX, CFLAGS, LIB_FUZZING_ENGINE, OUT + +set(CMAKE_C_STANDARD 23) + +add_definitions(-DNDEBUG) # Do not want assertions + +if (DEFINED ENV{CFLAGS}) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}") +endif () + +add_executable(fuzz_entry fuzz_entry.c) +target_link_libraries(fuzz_entry PRIVATE ${PROJECT_NAME} $ENV{LIB_FUZZING_ENGINE}) + +add_executable(fuzz_stream fuzz_stream.c) +target_link_libraries(fuzz_stream PRIVATE ${PROJECT_NAME} $ENV{LIB_FUZZING_ENGINE}) + +if (DEFINED ENV{OUT}) + install(TARGETS fuzz_entry DESTINATION $ENV{OUT}) + install(TARGETS fuzz_stream DESTINATION $ENV{OUT}) +else () + message(WARNING "Cannot install if $OUT is not defined!") +endif ()
\ No newline at end of file diff --git a/libs/zip/fuzz/build.sh b/libs/zip/fuzz/build.sh new file mode 100644 index 0000000..b0003ad --- /dev/null +++ b/libs/zip/fuzz/build.sh @@ -0,0 +1,4 @@ +cd $SRC/zip + +mkdir -p build +cmake -S . -B build -DCMAKE_C_COMPILER_WORKS=1 -DZIP_BUILD_FUZZ=ON && cmake --build build --target install diff --git a/libs/zip/fuzz/fuzz_entry.c b/libs/zip/fuzz/fuzz_entry.c new file mode 100644 index 0000000..c2c5f0c --- /dev/null +++ b/libs/zip/fuzz/fuzz_entry.c @@ -0,0 +1,38 @@ +#include "zip.h" +#include <stdint.h> +#include <stdlib.h> + +int LLVMFuzzerTestOneInput(const uint8_t *data, const size_t size) { + /* Discard inputs larger than 1MB. */ + static const size_t MaxSize = 1024 * 1024; + if (size < 1 || size > MaxSize) { + return 0; + } + + void *buf = NULL; + size_t bufsize = 0; + struct zip_t *zip = zip_stream_open((const char *)data, size, 0, 'r'); + if (NULL == zip) { + goto end; + } + + const ssize_t zip_entries_count = zip_entries_total(zip); + + if (zip_entries_count <= 0) { + goto end; + } + + if (0 != zip_entry_openbyindex(zip, 0)) { + goto end; + } + + zip_entry_read(zip, &buf, &bufsize); + +end: + zip_entry_close(zip); + if (NULL != zip) { + zip_close(zip); + } + free(buf); + return 0; +} diff --git a/libs/zip/fuzz/fuzz_stream.c b/libs/zip/fuzz/fuzz_stream.c new file mode 100644 index 0000000..6c557de --- /dev/null +++ b/libs/zip/fuzz/fuzz_stream.c @@ -0,0 +1,40 @@ +#include "zip.h" +#include <assert.h> +#include <stdint.h> +#include <stdlib.h> + +int LLVMFuzzerTestOneInput(const uint8_t *data, const size_t size) { + /* Discard inputs larger than 1MB. */ + static const size_t MaxSize = 1024 * 1024; + if (size < 1 || size > MaxSize) { + return 0; + } + + char *outbuf = NULL; + size_t outbufsize = 0; + { + struct zip_t *zip = + zip_stream_open(NULL, 0, ZIP_DEFAULT_COMPRESSION_LEVEL, 'w'); + zip_entry_open(zip, "test"); + zip_entry_write(zip, data, size); + zip_entry_close(zip); + zip_stream_copy(zip, (void **)&outbuf, &outbufsize); + zip_stream_close(zip); + } + + void *inbuf = NULL; + size_t inbufsize = 0; + { + struct zip_t *zip = zip_stream_open(outbuf, outbufsize, 0, 'r'); + zip_entry_open(zip, "test"); + zip_entry_read(zip, &inbuf, &inbufsize); + zip_entry_close(zip); + zip_stream_close(zip); + } + free(inbuf); + free(outbuf); + + assert(inbufsize == size); + + return 0; +} |
