| 1 |
/* |
| 2 |
* schema.c: a libFuzzer target to test the XML Schema processor. |
| 3 |
* |
| 4 |
* See Copyright for the status of this software. |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef XML_DEPRECATED |
| 8 |
#define XML_DEPRECATED |
| 9 |
#endif |
| 10 |
|
| 11 |
#include <libxml/catalog.h> |
| 12 |
#include <libxml/xmlschemas.h> |
| 13 |
#include <libxml/xmlschemastypes.h> |
| 14 |
#include "fuzz.h" |
| 15 |
|
| 16 |
int |
| 17 |
LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED, |
| 18 |
char ***argv ATTRIBUTE_UNUSED) { |
| 19 |
xmlFuzzMemSetup(); |
| 20 |
xmlInitParser(); |
| 21 |
#ifdef LIBXML_CATALOG_ENABLED |
| 22 |
xmlInitializeCatalog(); |
| 23 |
xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE); |
| 24 |
#endif |
| 25 |
|
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
int |
| 30 |
LLVMFuzzerTestOneInput(const char *data, size_t size) { |
| 31 |
xmlSchemaParserCtxtPtr pctxt; |
| 32 |
xmlSchemaPtr schema; |
| 33 |
size_t failurePos; |
| 34 |
|
| 35 |
if (size > 200000) |
| 36 |
return(0); |
| 37 |
|
| 38 |
xmlFuzzDataInit(data, size); |
| 39 |
|
| 40 |
failurePos = xmlFuzzReadInt(4) % (size + 100); |
| 41 |
|
| 42 |
xmlFuzzReadEntities(); |
| 43 |
|
| 44 |
xmlFuzzInjectFailure(failurePos); |
| 45 |
pctxt = xmlSchemaNewParserCtxt(xmlFuzzMainUrl()); |
| 46 |
xmlSchemaSetParserStructuredErrors(pctxt, xmlFuzzSErrorFunc, NULL); |
| 47 |
xmlSchemaSetResourceLoader(pctxt, xmlFuzzResourceLoader, NULL); |
| 48 |
schema = xmlSchemaParse(pctxt); |
| 49 |
xmlSchemaFreeParserCtxt(pctxt); |
| 50 |
|
| 51 |
if (schema != NULL) { |
| 52 |
xmlSchemaValidCtxtPtr vctxt; |
| 53 |
xmlParserCtxtPtr ctxt; |
| 54 |
xmlDocPtr doc; |
| 55 |
|
| 56 |
ctxt = xmlNewParserCtxt(); |
| 57 |
xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL); |
| 58 |
xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL); |
| 59 |
doc = xmlCtxtReadFile(ctxt, xmlFuzzSecondaryUrl(), NULL, |
| 60 |
XML_PARSE_NOENT); |
| 61 |
xmlFreeParserCtxt(ctxt); |
| 62 |
|
| 63 |
vctxt = xmlSchemaNewValidCtxt(schema); |
| 64 |
xmlSchemaSetValidStructuredErrors(vctxt, xmlFuzzSErrorFunc, NULL); |
| 65 |
xmlSchemaValidateDoc(vctxt, doc); |
| 66 |
xmlSchemaFreeValidCtxt(vctxt); |
| 67 |
|
| 68 |
xmlFreeDoc(doc); |
| 69 |
xmlSchemaFree(schema); |
| 70 |
} |
| 71 |
|
| 72 |
xmlFuzzInjectFailure(0); |
| 73 |
xmlFuzzDataCleanup(); |
| 74 |
xmlResetLastError(); |
| 75 |
xmlSchemaCleanupTypes(); |
| 76 |
|
| 77 |
return(0); |
| 78 |
} |
| 79 |
|
| 80 |
size_t |
| 81 |
LLVMFuzzerCustomMutator(char *data, size_t size, size_t maxSize, |
| 82 |
unsigned seed) { |
| 83 |
static const xmlFuzzChunkDesc chunks[] = { |
| 84 |
{ 4, XML_FUZZ_PROB_ONE / 10 }, /* failurePos */ |
| 85 |
{ 0, 0 } |
| 86 |
}; |
| 87 |
|
| 88 |
return xmlFuzzMutateChunks(chunks, data, size, maxSize, seed, |
| 89 |
LLVMFuzzerMutate); |
| 90 |
} |
| 91 |
|