1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-02-06 05:57:39 +03:00

Check for feature flags in fuzzer tests

This commit is contained in:
Nick Wellnhofer 2021-02-22 21:26:13 +01:00
parent 88c657d643
commit f9ccb3b818
3 changed files with 102 additions and 33 deletions

View File

@ -15,6 +15,27 @@
extern "C" {
#endif
#if defined(LIBXML_HTML_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
#define HAVE_HTML_FUZZER
#endif
#if defined(LIBXML_REGEXP_ENABLED)
#define HAVE_REGEXP_FUZZER
#endif
#if defined(LIBXML_SCHEMAS_ENABLED)
#define HAVE_SCHEMA_FUZZER
#endif
#if 1
#define HAVE_URI_FUZZER
#endif
#if defined(LIBXML_OUTPUT_ENABLED) && \
defined(LIBXML_READER_ENABLED) && \
defined(LIBXML_XINCLUDE_ENABLED)
#define HAVE_XML_FUZZER
#endif
#if defined(LIBXML_XPATH_ENABLED)
#define HAVE_XPATH_FUZZER
#endif
int
LLVMFuzzerInitialize(int *argc, char ***argv);

View File

@ -102,6 +102,7 @@ fuzzRecorderCleanup() {
globalData.oldLoader = NULL;
}
#ifdef HAVE_XML_FUZZER
static int
processXml(const char *docFile, FILE *out) {
int opts = XML_PARSE_NOENT | XML_PARSE_DTDLOAD;
@ -119,7 +120,9 @@ processXml(const char *docFile, FILE *out) {
return(0);
}
#endif
#ifdef HAVE_HTML_FUZZER
static int
processHtml(const char *docFile, FILE *out) {
char buf[SEED_BUF_SIZE];
@ -144,7 +147,9 @@ processHtml(const char *docFile, FILE *out) {
return(0);
}
#endif
#ifdef HAVE_SCHEMA_FUZZER
static int
processSchema(const char *docFile, FILE *out) {
xmlSchemaPtr schema;
@ -162,6 +167,7 @@ processSchema(const char *docFile, FILE *out) {
return(0);
}
#endif
static int
processPattern(const char *pattern) {
@ -240,6 +246,7 @@ error:
return(ret);
}
#ifdef HAVE_XPATH_FUZZER
static int
processXPath(const char *testDir, const char *prefix, const char *name,
const char *data, const char *subdir, int xptr) {
@ -363,10 +370,11 @@ processXPathDir(const char *testDir) {
return(ret);
}
#endif
int
main(int argc, const char **argv) {
mainFunc processArg = processPattern;
mainFunc processArg = NULL;
const char *fuzzer;
int ret = 0;
int xpath = 0;
@ -381,13 +389,24 @@ main(int argc, const char **argv) {
fuzzer = argv[1];
if (strcmp(fuzzer, "html") == 0) {
#ifdef HAVE_HTML_FUZZER
processArg = processPattern;
globalData.processFile = processHtml;
#endif
} else if (strcmp(fuzzer, "schema") == 0) {
#ifdef HAVE_SCHEMA_FUZZER
processArg = processPattern;
globalData.processFile = processSchema;
#endif
} else if (strcmp(fuzzer, "xml") == 0) {
#ifdef HAVE_XML_FUZZER
processArg = processPattern;
globalData.processFile = processXml;
#endif
} else if (strcmp(fuzzer, "xpath") == 0) {
#ifdef HAVE_XPATH_FUZZER
processArg = processXPathDir;
#endif
} else {
fprintf(stderr, "unknown fuzzer %s\n", fuzzer);
return(1);
@ -399,8 +418,9 @@ main(int argc, const char **argv) {
return(1);
}
for (i = 2; i < argc; i++)
processArg(argv[i]);
if (processArg != NULL)
for (i = 2; i < argc; i++)
processArg(argv[i]);
return(ret);
}

View File

@ -12,41 +12,53 @@
#include <libxml/xmlstring.h>
#include "fuzz.h"
#define LLVMFuzzerInitialize fuzzHtmlInit
#define LLVMFuzzerTestOneInput fuzzHtml
#include "html.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#ifdef HAVE_HTML_FUZZER
#define LLVMFuzzerInitialize fuzzHtmlInit
#define LLVMFuzzerTestOneInput fuzzHtml
#include "html.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#endif
#define LLVMFuzzerInitialize fuzzRegexpInit
#define LLVMFuzzerTestOneInput fuzzRegexp
#include "regexp.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#ifdef HAVE_REGEXP_FUZZER
#define LLVMFuzzerInitialize fuzzRegexpInit
#define LLVMFuzzerTestOneInput fuzzRegexp
#include "regexp.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#endif
#define LLVMFuzzerInitialize fuzzSchemaInit
#define LLVMFuzzerTestOneInput fuzzSchema
#include "schema.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#ifdef HAVE_SCHEMA_FUZZER
#define LLVMFuzzerInitialize fuzzSchemaInit
#define LLVMFuzzerTestOneInput fuzzSchema
#include "schema.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#endif
#define LLVMFuzzerInitialize fuzzUriInit
#define LLVMFuzzerTestOneInput fuzzUri
#include "uri.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#ifdef HAVE_URI_FUZZER
#define LLVMFuzzerInitialize fuzzUriInit
#define LLVMFuzzerTestOneInput fuzzUri
#include "uri.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#endif
#define LLVMFuzzerInitialize fuzzXmlInit
#define LLVMFuzzerTestOneInput fuzzXml
#include "xml.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#ifdef HAVE_XML_FUZZER
#define LLVMFuzzerInitialize fuzzXmlInit
#define LLVMFuzzerTestOneInput fuzzXml
#include "xml.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#endif
#define LLVMFuzzerInitialize fuzzXPathInit
#define LLVMFuzzerTestOneInput fuzzXPath
#include "xpath.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#ifdef HAVE_XPATH_FUZZER
#define LLVMFuzzerInitialize fuzzXPathInit
#define LLVMFuzzerTestOneInput fuzzXPath
#include "xpath.c"
#undef LLVMFuzzerInitialize
#undef LLVMFuzzerTestOneInput
#endif
typedef int
(*initFunc)(int *argc, char ***argv);
@ -91,6 +103,7 @@ error:
return(ret);
}
#ifdef HAVE_XML_FUZZER
static int
testEntityLoader() {
static const char data[] =
@ -132,25 +145,40 @@ testEntityLoader() {
return(ret);
}
#endif
int
main() {
int ret = 0;
#ifdef HAVE_XML_FUZZER
if (testEntityLoader() != 0)
ret = 1;
#endif
#ifdef HAVE_HTML_FUZZER
if (testFuzzer(fuzzHtmlInit, fuzzHtml, "seed/html/*") != 0)
ret = 1;
#endif
#ifdef HAVE_REGEXP_FUZZER
if (testFuzzer(fuzzRegexpInit, fuzzRegexp, "seed/regexp/*") != 0)
ret = 1;
#endif
#ifdef HAVE_SCHEMA_FUZZER
if (testFuzzer(fuzzSchemaInit, fuzzSchema, "seed/schema/*") != 0)
ret = 1;
#endif
#ifdef HAVE_URI_FUZZER
if (testFuzzer(NULL, fuzzUri, "seed/uri/*") != 0)
ret = 1;
#endif
#ifdef HAVE_XML_FUZZER
if (testFuzzer(fuzzXmlInit, fuzzXml, "seed/xml/*") != 0)
ret = 1;
#endif
#ifdef HAVE_XPATH_FUZZER
if (testFuzzer(fuzzXPathInit, fuzzXPath, "seed/xpath/*") != 0)
ret = 1;
#endif
if (ret == 0)
printf("Successfully tested %d inputs\n", numInputs);