fuzz: remove UNITTEST dependency

Expose UTH flow builder to new 'FUZZ' define as well. Move UTHbufferToFile
as well and rename it to a more generic 'TestHelperBufferToFile'.

This way UNITTESTS can be disabled. This leads to smaller code size
and more realistic testing as in some parts of the code things
behave slightly differently when UNITTESTS are enabled.
pull/4793/head
Victor Julien 5 years ago
parent edcb784f1a
commit 794d9eeb83

@ -441,6 +441,7 @@
AM_CONDITIONAL([BUILD_FUZZTARGETS], [test "x$enable_fuzztargets" = "xyes"]) AM_CONDITIONAL([BUILD_FUZZTARGETS], [test "x$enable_fuzztargets" = "xyes"])
AC_PROG_CXX AC_PROG_CXX
AS_IF([test "x$enable_fuzztargets" = "xyes"], [ AS_IF([test "x$enable_fuzztargets" = "xyes"], [
AC_DEFINE([FUZZ], [1], [Fuzz targets are enabled])
AC_DEFINE([AFLFUZZ_NO_RANDOM], [1], [Disable all use of random functions]) AC_DEFINE([AFLFUZZ_NO_RANDOM], [1], [Disable all use of random functions])
CFLAGS_ORIG=$CFLAGS CFLAGS_ORIG=$CFLAGS
CFLAGS="-Werror" CFLAGS="-Werror"
@ -478,9 +479,6 @@ return 0;
# enable the running of unit tests # enable the running of unit tests
AC_ARG_ENABLE(unittests, AC_ARG_ENABLE(unittests,
AS_HELP_STRING([--enable-unittests], [Enable compilation of the unit tests]),[enable_unittests=$enableval],[enable_unittests=no]) AS_HELP_STRING([--enable-unittests], [Enable compilation of the unit tests]),[enable_unittests=$enableval],[enable_unittests=no])
AS_IF([test "x$enable_fuzztargets" = "xyes"], [
export enable_unittests="yes"
])
AS_IF([test "x$enable_unittests" = "xyes"], [ AS_IF([test "x$enable_unittests" = "xyes"], [
AC_DEFINE([UNITTESTS],[1],[Enable built-in unittests]) AC_DEFINE([UNITTESTS],[1],[Enable built-in unittests])
]) ])

@ -45,7 +45,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
alpd_tctx = AppLayerProtoDetectGetCtxThread(); alpd_tctx = AppLayerProtoDetectGetCtxThread();
} }
f = UTHBuildFlow(AF_INET, "1.2.3.4", "5.6.7.8", (data[2] << 8) | data[3], (data[4] << 8) | data[5]); f = TestHelperBuildFlow(AF_INET, "1.2.3.4", "5.6.7.8", (data[2] << 8) | data[3], (data[4] << 8) | data[5]);
if (f == NULL) { if (f == NULL) {
return 0; return 0;
} }
@ -69,7 +69,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
} }
} }
} }
UTHFreeFlow(f); FlowFree(f);
return 0; return 0;
} }

@ -83,7 +83,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
} }
//rewrite buffer to a file as libpcap does not have buffer inputs //rewrite buffer to a file as libpcap does not have buffer inputs
if (UTHbufferToFile("/tmp/fuzz.pcap", data, size) < 0) { if (TestHelperBufferToFile("/tmp/fuzz.pcap", data, size) < 0) {
return 0; return 0;
} }

@ -119,11 +119,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
} }
if (pos > 0 && pos < size) { if (pos > 0 && pos < size) {
// dump signatures to a file so as to reuse SigLoadSignatures // dump signatures to a file so as to reuse SigLoadSignatures
if (UTHbufferToFile(suricata.sig_file, data, pos-1) < 0) { if (TestHelperBufferToFile(suricata.sig_file, data, pos-1) < 0) {
return 0; return 0;
} }
} else { } else {
if (UTHbufferToFile(suricata.sig_file, data, pos) < 0) { if (TestHelperBufferToFile(suricata.sig_file, data, pos) < 0) {
return 0; return 0;
} }
} }
@ -139,7 +139,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
size -= pos; size -= pos;
//rewrite buffer to a file as libpcap does not have buffer inputs //rewrite buffer to a file as libpcap does not have buffer inputs
if (UTHbufferToFile("/tmp/fuzz.pcap", data, size) < 0) { if (TestHelperBufferToFile("/tmp/fuzz.pcap", data, size) < 0) {
return 0; return 0;
} }

