From 02b5b3c242fd4131983152f0dd422429e6702923 Mon Sep 17 00:00:00 2001 From: Max Dymond Date: Fri, 28 Jun 2019 23:48:33 +0100 Subject: Move to using C rather than C++ for compilation --- Makefile | 1 + ossfuzz/Makefile | 12 +++++-- ossfuzz/compress_fuzzer.c | 26 ++++++++++++++++ ossfuzz/compress_fuzzer.cc | 26 ---------------- ossfuzz/decompress_fuzzer.c | 28 +++++++++++++++++ ossfuzz/decompress_fuzzer.cc | 28 ----------------- ossfuzz/standaloneengine.c | 74 ++++++++++++++++++++++++++++++++++++++++++++ ossfuzz/standaloneengine.cc | 74 -------------------------------------------- ossfuzz/testinput.h | 2 +- 9 files changed, 139 insertions(+), 132 deletions(-) create mode 100644 ossfuzz/compress_fuzzer.c delete mode 100644 ossfuzz/compress_fuzzer.cc create mode 100644 ossfuzz/decompress_fuzzer.c delete mode 100644 ossfuzz/decompress_fuzzer.cc create mode 100644 ossfuzz/standaloneengine.c delete mode 100644 ossfuzz/standaloneengine.cc diff --git a/Makefile b/Makefile index 34835fd..f25f951 100644 --- a/Makefile +++ b/Makefile @@ -77,6 +77,7 @@ clean: @$(MAKE) -C $(PRGDIR) $@ > $(VOID) @$(MAKE) -C $(TESTDIR) $@ > $(VOID) @$(MAKE) -C $(EXDIR) $@ > $(VOID) + @$(MAKE) -C $(FUZZDIR) $@ > $(VOID) @$(MAKE) -C contrib/gen_manual $@ > $(VOID) @$(RM) lz4$(EXT) @echo Cleaning completed diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile index 1e7679b..1480ccb 100644 --- a/ossfuzz/Makefile +++ b/ossfuzz/Makefile @@ -42,14 +42,20 @@ include ../Makefile.inc $(LZ4DIR)/liblz4.a: $(MAKE) -C $(LZ4DIR) CFLAGS="$(CFLAGS)" liblz4.a -%.o: %.cc - $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $< -o $@ +%.o: %.c + $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ # Generic rule for generating fuzzers %_fuzzer: %_fuzzer.o $(LZ4DIR)/liblz4.a # Compile the standalone code just in case. The OSS-Fuzz code might # override the LIB_FUZZING_ENGINE value to "-fsanitize=fuzzer" - $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) standaloneengine.cc -o standaloneengine.o + $(CC) -c $(CFLAGS) $(CPPFLAGS) standaloneengine.c -o standaloneengine.o # Now compile the actual fuzzer. $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LIB_FUZZING_ENGINE) $^ -o $@$(EXT) + +%_fuzzer_clean: + $(RM) $*_fuzzer $*_fuzzer.o standaloneengine.o + +.PHONY: clean +clean: compress_fuzzer_clean decompress_fuzzer_clean diff --git a/ossfuzz/compress_fuzzer.c b/ossfuzz/compress_fuzzer.c new file mode 100644 index 0000000..28610ad --- /dev/null +++ b/ossfuzz/compress_fuzzer.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include "lz4.h" + +#define CHECK(COND) if (!(COND)) { abort(); } + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + size_t const compressed_dest_size = LZ4_compressBound(size); + char *const dest_buffer = (char *)malloc(compressed_dest_size); + + if (dest_buffer != NULL) + { + // Allocation succeeded, try compressing the incoming data. + int result = LZ4_compress_default((const char*)data, + dest_buffer, + size, + compressed_dest_size); + CHECK(result != 0); + + free(dest_buffer); + } + + return 0; +} diff --git a/ossfuzz/compress_fuzzer.cc b/ossfuzz/compress_fuzzer.cc deleted file mode 100644 index 4a720e2..0000000 --- a/ossfuzz/compress_fuzzer.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include -#include "lz4.h" - -#define CHECK(COND) if (!(COND)) { abort(); } - -extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) -{ - size_t const compressed_dest_size = LZ4_compressBound(size); - char *const dest_buffer = (char *)malloc(compressed_dest_size); - - if (dest_buffer != NULL) - { - // Allocation succeeded, try compressing the incoming data. - int result = LZ4_compress_default((const char*)data, - dest_buffer, - size, - compressed_dest_size); - CHECK(result != 0); - - free(dest_buffer); - } - - return 0; -} diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c new file mode 100644 index 0000000..1fa2b1a --- /dev/null +++ b/ossfuzz/decompress_fuzzer.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include "lz4.h" + +#define CHECK(COND) if (!(COND)) { abort(); } + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + size_t const buffer_size = 10 * 1024 * 1024; + char *const dest_buffer = (char *)malloc(buffer_size); + + if (dest_buffer != NULL) + { + // Allocation succeeded, try decompressing the incoming data. + int result = LZ4_decompress_safe((const char*)data, + dest_buffer, + size, + buffer_size); + + // Ignore the result of decompression. + (void)result; + + free(dest_buffer); + } + + return 0; +} diff --git a/ossfuzz/decompress_fuzzer.cc b/ossfuzz/decompress_fuzzer.cc deleted file mode 100644 index 594a5af..0000000 --- a/ossfuzz/decompress_fuzzer.cc +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -#include "lz4.h" - -#define CHECK(COND) if (!(COND)) { abort(); } - -extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) -{ - size_t const buffer_size = 10 * 1024 * 1024; - char *const dest_buffer = (char *)malloc(buffer_size); - - if (dest_buffer != NULL) - { - // Allocation succeeded, try decompressing the incoming data. - int result = LZ4_decompress_safe((const char*)data, - dest_buffer, - size, - buffer_size); - - // Ignore the result of decompression. - (void)result; - - free(dest_buffer); - } - - return 0; -} diff --git a/ossfuzz/standaloneengine.c b/ossfuzz/standaloneengine.c new file mode 100644 index 0000000..175360e --- /dev/null +++ b/ossfuzz/standaloneengine.c @@ -0,0 +1,74 @@ +#include +#include +#include + +#include "testinput.h" + +/** + * Main procedure for standalone fuzzing engine. + * + * Reads filenames from the argument array. For each filename, read the file + * into memory and then call the fuzzing interface with the data. + */ +int main(int argc, char **argv) +{ + int ii; + for(ii = 1; ii < argc; ii++) + { + FILE *infile; + printf("[%s] ", argv[ii]); + + /* Try and open the file. */ + infile = fopen(argv[ii], "rb"); + if(infile) + { + uint8_t *buffer = NULL; + size_t buffer_len; + + printf("Opened.. "); + + /* Get the length of the file. */ + fseek(infile, 0L, SEEK_END); + buffer_len = ftell(infile); + + /* Reset the file indicator to the beginning of the file. */ + fseek(infile, 0L, SEEK_SET); + + /* Allocate a buffer for the file contents. */ + buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t)); + if(buffer) + { + /* Read all the text from the file into the buffer. */ + fread(buffer, sizeof(uint8_t), buffer_len, infile); + printf("Read %zu bytes, fuzzing.. ", buffer_len); + + /* Call the fuzzer with the data. */ + LLVMFuzzerTestOneInput(buffer, buffer_len); + + printf("complete !!"); + + /* Free the buffer as it's no longer needed. */ + free(buffer); + buffer = NULL; + } + else + { + fprintf(stderr, + "[%s] Failed to allocate %zu bytes \n", + argv[ii], + buffer_len); + } + + /* Close the file as it's no longer needed. */ + fclose(infile); + infile = NULL; + } + else + { + /* Failed to open the file. Maybe wrong name or wrong permissions? */ + fprintf(stderr, "[%s] Open failed. \n", argv[ii]); + } + + printf("\n"); + } +} diff --git a/ossfuzz/standaloneengine.cc b/ossfuzz/standaloneengine.cc deleted file mode 100644 index 175360e..0000000 --- a/ossfuzz/standaloneengine.cc +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include -#include - -#include "testinput.h" - -/** - * Main procedure for standalone fuzzing engine. - * - * Reads filenames from the argument array. For each filename, read the file - * into memory and then call the fuzzing interface with the data. - */ -int main(int argc, char **argv) -{ - int ii; - for(ii = 1; ii < argc; ii++) - { - FILE *infile; - printf("[%s] ", argv[ii]); - - /* Try and open the file. */ - infile = fopen(argv[ii], "rb"); - if(infile) - { - uint8_t *buffer = NULL; - size_t buffer_len; - - printf("Opened.. "); - - /* Get the length of the file. */ - fseek(infile, 0L, SEEK_END); - buffer_len = ftell(infile); - - /* Reset the file indicator to the beginning of the file. */ - fseek(infile, 0L, SEEK_SET); - - /* Allocate a buffer for the file contents. */ - buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t)); - if(buffer) - { - /* Read all the text from the file into the buffer. */ - fread(buffer, sizeof(uint8_t), buffer_len, infile); - printf("Read %zu bytes, fuzzing.. ", buffer_len); - - /* Call the fuzzer with the data. */ - LLVMFuzzerTestOneInput(buffer, buffer_len); - - printf("complete !!"); - - /* Free the buffer as it's no longer needed. */ - free(buffer); - buffer = NULL; - } - else - { - fprintf(stderr, - "[%s] Failed to allocate %zu bytes \n", - argv[ii], - buffer_len); - } - - /* Close the file as it's no longer needed. */ - fclose(infile); - infile = NULL; - } - else - { - /* Failed to open the file. Maybe wrong name or wrong permissions? */ - fprintf(stderr, "[%s] Open failed. \n", argv[ii]); - } - - printf("\n"); - } -} diff --git a/ossfuzz/testinput.h b/ossfuzz/testinput.h index 6ab9b51..8da6215 100644 --- a/ossfuzz/testinput.h +++ b/ossfuzz/testinput.h @@ -1,3 +1,3 @@ #include -extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); -- cgit v0.12