Coverage Report

Created: 2025-08-26 06:42

/src/libxml2/fuzz/xml.c
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 <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
2
                     char ***argv ATTRIBUTE_UNUSED) {
21
2
    xmlFuzzMemSetup();
22
2
    xmlInitParser();
23
2
#ifdef LIBXML_CATALOG_ENABLED
24
2
    xmlInitializeCatalog();
25
2
    xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE);
26
2
#endif
27
28
2
    return 0;
29
2
}
30
31
int
32
24.9k
LLVMFuzzerTestOneInput(const char *data, size_t size) {
33
24.9k
    xmlParserCtxtPtr ctxt;
34
24.9k
    xmlDocPtr doc;
35
24.9k
    const char *docBuffer, *docUrl;
36
24.9k
    size_t failurePos, docSize, maxChunkSize;
37
24.9k
    int opts;
38
24.9k
    int errorCode;
39
24.9k
#ifdef LIBXML_OUTPUT_ENABLED
40
24.9k
    xmlBufferPtr outbuf = NULL;
41
24.9k
    const char *saveEncoding;
42
24.9k
    int saveOpts;
43
24.9k
#endif
44
45
24.9k
    xmlFuzzDataInit(data, size);
46
24.9k
    opts = (int) xmlFuzzReadInt(4);
47
    /*
48
     * Disable options that are known to cause timeouts
49
     */
50
24.9k
    opts &= ~XML_PARSE_DTDVALID &
51
24.9k
            ~XML_PARSE_SAX1;
52
24.9k
    failurePos = xmlFuzzReadInt(4) % (size + 100);
53
54
24.9k
    maxChunkSize = xmlFuzzReadInt(4) % (size + size / 8 + 1);
55
24.9k
    if (maxChunkSize == 0)
56
1.99k
        maxChunkSize = 1;
57
58
24.9k
#ifdef LIBXML_OUTPUT_ENABLED
59
    /* TODO: Take from fuzz data */
60
24.9k
    saveOpts = 0;
61
24.9k
    saveEncoding = NULL;
62
24.9k
#endif
63
64
24.9k
    xmlFuzzReadEntities();
65
24.9k
    docBuffer = xmlFuzzMainEntity(&docSize);
66
24.9k
    docUrl = xmlFuzzMainUrl();
67
24.9k
    if (docBuffer == NULL)
68
131
        goto exit;
69
70
    /* Pull parser */
71
72
24.8k
    xmlFuzzInjectFailure(failurePos);
73
24.8k
    ctxt = xmlNewParserCtxt();
74
24.8k
    if (ctxt == NULL) {
75
30
        errorCode = XML_ERR_NO_MEMORY;
76
24.8k
    } else {
77
24.8k
        xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL);
78
24.8k
        xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL);
79
24.8k
        doc = xmlCtxtReadMemory(ctxt, docBuffer, docSize, docUrl, NULL, opts);
80
24.8k
        errorCode = ctxt->errNo;
81
24.8k
        xmlFuzzCheckFailureReport("xmlCtxtReadMemory",
82
24.8k
                doc == NULL && errorCode == XML_ERR_NO_MEMORY,
83
24.8k
                doc == NULL && errorCode == XML_IO_EIO);
84
85
24.8k
        if (doc != NULL) {
86
13.4k
#ifdef LIBXML_OUTPUT_ENABLED
87
13.4k
            xmlSaveCtxtPtr save;
88
89
13.4k
            outbuf = xmlBufferCreate();
90
91
            /* Also test the serializer. */
92
13.4k
            save = xmlSaveToBuffer(outbuf, saveEncoding, saveOpts);
93
94
13.4k
            if (save == NULL) {
95
433
                xmlBufferFree(outbuf);
96
433
                outbuf = NULL;
97
13.0k
            } else {
98
13.0k
                int saveErr;
99
100
13.0k
                xmlSaveDoc(save, doc);
101
13.0k
                saveErr = xmlSaveFinish(save);
102
13.0k
                xmlFuzzCheckFailureReport("xmlSaveToBuffer",
103
13.0k
                                          saveErr == XML_ERR_NO_MEMORY,
104
13.0k
                                          saveErr == XML_IO_EIO);
105
13.0k
                if (saveErr != XML_ERR_OK) {
106
65
                    xmlBufferFree(outbuf);
107
65
                    outbuf = NULL;
108
65
                }
109
13.0k
            }
110
13.4k
#endif
111
13.4k
            xmlFreeDoc(doc);
112
13.4k
        }
113
114
24.8k
        xmlFreeParserCtxt(ctxt);
115
24.8k
    }
116
117
    /* Push parser */
118
119
24.8k
#ifdef LIBXML_PUSH_ENABLED
120
24.8k
    xmlFuzzInjectFailure(failurePos);
121
    /*
122
     * FIXME: xmlCreatePushParserCtxt can still report OOM errors
123
     * to stderr.
124
     */
125
24.8k
    xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
126
24.8k
    ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, docUrl);
127
24.8k
    xmlSetGenericErrorFunc(NULL, NULL);
