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/xmlreader.h> |
12 | | #include "fuzz.h" |
13 | | |
14 | | int |
15 | | LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED, |
16 | 7.69k | char ***argv ATTRIBUTE_UNUSED) { |
17 | 7.69k | xmlInitParser(); |
18 | 7.69k | #ifdef LIBXML_CATALOG_ENABLED |
19 | 7.69k | xmlInitializeCatalog(); |
20 | 7.69k | #endif |
21 | 7.69k | xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc); |
22 | 7.69k | xmlSetExternalEntityLoader(xmlFuzzEntityLoader); |
23 | | |
24 | 7.69k | return 0; |
25 | 7.69k | } |
26 | | |
27 | | int |
28 | 395k | LLVMFuzzerTestOneInput(const char *data, size_t size) { |
29 | 395k | static const size_t maxChunkSize = 128; |
30 | 395k | xmlDocPtr doc; |
31 | 395k | xmlParserCtxtPtr ctxt; |
32 | 395k | xmlTextReaderPtr reader; |
33 | 395k | xmlChar *out; |
34 | 395k | const char *docBuffer, *docUrl; |
35 | 395k | size_t docSize, consumed, chunkSize; |
36 | 395k | int opts, outSize; |
37 | | |
38 | 395k | xmlFuzzDataInit(data, size); |
39 | 395k | opts = xmlFuzzReadInt(); |
40 | 395k | opts &= ~XML_PARSE_XINCLUDE; |
41 | | |
42 | 395k | xmlFuzzReadEntities(); |
43 | 395k | docBuffer = xmlFuzzMainEntity(&docSize); |
44 | 395k | docUrl = xmlFuzzMainUrl(); |
45 | 395k | if (docBuffer == NULL) |
46 | 194k | goto exit; |
47 | | |
48 | | /* Pull parser */ |
49 | | |
50 | 200k | doc = xmlReadMemory(docBuffer, docSize, docUrl, NULL, opts); |
51 | | /* Also test the serializer. */ |
52 | 200k | xmlDocDumpMemory(doc, &out, &outSize); |
53 | 200k | xmlFree(out); |
54 | 200k | xmlFreeDoc(doc); |
55 | | |
56 | | /* Push parser */ |
57 | | |
58 | 200k | ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, docUrl); |
59 | 200k | if (ctxt == NULL) |
60 | 0 | goto exit; |
61 | 200k | xmlCtxtUseOptions(ctxt, opts); |
62 | | |
63 | 5.80M | for (consumed = 0; consumed < docSize; consumed += chunkSize) { |
64 | 5.59M | chunkSize = docSize - consumed; |
65 | 5.59M | if (chunkSize > maxChunkSize) |
66 | 5.39M | chunkSize = maxChunkSize; |
67 | 5.59M | xmlParseChunk(ctxt, docBuffer + consumed, chunkSize, 0); |
68 | 5.59M | } |
69 | | |
70 | 200k | xmlParseChunk(ctxt, NULL, 0, 1); |
71 | 200k | xmlFreeDoc(ctxt->myDoc); |
72 | 200k | xmlFreeParserCtxt(ctxt); |
73 | | |
74 | | /* Reader */ |
75 | | |
76 | 200k | reader = xmlReaderForMemory(docBuffer, docSize, NULL, NULL, opts); |
77 | 200k | if (reader == NULL) |
78 | 0 | goto exit; |
79 | 5.34M | while (xmlTextReaderRead(reader) == 1) { |
80 | 5.14M | if (xmlTextReaderNodeType(reader) == XML_ELEMENT_NODE) { |
81 | 1.81M | int i, n = xmlTextReaderAttributeCount(reader); |
82 | 3.86M | for (i=0; i<n; i++) { |
83 | 2.05M | xmlTextReaderMoveToAttributeNo(reader, i); |
84 | 4.12M | while (xmlTextReaderReadAttributeValue(reader) == 1); |
85 | 2.05M | } |
86 | 1.81M | } |
87 | 5.14M | } |
88 | 200k | xmlFreeTextReader(reader); |
89 | | |
90 | 395k | exit: |
91 | 395k | xmlFuzzDataCleanup(); |
92 | 395k | xmlResetLastError(); |
93 | 395k | return(0); |
94 | 200k | } |
95 | | |