Debian Codegraph @

fuzz/xinclude.c

raw
1 /*
2 * xinclude.c: a libFuzzer target to test the XInclude engine.
3 *
4 * See Copyright for the status of this software.
5 */
6
7 #include <libxml/catalog.h>
8 #include <libxml/parser.h>
9 #include <libxml/tree.h>
10 #include <libxml/xmlerror.h>
11 #include <libxml/xinclude.h>
12 #include "fuzz.h"
13
14 int
15 LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED,
16 char ***argv ATTRIBUTE_UNUSED) {
17 xmlFuzzMemSetup();
18 xmlInitParser();
19 #ifdef LIBXML_CATALOG_ENABLED
20 xmlInitializeCatalog();
21 xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE);
22 #endif
23 xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
24
25 return 0;
26 }
27
28 int
29 LLVMFuzzerTestOneInput(const char *data, size_t size) {
30 xmlParserCtxtPtr ctxt;
31 xmlDocPtr doc;
32 const char *docBuffer, *docUrl;
33 size_t failurePos, docSize;
34 int opts;
35
36 xmlFuzzDataInit(data, size);
37 opts = (int) xmlFuzzReadInt(4);
38 opts &= ~XML_PARSE_DTDVALID &
39 ~XML_PARSE_SAX1;
40 failurePos = xmlFuzzReadInt(4) % (size + 100);
41
42 xmlFuzzReadEntities();
43 docBuffer = xmlFuzzMainEntity(&docSize);
44 docUrl = xmlFuzzMainUrl();
45 if (docBuffer == NULL)
46 goto exit;
47
48 /* Pull parser */
49
50 xmlFuzzInjectFailure(failurePos);
51 ctxt = xmlNewParserCtxt();
52 if (ctxt != NULL) {
53 xmlXIncludeCtxtPtr xinc;
54 xmlDocPtr copy;
55
56 xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL);
57
58 doc = xmlCtxtReadMemory(ctxt, docBuffer, docSize, docUrl, NULL, opts);
59 xmlFuzzCheckFailureReport("xmlCtxtReadMemory",
60 doc == NULL && ctxt->errNo == XML_ERR_NO_MEMORY,
61 doc == NULL && ctxt->errNo == XML_IO_EIO);
62
63 xinc = xmlXIncludeNewContext(doc);
64 xmlXIncludeSetResourceLoader(xinc, xmlFuzzResourceLoader, NULL);
65 xmlXIncludeSetFlags(xinc, opts);
66 xmlXIncludeProcessNode(xinc, (xmlNodePtr) doc);
67 if (doc != NULL) {
68 xmlFuzzCheckFailureReport("xmlXIncludeProcessNode",
69 xinc == NULL ||
70 xmlXIncludeGetLastError(xinc) == XML_ERR_NO_MEMORY,
71 xinc != NULL &&
72 xmlXIncludeGetLastError(xinc) == XML_IO_EIO);
73 }
74 xmlXIncludeFreeContext(xinc);
75
76 xmlFuzzResetFailure();
77 copy = xmlCopyDoc(doc, 1);
78 if (doc != NULL)
79 xmlFuzzCheckFailureReport("xmlCopyNode", copy == NULL, 0);
80 xmlFreeDoc(copy);
81
82 xmlFreeDoc(doc);
83 xmlFreeParserCtxt(ctxt);
84 }
85
86 exit:
87 xmlFuzzInjectFailure(0);
88 xmlFuzzDataCleanup();
89 xmlResetLastError();
90 return(0);
91 }
92
93 size_t
94 LLVMFuzzerCustomMutator(char *data, size_t size, size_t maxSize,
95 unsigned seed) {
96 static const xmlFuzzChunkDesc chunks[] = {
97 { 4, XML_FUZZ_PROB_ONE / 10 }, /* opts */
98 { 4, XML_FUZZ_PROB_ONE / 10 }, /* failurePos */
99 { 0, 0 }
100 };
101
102 return xmlFuzzMutateChunks(chunks, data, size, maxSize, seed,
103 LLVMFuzzerMutate);
104 }
105