Coverage Report

Created: 2026-05-11 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/fuzz/xinclude.c
Line
Count
Source
1
/*
2
 * xinclude.c: a libFuzzer target to test the XInclude engine.
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/xinclude.h>
12
#include "fuzz.h"
13
14
int
15
LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED,
16
2
                     char ***argv ATTRIBUTE_UNUSED) {
17
2
    xmlFuzzMemSetup();
18
2
    xmlInitParser();
19
2
#ifdef LIBXML_CATALOG_ENABLED
20
2
    xmlInitializeCatalog();
21
2
    xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE);
22
2
#endif
23
2
    xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
24
25
2
    return 0;
26
2
}
27
28
int
29
29.0k
LLVMFuzzerTestOneInput(const char *data, size_t size) {
30
29.0k
    xmlParserCtxtPtr ctxt;
31
29.0k
    xmlDocPtr doc;
32
29.0k
    const char *docBuffer, *docUrl;
33
29.0k
    size_t failurePos, docSize;
34
29.0k
    int opts;
35
36
29.0k
    xmlFuzzDataInit(data, size);
37
29.0k
    opts = (int) xmlFuzzReadInt(4);
38
29.0k
    opts &= ~XML_PARSE_DTDVALID &
39
29.0k
            ~XML_PARSE_SAX1;
40
29.0k
    failurePos = xmlFuzzReadInt(4) % (size + 100);
41
42
29.0k
    xmlFuzzReadEntities();
43
29.0k
    docBuffer = xmlFuzzMainEntity(&docSize);
44
29.0k
    docUrl = xmlFuzzMainUrl();
45
29.0k
    if (docBuffer == NULL)
46
68
        goto exit;
47
48
    /* Pull parser */
49
50
29.0k
    xmlFuzzInjectFailure(failurePos);
51
29.0k
    ctxt = xmlNewParserCtxt();
52
29.0k
    if (ctxt != NULL) {
53
28.9k
        xmlXIncludeCtxtPtr xinc;
54
28.9k
        xmlDocPtr copy;
55
56
28.9k
        xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL);
57
58
28.9k
        doc = xmlCtxtReadMemory(ctxt, docBuffer, docSize, docUrl, NULL, opts);
59
28.9k
        xmlFuzzCheckFailureReport("xmlCtxtReadMemory",
60
28.9k
                doc == NULL && ctxt->errNo == XML_ERR_NO_MEMORY,
61
28.9k
                doc == NULL && ctxt->errNo == XML_IO_EIO);
62
63
28.9k
        xinc = xmlXIncludeNewContext(doc);
64
28.9k
        xmlXIncludeSetResourceLoader(xinc, xmlFuzzResourceLoader, NULL);
65
28.9k
        xmlXIncludeSetFlags(xinc, opts);
66
28.9k
        xmlXIncludeProcessNode(xinc, (xmlNodePtr) doc);
67
28.9k
        if (doc != NULL) {
68
20.0k
            xmlFuzzCheckFailureReport("xmlXIncludeProcessNode",
69
20.0k
                    xinc == NULL ||
70
20.0k
                    xmlXIncludeGetLastError(xinc) == XML_ERR_NO_MEMORY,
71
20.0k
                    xinc != NULL &&
72
20.0k
                    xmlXIncludeGetLastError(xinc) == XML_IO_EIO);
73
20.0k
        }
74
28.9k
        xmlXIncludeFreeContext(xinc);
75
76
28.9k
        xmlFuzzResetFailure();
77
28.9k
        copy = xmlCopyDoc(doc, 1);
78
28.9k
        if (doc != NULL)
79
20.0k
            xmlFuzzCheckFailureReport("xmlCopyNode", copy == NULL, 0);
80
28.9k
        xmlFreeDoc(copy);
81
82
28.9k
        xmlFreeDoc(doc);
83
28.9k
        xmlFreeParserCtxt(ctxt);
84
28.9k
    }
85
86
29.0k
exit:
87
29.0k
    xmlFuzzInjectFailure(0);
88
29.0k
    xmlFuzzDataCleanup();
89
29.0k
    xmlResetLastError();
90
29.0k
    return(0);
91
29.0k
}
92
93
size_t
94
LLVMFuzzerCustomMutator(char *data, size_t size, size_t maxSize,
95
0
                        unsigned seed) {
96
0
    static const xmlFuzzChunkDesc chunks[] = {
97
0
        { 4, XML_FUZZ_PROB_ONE / 10 }, /* opts */
98
0
        { 4, XML_FUZZ_PROB_ONE / 10 }, /* failurePos */
99
0
        { 0, 0 }
100
0
    };
101
102
0
    return xmlFuzzMutateChunks(chunks, data, size, maxSize, seed,
103
0
                               LLVMFuzzerMutate);
104
0
}
105