Debian Codegraph @

fuzz/xml.c

raw
1 /*
2 * xml.c: a libFuzzer target to test several XML parser interfaces.
3 *
4 * See Copyright for the status of this software.
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include <libxml/catalog.h>
12 #include <libxml/parser.h>
13 #include <libxml/tree.h>
14 #include <libxml/xmlerror.h>
15 #include <libxml/xmlsave.h>
16 #include "fuzz.h"
17
18 int
19 LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED,
20 char ***argv ATTRIBUTE_UNUSED) {
21 xmlFuzzMemSetup();
22 xmlInitParser();
23 #ifdef LIBXML_CATALOG_ENABLED
24 xmlInitializeCatalog();
25 xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE);
26 #endif
27
28 return 0;
29 }
30
31 int
32 LLVMFuzzerTestOneInput(const char *data, size_t size) {
33 xmlParserCtxtPtr ctxt;
34 xmlDocPtr doc;
35 const char *docBuffer, *docUrl;
36 size_t failurePos, docSize, maxChunkSize;
37 int opts;
38 int errorCode;
39 #ifdef LIBXML_OUTPUT_ENABLED
40 xmlBufferPtr outbuf = NULL;
41 const char *saveEncoding;
42 int saveOpts;
43 #endif
44
45 xmlFuzzDataInit(data, size);
46 opts = (int) xmlFuzzReadInt(4);
47 /*
48 * Disable options that are known to cause timeouts
49 */
50 opts &= ~XML_PARSE_DTDVALID &
51 ~XML_PARSE_SAX1;
52 failurePos = xmlFuzzReadInt(4) % (size + 100);
53
54 maxChunkSize = xmlFuzzReadInt(4) % (size + size / 8 + 1);
55 if (maxChunkSize == 0)
56 maxChunkSize = 1;
57
58 #ifdef LIBXML_OUTPUT_ENABLED
59 /* TODO: Take from fuzz data */
60 saveOpts = 0;
61 saveEncoding = NULL;
62 #endif
63
64 xmlFuzzReadEntities();
65 docBuffer = xmlFuzzMainEntity(&docSize);
66 docUrl = xmlFuzzMainUrl();
67 if (docBuffer == NULL)
68 goto exit;
69
70 /* Pull parser */
71
72 xmlFuzzInjectFailure(failurePos);
73 ctxt = xmlNewParserCtxt();
74 if (ctxt == NULL) {
75 errorCode = XML_ERR_NO_MEMORY;
76 } else {
77 xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL);
78 xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL);
79 doc = xmlCtxtReadMemory(ctxt, docBuffer, docSize, docUrl, NULL, opts);
80 errorCode = ctxt->errNo;
81 xmlFuzzCheckFailureReport("xmlCtxtReadMemory",
82 doc == NULL && errorCode == XML_ERR_NO_MEMORY,
83 doc == NULL && errorCode == XML_IO_EIO);
84
85 if (doc != NULL) {
86 #ifdef LIBXML_OUTPUT_ENABLED
87 xmlSaveCtxtPtr save;
88
89 outbuf = xmlBufferCreate();
90
91 /* Also test the serializer. */
92 save = xmlSaveToBuffer(outbuf, saveEncoding, saveOpts);
93
94 if (save == NULL) {
95 xmlBufferFree(outbuf);
96 outbuf = NULL;
97 } else {
98 int saveErr;
99
100 xmlSaveDoc(save, doc);
101 saveErr = xmlSaveFinish(save);
102 xmlFuzzCheckFailureReport("xmlSaveToBuffer",
103 saveErr == XML_ERR_NO_MEMORY,
104 saveErr == XML_IO_EIO);
105 if (saveErr != XML_ERR_OK) {
106 xmlBufferFree(outbuf);
107 outbuf = NULL;
108 }
109 }
110 #endif
111 xmlFreeDoc(doc);
112 }
113
114 xmlFreeParserCtxt(ctxt);
115 }
116
117 /* Push parser */
118
119 #ifdef LIBXML_PUSH_ENABLED
120 xmlFuzzInjectFailure(failurePos);
121 /*
122 * FIXME: xmlCreatePushParserCtxt can still report OOM errors
123 * to stderr.
124 */
125 xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
126 ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, docUrl);
127 xmlSetGenericErrorFunc(NULL, NULL);
128
129 if (ctxt != NULL) {
130 size_t consumed;
131 int errorCodePush, numChunks, maxChunks;
132
133 xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL);
134 xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL);
135 xmlCtxtUseOptions(ctxt, opts);
136
137 consumed = 0;
138 numChunks = 0;
139 maxChunks = 50 + docSize / 100;
140 while (numChunks == 0 ||
141 (consumed < docSize && numChunks < maxChunks)) {
142 size_t chunkSize;
143 int terminate;
144
145 numChunks += 1;
146 chunkSize = docSize - consumed;
147
148 if (numChunks < maxChunks && chunkSize > maxChunkSize) {
149 chunkSize = maxChunkSize;
150 terminate = 0;
151 } else {
152 terminate = 1;
153 }
154
155 xmlParseChunk(ctxt, docBuffer + consumed, chunkSize, terminate);
156 consumed += chunkSize;
157 }
158
159 errorCodePush = ctxt->errNo;
160 xmlFuzzCheckFailureReport("xmlParseChunk",
161 errorCodePush == XML_ERR_NO_MEMORY,
162 errorCodePush == XML_IO_EIO);
163 doc = ctxt->myDoc;
164
165 /*
166 * Push and pull parser differ in when exactly they
167 * stop parsing, and the error code is the *last* error
168 * reported, so we can't check whether the codes match.
169 */
170 if (errorCode != XML_ERR_NO_MEMORY &&
171 errorCode != XML_IO_EIO &&
172 errorCodePush != XML_ERR_NO_MEMORY &&
173 errorCodePush != XML_IO_EIO &&
174 (errorCode == XML_ERR_OK) != (errorCodePush == XML_ERR_OK)) {
175 fprintf(stderr, "pull/push parser error mismatch: %d != %d\n",
176 errorCode, errorCodePush);
177 #if 0
178 FILE *f = fopen("c.xml", "wb");
179 fwrite(docBuffer, docSize, 1, f);
180 fclose(f);
181 #endif
182 abort();
183 }
184
185 #ifdef LIBXML_OUTPUT_ENABLED
186 /*
187 * Verify that pull and push parser produce the same result.
188 *
189 * The NOBLANKS option doesn't work reliably in push mode.
190 */
191 if ((opts & XML_PARSE_NOBLANKS) == 0 &&
192 errorCode == XML_ERR_OK &&
193 errorCodePush == XML_ERR_OK &&
194 outbuf != NULL) {
195 xmlBufferPtr outbufPush;
196 xmlSaveCtxtPtr save;
197
198 outbufPush = xmlBufferCreate();
199
200 save = xmlSaveToBuffer(outbufPush, saveEncoding, saveOpts);
201
202 if (save != NULL) {
203 int saveErr;
204
205 xmlSaveDoc(save, doc);
206 saveErr = xmlSaveFinish(save);
207
208 if (saveErr == XML_ERR_OK) {
209 int outbufSize = xmlBufferLength(outbuf);
210
211 if (outbufSize != xmlBufferLength(outbufPush) ||
212 memcmp(xmlBufferContent(outbuf),
213 xmlBufferContent(outbufPush),
214 outbufSize) != 0) {
215 fprintf(stderr, "pull/push parser roundtrip "
216 "mismatch\n");
217 #if 0
218 FILE *f = fopen("c.xml", "wb");
219 fwrite(docBuffer, docSize, 1, f);
220 fclose(f);
221 fprintf(stderr, "opts: %X\n", opts);
222 fprintf(stderr, "---\n%s\n---\n%s\n---\n",
223 xmlBufferContent(outbuf),
224 xmlBufferContent(outbufPush));
225 #endif
226 abort();
227 }
228 }
229 }
230
231 xmlBufferFree(outbufPush);
232 }
233 #endif
234
235 xmlFreeDoc(doc);
236 xmlFreeParserCtxt(ctxt);
237 }
238 #endif
239
240 exit:
241 #ifdef LIBXML_OUTPUT_ENABLED
242 xmlBufferFree(outbuf);
243 #endif
244 xmlFuzzInjectFailure(0);
245 xmlFuzzDataCleanup();
246 xmlResetLastError();
247 return(0);
248 }
249
250 size_t
251 LLVMFuzzerCustomMutator(char *data, size_t size, size_t maxSize,
252 unsigned seed) {
253 static const xmlFuzzChunkDesc chunks[] = {
254 { 4, XML_FUZZ_PROB_ONE / 10 }, /* opts */
255 { 4, XML_FUZZ_PROB_ONE / 10 }, /* failurePos */
256 { 4, XML_FUZZ_PROB_ONE / 10 }, /* maxChunkSize */
257 { 0, 0 }
258 };
259
260 return xmlFuzzMutateChunks(chunks, data, size, maxSize, seed,
261 LLVMFuzzerMutate);
262 }
263