diff --git a/.gitignore b/.gitignore index e55689a2d..b69a1d82b 100644 --- a/.gitignore +++ b/.gitignore @@ -50,7 +50,8 @@ pcre2posix_test.exe pcre2posix_test.log pcre2posix_test.trs pcre2demo -pcre2fuzzcheck +pcre2fuzzcheck-* +pcre2fuzzer-* pcre2grep pcre2grep.exe pcre2test diff --git a/Makefile.am b/Makefile.am index 13d1bc8c2..3e3eeb4f8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -540,28 +540,64 @@ if WITH_GCOV pcre2grep_CFLAGS += $(GCOV_CFLAGS) pcre2grep_LDADD += $(GCOV_LIBS) endif # WITH_GCOV +endif # WITH_PCRE2_8 ## If fuzzer support is enabled, build a non-distributed library containing the ## fuzzing function. Also build the standalone checking binary from the same ## source but using -DSTANDALONE. if WITH_FUZZ_SUPPORT -noinst_LIBRARIES = .libs/libpcre2-fuzzsupport.a +noinst_LIBRARIES = +if WITH_PCRE2_8 +noinst_LIBRARIES += .libs/libpcre2-fuzzsupport.a _libs_libpcre2_fuzzsupport_a_SOURCES = src/pcre2_fuzzsupport.c _libs_libpcre2_fuzzsupport_a_CFLAGS = $(AM_CFLAGS) _libs_libpcre2_fuzzsupport_a_LIBADD = -noinst_PROGRAMS += pcre2fuzzcheck -pcre2fuzzcheck_SOURCES = src/pcre2_fuzzsupport.c -pcre2fuzzcheck_CFLAGS = -DSTANDALONE $(AM_CFLAGS) -pcre2fuzzcheck_LDADD = libpcre2-8.la +noinst_PROGRAMS += pcre2fuzzcheck-8 +pcre2fuzzcheck_8_SOURCES = src/pcre2_fuzzsupport.c +pcre2fuzzcheck_8_CFLAGS = -DSTANDALONE $(AM_CFLAGS) +pcre2fuzzcheck_8_LDADD = libpcre2-8.la if WITH_GCOV -pcre2fuzzcheck_CFLAGS += $(GCOV_CFLAGS) -pcre2fuzzcheck_LDADD += $(GCOV_LIBS) +pcre2fuzzcheck_8_CFLAGS += $(GCOV_CFLAGS) +pcre2fuzzcheck_8_LDADD += $(GCOV_LIBS) endif # WITH_GCOV -endif # WITH FUZZ_SUPPORT endif # WITH_PCRE2_8 +if WITH_PCRE2_16 +noinst_LIBRARIES += .libs/libpcre2-fuzzsupport-16.a +_libs_libpcre2_fuzzsupport_16_a_SOURCES = src/pcre2_fuzzsupport.c +_libs_libpcre2_fuzzsupport_16_a_CFLAGS = $(AM_CFLAGS) -DPCRE2_CODE_UNIT_WIDTH=16 +_libs_libpcre2_fuzzsupport_16_a_LIBADD = + +noinst_PROGRAMS += pcre2fuzzcheck-16 +pcre2fuzzcheck_16_SOURCES = src/pcre2_fuzzsupport.c +pcre2fuzzcheck_16_CFLAGS = -DSTANDALONE $(AM_CFLAGS) -DPCRE2_CODE_UNIT_WIDTH=16 +pcre2fuzzcheck_16_LDADD = libpcre2-16.la +if WITH_GCOV +pcre2fuzzcheck_16_CFLAGS += $(GCOV_CFLAGS) +pcre2fuzzcheck_16_LDADD += $(GCOV_LIBS) +endif # WITH_GCOV +endif # WITH_PCRE2_16 + +if WITH_PCRE2_32 +noinst_LIBRARIES += .libs/libpcre2-fuzzsupport-32.a +_libs_libpcre2_fuzzsupport_32_a_SOURCES = src/pcre2_fuzzsupport.c +_libs_libpcre2_fuzzsupport_32_a_CFLAGS = $(AM_CFLAGS) -DPCRE2_CODE_UNIT_WIDTH=32 +_libs_libpcre2_fuzzsupport_32_a_LIBADD = + +noinst_PROGRAMS += pcre2fuzzcheck-32 +pcre2fuzzcheck_32_SOURCES = src/pcre2_fuzzsupport.c +pcre2fuzzcheck_32_CFLAGS = -DSTANDALONE $(AM_CFLAGS) -DPCRE2_CODE_UNIT_WIDTH=32 +pcre2fuzzcheck_32_LDADD = libpcre2-32.la +if WITH_GCOV +pcre2fuzzcheck_32_CFLAGS += $(GCOV_CFLAGS) +pcre2fuzzcheck_32_LDADD += $(GCOV_LIBS) +endif # WITH_GCOV +endif # WITH_PCRE2_32 + +endif # WITH_FUZZ_SUPPORT + ## -------- Testing ---------- ## If the 8-bit library is enabled, build the POSIX wrapper test program and diff --git a/src/pcre2_fuzzsupport.c b/src/pcre2_fuzzsupport.c index f226b765b..e635eb3c2 100644 --- a/src/pcre2_fuzzsupport.c +++ b/src/pcre2_fuzzsupport.c @@ -14,8 +14,11 @@ Written by Philip Hazel, October 2016 #include #include -#include "config.h" +#ifndef PCRE2_CODE_UNIT_WIDTH #define PCRE2_CODE_UNIT_WIDTH 8 +#endif + +#include "config.h" #include "pcre2.h" #define MAX_MATCH_SIZE 1000 @@ -86,7 +89,9 @@ fprintf(stream, "%s%s%s%s%s%s%s%s%s\n", static void dump_matches(FILE *stream, int count, pcre2_match_data *match_data, pcre2_match_context *match_context) { +#if PCRE2_CODE_UNIT_WIDTH == 8 PCRE2_UCHAR error_buf[256]; +#endif int errorcode; for (uint32_t index = 0; index < count; index++) @@ -107,8 +112,12 @@ for (uint32_t index = 0; index < count; index++) } else { +#if PCRE2_CODE_UNIT_WIDTH == 8 pcre2_get_error_message(errorcode, error_buf, 256); fprintf(stream, "Match %d failed: %s\n", index, error_buf); +#else + fprintf(stream, "Match %d failed: %d\n", index, errorcode); +#endif } } } @@ -130,7 +139,9 @@ static void describe_failure( pcre2_match_data *match_data_jit, pcre2_match_context *match_context ) { +#if PCRE2_CODE_UNIT_WIDTH == 8 PCRE2_UCHAR buffer[256]; +#endif fprintf(stderr, "Encountered failure while performing %s; context:\n", task); @@ -146,8 +157,12 @@ print_match_options(stderr, match_options); if (errorcode < 0) { +#if PCRE2_CODE_UNIT_WIDTH == 8 pcre2_get_error_message(errorcode, buffer, 256); fprintf(stderr, "Non-JIT'd operation emitted an error: %s (%d)\n", buffer, errorcode); +#else + fprintf(stderr, "Non-JIT'd operation emitted an error: %d\n", errorcode); +#endif } if (matches >= 0) { @@ -162,8 +177,12 @@ if (matches >= 0) if (errorcode_jit < 0) { +#if PCRE2_CODE_UNIT_WIDTH == 8 pcre2_get_error_message(errorcode_jit, buffer, 256); fprintf(stderr, "JIT'd operation emitted an error: %s (%d)\n", buffer, errorcode_jit); +#else + fprintf(stderr, "JIT'd operation emitted an error: %d\n", errorcode); +#endif } if (matches_jit >= 0) { @@ -220,6 +239,7 @@ in large trees taking too much time. */ random_options = *(uint64_t *)(data); data += sizeof(random_options); size -= sizeof(random_options); +size /= PCRE2_CODE_UNIT_WIDTH / 8; match_size = (size > MAX_MATCH_SIZE)? MAX_MATCH_SIZE : size; @@ -323,9 +343,13 @@ for (i = 0; i < 2; i++) #ifdef STANDALONE if (errorcode >= 0) printf("Match returned %d\n", errorcode); else { +#if PCRE2_CODE_UNIT_WIDTH == 8 unsigned char buffer[256]; pcre2_get_error_message(errorcode, buffer, 256); printf("Match failed: error %d: %s\n", errorcode, buffer); +#else + printf("Match failed: error %d\n", errorcode); +#endif } #endif @@ -417,9 +441,13 @@ for (i = 0; i < 2; i++) #ifdef STANDALONE if (errorcode >= 0) printf("Match returned %d\n", errorcode); else { +#if PCRE2_CODE_UNIT_WIDTH == 8 unsigned char buffer[256]; pcre2_get_error_message(errorcode, buffer, 256); printf("Match failed: error %d: %s\n", errorcode, buffer); +#else + printf("Match failed: error %d\n", errorcode); +#endif } #endif @@ -434,12 +462,17 @@ for (i = 0; i < 2; i++) else { +#ifdef STANDALONE +#if PCRE2_CODE_UNIT_WIDTH == 8 unsigned char buffer[256]; pcre2_get_error_message(errorcode, buffer, 256); -#ifdef STANDALONE printf("Error %d at offset %lu: %s\n", errorcode, erroroffset, buffer); #else - if (strstr((const char *)buffer, "internal error") != NULL) abort(); + printf("Error %d at offset %lu\n", errorcode, erroroffset); +#endif + +#else + if (errorcode == PCRE2_ERROR_INTERNAL) abort(); #endif }