/src/libxml2/fuzz/schema.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * schema.c: a libFuzzer target to test the XML Schema processor. |
3 | | * |
4 | | * See Copyright for the status of this software. |
5 | | */ |
6 | | |
7 | | #ifndef XML_DEPRECATED |
8 | | #define XML_DEPRECATED |
9 | | #endif |
10 | | |
11 | | #include <libxml/catalog.h> |
12 | | #include <libxml/xmlschemas.h> |
13 | | #include <libxml/xmlschemastypes.h> |
14 | | #include "fuzz.h" |
15 | | |
16 | | int |
17 | | LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED, |
18 | 20 | char ***argv ATTRIBUTE_UNUSED) { |
19 | 20 | xmlFuzzMemSetup(); |
20 | 20 | xmlInitParser(); |
21 | 20 | #ifdef LIBXML_CATALOG_ENABLED |
22 | 20 | xmlInitializeCatalog(); |
23 | 20 | xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE); |
24 | 20 | #endif |
25 | | |
26 | 20 | return 0; |
27 | 20 | } |
28 | | |
29 | | int |
30 | 32.8k | LLVMFuzzerTestOneInput(const char *data, size_t size) { |
31 | 32.8k | xmlSchemaParserCtxtPtr pctxt; |
32 | 32.8k | xmlSchemaPtr schema; |
33 | 32.8k | size_t failurePos; |
34 | | |
35 | 32.8k | if (size > 200000) |
36 | 1 | return(0); |
37 | | |
38 | 32.8k | xmlFuzzDataInit(data, size); |
39 | | |
40 | 32.8k | failurePos = xmlFuzzReadInt(4) % (size + 100); |
41 | | |
42 | 32.8k | xmlFuzzReadEntities(); |
43 | | |
44 | 32.8k | xmlFuzzInjectFailure(failurePos); |
45 | 32.8k | pctxt = xmlSchemaNewParserCtxt(xmlFuzzMainUrl()); |
46 | 32.8k | xmlSchemaSetParserStructuredErrors(pctxt, xmlFuzzSErrorFunc, NULL); |
47 | 32.8k | xmlSchemaSetResourceLoader(pctxt, xmlFuzzResourceLoader, NULL); |
48 | 32.8k | schema = xmlSchemaParse(pctxt); |
49 | 32.8k | xmlSchemaFreeParserCtxt(pctxt); |
50 | | |
51 | 32.8k | if (schema != NULL) { |
52 | 5.58k | xmlSchemaValidCtxtPtr vctxt; |
53 | 5.58k | xmlParserCtxtPtr ctxt; |
54 | 5.58k | xmlDocPtr doc; |
55 | | |
56 | 5.58k | ctxt = xmlNewParserCtxt(); |
57 | 5.58k | xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL); |
58 | 5.58k | xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL); |
59 | 5.58k | doc = xmlCtxtReadFile(ctxt, xmlFuzzSecondaryUrl(), NULL, |
60 | 5.58k | XML_PARSE_NOENT); |
61 | 5.58k | xmlFreeParserCtxt(ctxt); |
62 | | |
63 | 5.58k | vctxt = xmlSchemaNewValidCtxt(schema); |
64 | 5.58k | xmlSchemaSetValidStructuredErrors(vctxt, xmlFuzzSErrorFunc, NULL); |
65 | 5.58k | xmlSchemaValidateDoc(vctxt, doc); |
66 | 5.58k | xmlSchemaFreeValidCtxt(vctxt); |
67 | | |
68 | 5.58k | xmlFreeDoc(doc); |
69 | 5.58k | xmlSchemaFree(schema); |
70 | 5.58k | } |
71 | | |
72 | 32.8k | xmlFuzzInjectFailure(0); |
73 | 32.8k | xmlFuzzDataCleanup(); |
74 | 32.8k | xmlResetLastError(); |
75 | 32.8k | xmlSchemaCleanupTypes(); |
76 | | |
77 | 32.8k | return(0); |
78 | 32.8k | } |
79 | | |
80 | | size_t |
81 | | LLVMFuzzerCustomMutator(char *data, size_t size, size_t maxSize, |
82 | 0 | unsigned seed) { |
83 | 0 | static const xmlFuzzChunkDesc chunks[] = { |
84 | 0 | { 4, XML_FUZZ_PROB_ONE / 10 }, /* failurePos */ |
85 | 0 | { 0, 0 } |
86 | 0 | }; |
87 | |
|
88 | 0 | return xmlFuzzMutateChunks(chunks, data, size, maxSize, seed, |
89 | 0 | LLVMFuzzerMutate); |
90 | 0 | } |
91 | | |