Skip to content

Commit

Permalink
support 8-, 16-, and 32-bit modes
Browse files Browse the repository at this point in the history
  • Loading branch information
addisoncrump committed Nov 14, 2023
1 parent bb2d53e commit 11e0d2f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ pcre2posix_test.exe
pcre2posix_test.log
pcre2posix_test.trs
pcre2demo
pcre2fuzzcheck
pcre2fuzzcheck-*
pcre2fuzzer-*
pcre2grep
pcre2grep.exe
pcre2test
Expand Down
52 changes: 44 additions & 8 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 36 additions & 3 deletions src/pcre2_fuzzsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ Written by Philip Hazel, October 2016
#include <stdlib.h>
#include <string.h>

#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
Expand Down Expand Up @@ -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++)
Expand All @@ -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
}
}
}
Expand All @@ -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);

Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
}

Expand Down

0 comments on commit 11e0d2f

Please sign in to comment.