128
129
24.8k
    if (ctxt != NULL) {
130
24.7k
        size_t consumed;
131
24.7k
        int errorCodePush, numChunks, maxChunks;
132
133
24.7k
        xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL);
134
24.7k
        xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL);
135
24.7k
        xmlCtxtUseOptions(ctxt, opts);
136
137
24.7k
        consumed = 0;
138
24.7k
        numChunks = 0;
139
24.7k
        maxChunks = 50 + docSize / 100;
140
752k
        while (numChunks == 0 ||
141
752k
               (consumed < docSize && numChunks < maxChunks)) {
142
728k
            size_t chunkSize;
143
728k
            int terminate;
144
145
728k
            numChunks += 1;
146
728k
            chunkSize = docSize - consumed;
147
148
728k
            if (numChunks < maxChunks && chunkSize > maxChunkSize) {
149
703k
                chunkSize = maxChunkSize;
150
703k
                terminate = 0;
151
703k
            } else {
152
24.7k
                terminate = 1;
153
24.7k
            }
154
155
728k
            xmlParseChunk(ctxt, docBuffer + consumed, chunkSize, terminate);
156
728k
            consumed += chunkSize;
157
728k
        }
158
159
24.7k
        errorCodePush = ctxt->errNo;
160
24.7k
        xmlFuzzCheckFailureReport("xmlParseChunk",
161
24.7k
                                  errorCodePush == XML_ERR_NO_MEMORY,
162
24.7k
                                  errorCodePush == XML_IO_EIO);
163
24.7k
        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
24.7k
        if (errorCode != XML_ERR_NO_MEMORY &&
171
24.7k
            errorCode != XML_IO_EIO &&
172
24.7k
            errorCodePush != XML_ERR_NO_MEMORY &&
173
24.7k
            errorCodePush != XML_IO_EIO &&
174
24.7k
            (errorCode == XML_ERR_OK) != (errorCodePush == XML_ERR_OK)) {
175
0
            fprintf(stderr, "pull/push parser error mismatch: %d != %d\n",
176
0
                    errorCode, errorCodePush);
177
#if 0
178
            FILE *f = fopen("c.xml", "wb");
179
            fwrite(docBuffer, docSize, 1, f);
180
            fclose(f);
181
#endif
182
0
            abort();
183
0
        }
184
185
24.7k
#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
24.7k
        if ((opts & XML_PARSE_NOBLANKS) == 0 &&
192
24.7k
            errorCode == XML_ERR_OK &&
193
24.7k
            errorCodePush == XML_ERR_OK &&
194
24.7k
            outbuf != NULL) {
195
120
            xmlBufferPtr outbufPush;
196
120
            xmlSaveCtxtPtr save;
197
198
120
            outbufPush = xmlBufferCreate();
199
200
120
            save = xmlSaveToBuffer(outbufPush, saveEncoding, saveOpts);
201
202
120
            if (save != NULL) {
203
120
                int saveErr;
204
205
120
                xmlSaveDoc(save, doc);
206
120
                saveErr = xmlSaveFinish(save);
207
208
120
                if (saveErr == XML_ERR_OK) {
209
120
                    int outbufSize = xmlBufferLength(outbuf);
210
211
120
                    if (outbufSize != xmlBufferLength(outbufPush) ||
212
120
                        memcmp(xmlBufferContent(outbuf),
213
120
                               xmlBufferContent(outbufPush),
214
120
                               outbufSize) != 0) {
215
0
                        fprintf(stderr, "pull/push parser roundtrip "
216
0
                                "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
0
                        abort();
227
0
                    }
228
120
                }
229
120
            }
230
231
120
            xmlBufferFree(outbufPush);
232
120
        }
233
24.7k
#endif
234
235
24.7k
        xmlFreeDoc(doc);
236
24.7k
        xmlFreeParserCtxt(ctxt);
237
24.7k
    }
238
24.8k
#endif
239
240
24.9k
exit:
241
24.9k
#ifdef LIBXML_OUTPUT_ENABLED
242
24.9k
    xmlBufferFree(outbuf);
243
24.9k
#endif
244
24.9k
    xmlFuzzInjectFailure(0);
245
24.9k
    xmlFuzzDataCleanup();
246
24.9k
    xmlResetLastError();
247
24.9k
    return(0);
248
24.8k
}
249
250
size_t
251
LLVMFuzzerCustomMutator(char *data, size_t size, size_t maxSize,
252
0
                        unsigned seed) {
253
0
    static const xmlFuzzChunkDesc chunks[] = {
254
0
        { 4, XML_FUZZ_PROB_ONE / 10 }, /* opts */
255
0
        { 4, XML_FUZZ_PROB_ONE / 10 }, /* failurePos */
256
0
        { 4, XML_FUZZ_PROB_ONE / 10 }, /* maxChunkSize */
257
0
        { 0, 0 }
258
0
    };
259
260
0
    return xmlFuzzMutateChunks(chunks, data, size, maxSize, seed,
261
0
                               LLVMFuzzerMutate);
262
0
}
263