Coverage Report

Created: 2026-07-30 06:08

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