Line | Count | Source (jump to first uncovered line) |
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 <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 <libxml/xmlreader.h> |
13 | | #include "fuzz.h" |
14 | | |
15 | | int |
16 | | LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED, |
17 | 8.72k | char ***argv ATTRIBUTE_UNUSED) { |
18 | 8.72k | xmlInitParser(); |
19 | 8.72k | #ifdef LIBXML_CATALOG_ENABLED |
20 | 8.72k | xmlInitializeCatalog(); |
21 | 8.72k | #endif |
22 | 8.72k | xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc); |
23 | 8.72k | xmlSetExternalEntityLoader(xmlFuzzEntityLoader); |
24 | | |
25 | 8.72k | return 0; |
26 | 8.72k | } |
27 | | |
28 | | int |
29 | 370k | LLVMFuzzerTestOneInput(const char *data, size_t size) { |
30 | 370k | static const size_t maxChunkSize = 128; |
31 | 370k | xmlDocPtr doc; |
32 | 370k | xmlParserCtxtPtr ctxt; |
33 | 370k | xmlTextReaderPtr reader; |
34 | 370k | xmlChar *out; |
35 | 370k | const char *docBuffer, *docUrl; |
36 | 370k | size_t maxSize, docSize, consumed, chunkSize; |
37 | 370k | int opts, outSize; |
38 | | |
39 | 370k | xmlFuzzDataInit(data, size); |
40 | 370k | opts = xmlFuzzReadInt(); |
41 | | |
42 | | /* Lower maximum size when processing entities for now. */ |
43 | 370k | maxSize = opts & XML_PARSE_NOENT ? 50000 : 500000; |
44 | 370k | if (size > maxSize) |
45 | 3.00k | goto exit; |
46 | | |
47 | 367k | xmlFuzzReadEntities(); |
48 | 367k | docBuffer = xmlFuzzMainEntity(&docSize); |
49 | 367k | docUrl = xmlFuzzMainUrl(); |
50 | 367k | if (docBuffer == NULL) |
51 | 2.14k | goto exit; |
52 | | |
53 | | /* Pull parser */ |
54 | | |
55 | 365k | doc = xmlReadMemory(docBuffer, docSize, docUrl, NULL, opts); |
56 | 365k | if (opts & XML_PARSE_XINCLUDE) |
57 | 166k | xmlXIncludeProcessFlags(doc, opts); |
58 | | /* Also test the serializer. */ |
59 | 365k | xmlDocDumpMemory(doc, &out, &outSize); |
60 | 365k | xmlFree(out); |
61 | 365k | xmlFreeDoc(doc); |
62 | | |
63 | | /* Push parser */ |
64 | | |
65 | 365k | ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, docUrl); |
66 | 365k | if (ctxt == NULL) |
67 | 0 | goto exit; |
68 | 365k | xmlCtxtUseOptions(ctxt, opts); |
69 | | |
70 | 15.5M | for (consumed = 0; consumed < docSize; consumed += chunkSize) { |
71 | 15.1M | chunkSize = docSize - consumed; |
72 | 15.1M | if (chunkSize > maxChunkSize) |
73 | 14.7M | chunkSize = maxChunkSize; |
74 | 15.1M | xmlParseChunk(ctxt, docBuffer + consumed, chunkSize, 0); |
75 | 15.1M | } |
76 | | |
77 | 365k | xmlParseChunk(ctxt, NULL, 0, 1); |
78 | 365k | if (opts & XML_PARSE_XINCLUDE) |
79 | 161k | xmlXIncludeProcessFlags(ctxt->myDoc, opts); |
80 | 365k | xmlFreeDoc(ctxt->myDoc); |
81 | 365k | xmlFreeParserCtxt(ctxt); |
82 | | |
83 | | /* Reader */ |
84 | | |
85 | 365k | reader = xmlReaderForMemory(docBuffer, docSize, NULL, NULL, opts); |
86 | 365k | if (reader == NULL) |
87 | 0 | goto exit; |
88 | 1.70M | while (xmlTextReaderRead(reader) == 1) { |
89 | 1.33M | if (xmlTextReaderNodeType(reader) == XML_ELEMENT_NODE) { |
90 | 496k | int i, n = xmlTextReaderAttributeCount(reader); |
91 | 933k | for (i=0; i<n; i++) { |
92 | 436k | xmlTextReaderMoveToAttributeNo(reader, i); |
93 | 875k | while (xmlTextReaderReadAttributeValue(reader) == 1); |
94 | 436k | } |
95 | 496k | } |
96 | 1.33M | } |
97 | 365k | xmlFreeTextReader(reader); |
98 | | |
99 | 365k | exit: |
100 | 365k | xmlFuzzDataCleanup(); |
101 | 365k | xmlResetLastError(); |
102 | 365k | return(0); |
103 | 365k | } |
104 | | |