Coverage Report

Created: 2025-08-11 06:23

/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.5k
LLVMFuzzerTestOneInput(const char *data, size_t size) {
33
24.5k
    xmlParserCtxtPtr ctxt;
34
24.5k
    xmlDocPtr doc;
35
24.5k
    const char *docBuffer, *docUrl;
36
24.5k
    size_t failurePos, docSize, maxChunkSize;
37
24.5k
    int opts;
38
24.5k
    int errorCode;
39
24.5k
#ifdef LIBXML_OUTPUT_ENABLED
40
24.5k
    xmlBufferPtr outbuf = NULL;
41
24.5k
    const char *saveEncoding;
42
24.5k
    int saveOpts;
43
24.5k
#endif
44
45
24.5k
    xmlFuzzDataInit(data, size);
46
24.5k
    opts = (int) xmlFuzzReadInt(4);
47
    /*
48
     * Disable options that are known to cause timeouts
49
     */
50
24.5k
    opts &= ~XML_PARSE_DTDVALID &
51
24.5k
            ~XML_PARSE_SAX1;
52
24.5k
    failurePos = xmlFuzzReadInt(4) % (size + 100);
53
54
24.5k
    maxChunkSize = xmlFuzzReadInt(4) % (size + size / 8 + 1);
55
24.5k
    if (maxChunkSize == 0)
56
1.92k
        maxChunkSize = 1;
57
58
24.5k
#ifdef LIBXML_OUTPUT_ENABLED
59
    /* TODO: Take from fuzz data */
60
24.5k
    saveOpts = 0;
61
24.5k
    saveEncoding = NULL;
62
24.5k
#endif
63
64
24.5k
    xmlFuzzReadEntities();
65
24.5k
    docBuffer = xmlFuzzMainEntity(&docSize);
66
24.5k
    docUrl = xmlFuzzMainUrl();
67
24.5k
    if (docBuffer == NULL)
68
131
        goto exit;
69
70
    /* Pull parser */
71
72
24.4k
    xmlFuzzInjectFailure(failurePos);
73
24.4k
    ctxt = xmlNewParserCtxt();
74
24.4k
    if (ctxt == NULL) {
75
27
        errorCode = XML_ERR_NO_MEMORY;
76
24.3k
    } else {
77
24.3k
        xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL);
78
24.3k
        xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL);
79
24.3k
        doc = xmlCtxtReadMemory(ctxt, docBuffer, docSize, docUrl, NULL, opts);
80
24.3k
        errorCode = ctxt->errNo;
81
24.3k
        xmlFuzzCheckFailureReport("xmlCtxtReadMemory",
82
24.3k
                doc == NULL && errorCode == XML_ERR_NO_MEMORY,
83
24.3k
                doc == NULL && errorCode == XML_IO_EIO);
84
85
24.3k
        if (doc != NULL) {
86
13.2k
#ifdef LIBXML_OUTPUT_ENABLED
87
13.2k
            xmlSaveCtxtPtr save;
88
89
13.2k
            outbuf = xmlBufferCreate();
90
91
            /* Also test the serializer. */
92
13.2k
            save = xmlSaveToBuffer(outbuf, saveEncoding, saveOpts);
93
94
13.2k
            if (save == NULL) {
95
404
                xmlBufferFree(outbuf);
96
404
                outbuf = NULL;
97
12.8k
            } else {
98
12.8k
                int saveErr;
99
100
12.8k
                xmlSaveDoc(save, doc);
101
12.8k
                saveErr = xmlSaveFinish(save);
102
12.8k
                xmlFuzzCheckFailureReport("xmlSaveToBuffer",
103
12.8k
                                          saveErr == XML_ERR_NO_MEMORY,
104
12.8k
                                          saveErr == XML_IO_EIO);
105
12.8k
                if (saveErr != XML_ERR_OK) {
106
60
                    xmlBufferFree(outbuf);
107
60
                    outbuf = NULL;
108
60
                }
109
12.8k
            }
110
13.2k
#endif
111
13.2k
            xmlFreeDoc(doc);
112
13.2k
        }
113
114
24.3k
        xmlFreeParserCtxt(ctxt);
