VirtualBox

source: vbox/trunk/src/libs/libxml2-2.12.6/fuzz/xml.c@ 105635

最後變更 在這個檔案從105635是 104106,由 vboxsync 提交於 10 月 前

libxml2-2.9.14: Applied and adjusted our libxml2 changes to 2.9.14. bugref:10640

  • 屬性 svn:eol-style 設為 native
檔案大小: 3.1 KB
 
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
14int
15LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED,
16 char ***argv ATTRIBUTE_UNUSED) {
17 xmlFuzzMemSetup();
18 xmlInitParser();
19#ifdef LIBXML_CATALOG_ENABLED
20 xmlInitializeCatalog();
21#endif
22 xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
23 xmlSetExternalEntityLoader(xmlFuzzEntityLoader);
24
25 return 0;
26}
27
28int
29LLVMFuzzerTestOneInput(const char *data, size_t size) {
30 xmlDocPtr doc;
31 const char *docBuffer, *docUrl;
32 size_t maxAlloc, docSize;
33 int opts;
34
35 xmlFuzzDataInit(data, size);
36 opts = (int) xmlFuzzReadInt(4);
37 /*
38 * Disable options that are known to cause timeouts
39 */
40 opts &= ~XML_PARSE_XINCLUDE &
41 ~XML_PARSE_DTDVALID &
42 ~XML_PARSE_SAX1;
43 maxAlloc = xmlFuzzReadInt(4) % (size + 1);
44
45 xmlFuzzReadEntities();
46 docBuffer = xmlFuzzMainEntity(&docSize);
47 docUrl = xmlFuzzMainUrl();
48 if (docBuffer == NULL)
49 goto exit;
50
51 /* Pull parser */
52
53 xmlFuzzMemSetLimit(maxAlloc);
54 doc = xmlReadMemory(docBuffer, docSize, docUrl, NULL, opts);
55
56#ifdef LIBXML_OUTPUT_ENABLED
57 {
58 xmlChar *out;
59 int outSize;
60
61 /* Also test the serializer. */
62 xmlDocDumpMemory(doc, &out, &outSize);
63 xmlFree(out);
64 }
65#endif
66
67 xmlFreeDoc(doc);
68
69 /* Push parser */
70
71#ifdef LIBXML_PUSH_ENABLED
72 {
73 static const size_t maxChunkSize = 128;
74 xmlParserCtxtPtr ctxt;
75 size_t consumed, chunkSize;
76
77 xmlFuzzMemSetLimit(maxAlloc);
78 ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, docUrl);
79 if (ctxt == NULL)
80 goto exit;
81 xmlCtxtUseOptions(ctxt, opts);
82
83 for (consumed = 0; consumed < docSize; consumed += chunkSize) {
84 chunkSize = docSize - consumed;
85 if (chunkSize > maxChunkSize)
86 chunkSize = maxChunkSize;
87 xmlParseChunk(ctxt, docBuffer + consumed, chunkSize, 0);
88 }
89
90 xmlParseChunk(ctxt, NULL, 0, 1);
91 xmlFreeDoc(ctxt->myDoc);
92 xmlFreeParserCtxt(ctxt);
93 }
94#endif
95
96 /* Reader */
97
98#ifdef LIBXML_READER_ENABLED
99 {
100 xmlTextReaderPtr reader;
101 int j;
102
103 xmlFuzzMemSetLimit(maxAlloc);
104 reader = xmlReaderForMemory(docBuffer, docSize, NULL, NULL, opts);
105 if (reader == NULL)
106 goto exit;
107 while (xmlTextReaderRead(reader) == 1) {
108 if (xmlTextReaderNodeType(reader) == XML_ELEMENT_NODE) {
109 int i, n = xmlTextReaderAttributeCount(reader);
110 for (i=0; i<n; i++) {
111 xmlTextReaderMoveToAttributeNo(reader, i);
112 while (xmlTextReaderReadAttributeValue(reader) == 1);
113 }
114 }
115 }
116 for (j = 0; j < 10; j++)
117 xmlTextReaderRead(reader);
118 xmlFreeTextReader(reader);
119 }
120#endif
121
122exit:
123 xmlFuzzMemSetLimit(0);
124 xmlFuzzDataCleanup();
125 xmlResetLastError();
126 return(0);
127}
128
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette