[libxml2] Check for feature flags in fuzzer tests



commit f9ccb3b818d37f7fc85017bf206e471cb37fbc82
Author: Nick Wellnhofer <wellnhofer aevum de>
Date:   Mon Feb 22 21:26:13 2021 +0100

    Check for feature flags in fuzzer tests

 fuzz/fuzz.h       | 21 ++++++++++++
 fuzz/genSeed.c    | 26 +++++++++++++--
 fuzz/testFuzzer.c | 98 +++++++++++++++++++++++++++++++++++--------------------
 3 files changed, 107 insertions(+), 38 deletions(-)
---
diff --git a/fuzz/fuzz.h b/fuzz/fuzz.h
index 8716af93..a51b3987 100644
--- a/fuzz/fuzz.h
+++ b/fuzz/fuzz.h
@@ -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);
 
diff --git a/fuzz/genSeed.c b/fuzz/genSeed.c
index 68fb87a1..2f038027 100644
--- a/fuzz/genSeed.c
+++ b/fuzz/genSeed.c
@@ -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);
 }
diff --git a/fuzz/testFuzzer.c b/fuzz/testFuzzer.c
index 678f3243..b0c7ffbc 100644
--- a/fuzz/testFuzzer.c
+++ b/fuzz/testFuzzer.c
@@ -12,41 +12,53 @@
 #include <libxml/xmlstring.h>
 #include "fuzz.h"
 
-#define LLVMFuzzerInitialize fuzzHtmlInit
-#define LLVMFuzzerTestOneInput fuzzHtml
-#include "html.c"
-#undef LLVMFuzzerInitialize
-#undef LLVMFuzzerTestOneInput
-
-#define LLVMFuzzerInitialize fuzzRegexpInit
-#define LLVMFuzzerTestOneInput fuzzRegexp
-#include "regexp.c"
-#undef LLVMFuzzerInitialize
-#undef LLVMFuzzerTestOneInput
-
-#define LLVMFuzzerInitialize fuzzSchemaInit
-#define LLVMFuzzerTestOneInput fuzzSchema
-#include "schema.c"
-#undef LLVMFuzzerInitialize
-#undef LLVMFuzzerTestOneInput
-
-#define LLVMFuzzerInitialize fuzzUriInit
-#define LLVMFuzzerTestOneInput fuzzUri
-#include "uri.c"
-#undef LLVMFuzzerInitialize
-#undef LLVMFuzzerTestOneInput
-
-#define LLVMFuzzerInitialize fuzzXmlInit
-#define LLVMFuzzerTestOneInput fuzzXml
-#include "xml.c"
-#undef LLVMFuzzerInitialize
-#undef LLVMFuzzerTestOneInput
-
-#define LLVMFuzzerInitialize fuzzXPathInit
-#define LLVMFuzzerTestOneInput fuzzXPath
-#include "xpath.c"
-#undef LLVMFuzzerInitialize
-#undef LLVMFuzzerTestOneInput
+#ifdef HAVE_HTML_FUZZER
+  #define LLVMFuzzerInitialize fuzzHtmlInit
+  #define LLVMFuzzerTestOneInput fuzzHtml
+  #include "html.c"
+  #undef LLVMFuzzerInitialize
+  #undef LLVMFuzzerTestOneInput
+#endif
+
+#ifdef HAVE_REGEXP_FUZZER
+  #define LLVMFuzzerInitialize fuzzRegexpInit
+  #define LLVMFuzzerTestOneInput fuzzRegexp
+  #include "regexp.c"
+  #undef LLVMFuzzerInitialize
+  #undef LLVMFuzzerTestOneInput
+#endif
+
+#ifdef HAVE_SCHEMA_FUZZER
+  #define LLVMFuzzerInitialize fuzzSchemaInit
+  #define LLVMFuzzerTestOneInput fuzzSchema
+  #include "schema.c"
+  #undef LLVMFuzzerInitialize
+  #undef LLVMFuzzerTestOneInput
+#endif
+
+#ifdef HAVE_URI_FUZZER
+  #define LLVMFuzzerInitialize fuzzUriInit
+  #define LLVMFuzzerTestOneInput fuzzUri
+  #include "uri.c"
+  #undef LLVMFuzzerInitialize
+  #undef LLVMFuzzerTestOneInput
+#endif
+
+#ifdef HAVE_XML_FUZZER
+  #define LLVMFuzzerInitialize fuzzXmlInit
+  #define LLVMFuzzerTestOneInput fuzzXml
+  #include "xml.c"
+  #undef LLVMFuzzerInitialize
+  #undef LLVMFuzzerTestOneInput
+#endif
+
+#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);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]