115
24.3k
    }
116
117
    /* Push parser */
118
119
24.4k
#ifdef LIBXML_PUSH_ENABLED
120
24.4k
    xmlFuzzInjectFailure(failurePos);
121
    /*
122
     * FIXME: xmlCreatePushParserCtxt can still report OOM errors
123
     * to stderr.
124
     */
125
24.4k
    xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
126
24.4k
    ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, docUrl);
127
24.4k
    xmlSetGenericErrorFunc(NULL, NULL);
128
129
24.4k
    if (ctxt != NULL) {
130
24.3k
        size_t consumed;
131
24.3k
        int errorCodePush, numChunks, maxChunks;
132
133
24.3k
        xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL);
134
24.3k
        xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL);
135
24.3k
        xmlCtxtUseOptions(ctxt, opts);
136
137
24.3k
        consumed = 0;
138
24.3k
        numChunks = 0;
139
24.3k
        maxChunks = 50 + docSize / 100;
140
712k
        while (numChunks == 0 ||
141
712k
               (consumed < docSize && numChunks < maxChunks)) {
142
687k
            size_t chunkSize;
143
687k
            int terminate;
144
145
687k
            numChunks += 1;
146
687k
            chunkSize = docSize - consumed;
147
148
687k
            if (numChunks < maxChunks && chunkSize > maxChunkSize) {
149
663k
                chunkSize = maxChunkSize;
150
663k
                terminate = 0;
151
663k
            } else {
152
24.3k
                terminate = 1;
153
24.3k
            }
154
155
687k
            xmlParseChunk(ctxt, docBuffer + consumed, chunkSize, terminate);
156
687k
            consumed += chunkSize;
157
687k
        }
158
159
24.3k
        errorCodePush = ctxt->errNo;
160
24.3k
        xmlFuzzCheckFailureReport("xmlParseChunk",
161
24.3k
                                  errorCodePush == XML_ERR_NO_MEMORY,
162
24.3k
                                  errorCodePush == XML_IO_EIO);
163
24.3k
        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.3k
        if (errorCode != XML_ERR_NO_MEMORY &&
171
24.3k
            errorCode != XML_IO_EIO &&
172
24.3k
            errorCodePush != XML_ERR_NO_MEMORY &&
173
24.3k
            errorCodePush != XML_IO_EIO &&
174
24.3k
            (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.3k
#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.3k
        if ((opts & XML_PARSE_NOBLANKS) == 0 &&
192
24.3k
            errorCode == XML_ERR_OK &&
193
24.3k
            errorCodePush == XML_ERR_OK &&
194
24.3k
            outbuf != NULL) {
195
112
            xmlBufferPtr outbufPush;
196
112
            xmlSaveCtxtPtr save;
197
198
112
            outbufPush = xmlBufferCreate();
199
200
112
            save = xmlSaveToBuffer(outbufPush, saveEncoding, saveOpts);
201
202
112
            if (save != NULL) {
203
112
                int saveErr;
204
205
112
                xmlSaveDoc(save, doc);
206
112
                saveErr = xmlSaveFinish(save);
207
208
112
                if (saveErr == XML_ERR_OK) {
209
112
                    int outbufSize = xmlBufferLength(outbuf);
210
211
112
                    if (outbufSize != xmlBufferLength(outbufPush) ||
212
112
                        memcmp(xmlBufferContent(outbuf),
213
112
                               xmlBufferContent(outbufPush),
214
112
                               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
112
                }
229
112
            }
230
231
112
            xmlBufferFree(outbufPush);
232
112
        }
233
24.3k
#endif
234
235
24.3k
        xmlFreeDoc(doc);
236
24.3k
        xmlFreeParserCtxt(ctxt);
237
24.3k
    }
238
24.4k
#endif
239
240
24.5k
exit:
241
24.5k
#ifdef LIBXML_OUTPUT_ENABLED
242
24.5k
    xmlBufferFree(outbuf);
243
24.5k
#endif
244
24.5k
    xmlFuzzInjectFailure(0);
245
24.5k
    xmlFuzzDataCleanup();
246
24.5k
    xmlResetLastError();
247
24.5k
    return(0);
248
24.4k
}
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