@ -45,6 +45,80 @@
#include "util-unittest.h" #include "util-unittest.h"
#include "util-unittest-helper.h" #include "util-unittest-helper.h"
#if defined(UNITTESTS) || defined(FUZZ)
Flow *TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
{
struct in_addr in;
Flow *f = SCMalloc(sizeof(Flow));
if (unlikely(f == NULL)) {
printf("FlowAlloc failed\n");
;
return NULL;
}
memset(f, 0x00, sizeof(Flow));
FLOW_INITIALIZE(f);
if (family == AF_INET) {
f->flags |= FLOW_IPV4;
} else if (family == AF_INET6) {
f->flags |= FLOW_IPV6;
}
if (src != NULL) {
if (family == AF_INET) {
if (inet_pton(AF_INET, src, &in) != 1) {
printf("invalid address %s\n", src);
SCFree(f);
return NULL;
}
f->src.addr_data32[0] = in.s_addr;
} else {
BUG_ON(1);
}
}
if (dst != NULL) {
if (family == AF_INET) {
if (inet_pton(AF_INET, dst, &in) != 1) {
printf("invalid address %s\n", dst);
SCFree(f);
return NULL;
}
f->dst.addr_data32[0] = in.s_addr;
} else {
BUG_ON(1);
}
}
f->sp = sp;
f->dp = dp;
return f;
}
/** \brief writes the contents of a buffer into a file */
int TestHelperBufferToFile(const char *name, const uint8_t *data, size_t size)
{
if (remove(name) != 0) {
if (errno != ENOENT) {
printf("failed remove, errno=%d\n", errno);
return -1;
}
}
FILE *fd = fopen(name, "wb");
if (fd == NULL) {
printf("failed open, errno=%d\n", errno);
return -2;
}
if (fwrite (data, 1, size, fd) != size) {
fclose(fd);
return -3;
}
fclose(fd);
return 0;
}
#endif
#ifdef UNITTESTS #ifdef UNITTESTS
/** /**
@ -445,53 +519,7 @@ void UTHAssignFlow(Packet *p, Flow *f)
Flow *UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp) Flow *UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
{ {
struct in_addr in; return TestHelperBuildFlow(family, src, dst, sp, dp);
Flow *f = SCMalloc(sizeof(Flow));
if (unlikely(f == NULL)) {
printf("FlowAlloc failed\n");
;
return NULL;
}
memset(f, 0x00, sizeof(Flow));
FLOW_INITIALIZE(f);
if (family == AF_INET) {
f->flags |= FLOW_IPV4;
} else if (family == AF_INET6) {
f->flags |= FLOW_IPV6;
}
if (src != NULL) {
if (family == AF_INET) {
if (inet_pton(AF_INET, src, &in) != 1) {
printf("invalid address %s\n", src);
SCFree(f);
return NULL;
}
f->src.addr_data32[0] = in.s_addr;
} else {
BUG_ON(1);
}
}
if (dst != NULL) {
if (family == AF_INET) {
if (inet_pton(AF_INET, dst, &in) != 1) {
printf("invalid address %s\n", dst);
SCFree(f);
return NULL;
}
f->dst.addr_data32[0] = in.s_addr;
} else {
BUG_ON(1);
}
}
f->sp = sp;
f->dp = dp;
return f;
} }
void UTHFreeFlow(Flow *flow) void UTHFreeFlow(Flow *flow)
@ -942,28 +970,6 @@ int UTHParseSignature(const char *str, bool expect)
PASS; PASS;
} }
/** \brief writes the contents of a buffer into a file */
int UTHbufferToFile(const char * name, const uint8_t *data, size_t size) {
FILE * fd;
if (remove(name) != 0) {
if (errno != ENOENT) {
printf("failed remove, errno=%d\n", errno);
return -1;
}
}
fd = fopen(name, "wb");
if (fd == NULL) {
printf("failed open, errno=%d\n", errno);
return -2;
}
if (fwrite (data, 1, size, fd) != size) {
fclose(fd);
return -3;
}
fclose(fd);
return 0;
}
/* /*
* unittests for the unittest helpers * unittests for the unittest helpers
*/ */

@ -24,6 +24,10 @@
#ifndef __UTIL_UNITTEST_HELPER__ #ifndef __UTIL_UNITTEST_HELPER__
#define __UTIL_UNITTEST_HELPER__ #define __UTIL_UNITTEST_HELPER__
#if defined(UNITTESTS) || defined(FUZZ)
Flow *TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp);
int TestHelperBufferToFile(const char *name, const uint8_t *data, size_t size);
#endif
#ifdef UNITTESTS #ifdef UNITTESTS
uint32_t UTHSetIPv4Address(const char *); uint32_t UTHSetIPv4Address(const char *);
@ -63,7 +67,6 @@ Packet *UTHBuildPacketIPV6Real(uint8_t *, uint16_t , uint8_t ipproto, const char
void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len); void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len);
int UTHParseSignature(const char *str, bool expect); int UTHParseSignature(const char *str, bool expect);
int UTHbufferToFile(const char * name, const uint8_t *data, size_t size);
#endif #endif
void UTHRegisterTests(void); void UTHRegisterTests(void);

Loading…
Cancel
Save