| 1 |
/* |
| 2 |
* html.c: a libFuzzer target to test several HTML 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/HTMLparser.h> |
| 12 |
#include <libxml/HTMLtree.h> |
| 13 |
#include <libxml/catalog.h> |
| 14 |
#include "fuzz.h" |
| 15 |
|
| 16 |
int |
| 17 |
LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED, |
| 18 |
char ***argv ATTRIBUTE_UNUSED) { |
| 19 |
xmlFuzzMemSetup(); |
| 20 |
xmlInitParser(); |
| 21 |
#ifdef LIBXML_CATALOG_ENABLED |
| 22 |
xmlInitializeCatalog(); |
| 23 |
xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE); |
| 24 |
#endif |
| 25 |
|
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
int |
| 30 |
LLVMFuzzerTestOneInput(const char *data, size_t size) { |
| 31 |
xmlParserCtxtPtr ctxt; |
| 32 |
htmlDocPtr doc; |
| 33 |
const char *docBuffer; |
| 34 |
size_t failurePos, docSize, maxChunkSize; |
| 35 |
int opts, errorCode; |
| 36 |
#ifdef LIBXML_OUTPUT_ENABLED |
| 37 |
xmlOutputBufferPtr out = NULL; |
| 38 |
#endif |
| 39 |
|
| 40 |
xmlFuzzDataInit(data, size); |
| 41 |
opts = (int) xmlFuzzReadInt(4); |
| 42 |
failurePos = xmlFuzzReadInt(4) % (size + 100); |
| 43 |
|
| 44 |
maxChunkSize = xmlFuzzReadInt(4) % (size + size / 8 + 1); |
| 45 |
if (maxChunkSize == 0) |
| 46 |
maxChunkSize = 1; |
| 47 |
|
| 48 |
docBuffer = xmlFuzzReadRemaining(&docSize); |
| 49 |
if (docBuffer == NULL) { |
| 50 |
xmlFuzzDataCleanup(); |
| 51 |
return(0); |
| 52 |
} |
| 53 |
|
| 54 |
/* Pull parser */ |
| 55 |
|
| 56 |
xmlFuzzInjectFailure(failurePos); |
| 57 |
ctxt = htmlNewParserCtxt(); |
| 58 |
if (ctxt == NULL) { |
| 59 |
errorCode = XML_ERR_NO_MEMORY; |
| 60 |
} else { |
| 61 |
xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL); |
| 62 |
doc = htmlCtxtReadMemory(ctxt, docBuffer, docSize, NULL, NULL, opts); |
| 63 |
errorCode = ctxt->errNo; |
| 64 |
xmlFuzzCheckFailureReport("htmlCtxtReadMemory", |
| 65 |
errorCode == XML_ERR_NO_MEMORY, |
| 66 |
errorCode == XML_IO_EIO); |
| 67 |
|
| 68 |
if (doc != NULL) { |
| 69 |
xmlDocPtr copy; |
| 70 |
|
| 71 |
#ifdef LIBXML_OUTPUT_ENABLED |
| 72 |
const xmlChar *content; |
| 73 |
|
| 74 |
/* |
| 75 |
* Also test the serializer. Call htmlDocContentDumpOutput with our |
| 76 |
* own buffer to avoid encoding the output. The HTML encoding is |
| 77 |
* excruciatingly slow (see htmlEntityValueLookup). |
| 78 |
*/ |
| 79 |
out = xmlAllocOutputBuffer(NULL); |
| 80 |
htmlDocContentDumpOutput(out, doc, NULL); |
| 81 |
content = xmlOutputBufferGetContent(out); |
| 82 |
xmlFuzzCheckFailureReport("htmlDocContentDumpOutput", |
| 83 |
content == NULL, 0); |
| 84 |
if (content == NULL) { |
| 85 |
xmlOutputBufferClose(out); |
| 86 |
out = NULL; |
| 87 |
} |
| 88 |
#endif |
| 89 |
|
| 90 |
copy = xmlCopyDoc(doc, 1); |
| 91 |
xmlFuzzCheckFailureReport("xmlCopyNode", copy == NULL, 0); |
| 92 |
xmlFreeDoc(copy); |
| 93 |
|
| 94 |
xmlFreeDoc(doc); |
| 95 |
} |
| 96 |
|
| 97 |
htmlFreeParserCtxt(ctxt); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/* Push parser */ |
| 102 |
|
| 103 |
#ifdef LIBXML_PUSH_ENABLED |
| 104 |
xmlFuzzInjectFailure(failurePos); |
| 105 |
ctxt = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL, |
| 106 |
XML_CHAR_ENCODING_NONE); |
| 107 |
|
| 108 |
if (ctxt != NULL) { |
| 109 |
size_t consumed; |
| 110 |
int errorCodePush, numChunks, maxChunks; |
| 111 |
|
| 112 |
xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL); |
| 113 |
htmlCtxtUseOptions(ctxt, opts); |
| 114 |
|
| 115 |
consumed = 0; |
| 116 |
numChunks = 0; |
| 117 |
maxChunks = 50 + docSize / 100; |
| 118 |
while (numChunks == 0 || |
| 119 |
(consumed < docSize && numChunks < maxChunks)) { |
| 120 |
size_t chunkSize; |
| 121 |
int terminate; |
| 122 |
|
| 123 |
numChunks += 1; |
| 124 |
chunkSize = docSize - consumed; |
| 125 |
|
| 126 |
if (numChunks < maxChunks && chunkSize > maxChunkSize) { |
| 127 |
chunkSize = maxChunkSize; |
| 128 |
terminate = 0; |
| 129 |
} else { |
| 130 |
terminate = 1; |
| 131 |
} |
| 132 |
|
| 133 |
htmlParseChunk(ctxt, docBuffer + consumed, chunkSize, terminate); |
| 134 |
consumed += chunkSize; |
| 135 |
} |
| 136 |
|
| 137 |
errorCodePush = ctxt->errNo; |
| 138 |
xmlFuzzCheckFailureReport("htmlParseChunk", |
| 139 |
errorCodePush == XML_ERR_NO_MEMORY, |
| 140 |
errorCodePush == XML_IO_EIO); |
| 141 |
doc = ctxt->myDoc; |
| 142 |
|
| 143 |
/* |
| 144 |
* Push and pull parser differ in when exactly they |
| 145 |
* stop parsing, and the error code is the *last* error |
| 146 |
* reported, so we can't check whether the codes match. |
| 147 |
*/ |
| 148 |
if (errorCode != XML_ERR_NO_MEMORY && |
| 149 |
errorCode != XML_IO_EIO && |
| 150 |
errorCodePush != XML_ERR_NO_MEMORY && |
| 151 |
errorCodePush != XML_IO_EIO && |
| 152 |
(errorCode == XML_ERR_OK) != (errorCodePush == XML_ERR_OK)) { |
| 153 |
fprintf(stderr, "pull/push parser error mismatch: %d != %d\n", |
| 154 |
errorCode, errorCodePush); |
| 155 |
#if 0 |
| 156 |
FILE *f = fopen("c.html", "wb"); |
| 157 |
fwrite(docBuffer, docSize, 1, f); |
| 158 |
fclose(f); |
| 159 |
fprintf(stderr, "opts: %X\n", opts); |
| 160 |
#endif |
| 161 |
abort(); |
| 162 |
} |
| 163 |
|
| 164 |
#ifdef LIBXML_OUTPUT_ENABLED |
| 165 |
/* |
| 166 |
* Verify that pull and push parser produce the same result. |
| 167 |
* |
| 168 |
* The NOBLANKS option doesn't work reliably in push mode. |
| 169 |
*/ |
| 170 |
if ((opts & XML_PARSE_NOBLANKS) == 0 && |
| 171 |
errorCode == XML_ERR_OK && |
| 172 |
errorCodePush == XML_ERR_OK && |
| 173 |
out != NULL) { |
| 174 |
xmlOutputBufferPtr outPush; |
| 175 |
const xmlChar *content, *contentPush; |
| 176 |
|
| 177 |
outPush = xmlAllocOutputBuffer(NULL); |
| 178 |
htmlDocContentDumpOutput(outPush, doc, NULL); |
| 179 |
content = xmlOutputBufferGetContent(out); |
| 180 |
contentPush = xmlOutputBufferGetContent(outPush); |
| 181 |
|
| 182 |
if (content != NULL && contentPush != NULL) { |
| 183 |
size_t outSize = xmlOutputBufferGetSize(out); |
| 184 |
|
| 185 |
if (outSize != xmlOutputBufferGetSize(outPush) || |
| 186 |
memcmp(content, contentPush, outSize) != 0) { |
| 187 |
fprintf(stderr, "pull/push parser roundtrip " |
| 188 |
"mismatch\n"); |
| 189 |
#if 0 |
| 190 |
FILE *f = fopen("c.html", "wb"); |
| 191 |
fwrite(docBuffer, docSize, 1, f); |
| 192 |
fclose(f); |
| 193 |
fprintf(stderr, "opts: %X\n", opts); |
| 194 |
fprintf(stderr, "---\n%s\n---\n%s\n---\n", |
| 195 |
xmlOutputBufferGetContent(out), |
| 196 |
xmlOutputBufferGetContent(outPush)); |
| 197 |
#endif |
| 198 |
abort(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
xmlOutputBufferClose(outPush); |
| 203 |
} |
| 204 |
#endif |
| 205 |
|
| 206 |
xmlFreeDoc(doc); |
| 207 |
htmlFreeParserCtxt(ctxt); |
| 208 |
} |
| 209 |
#endif |
| 210 |
|
| 211 |
/* Cleanup */ |
| 212 |
|
| 213 |
#ifdef LIBXML_OUTPUT_ENABLED |
| 214 |
xmlOutputBufferClose(out); |
| 215 |
#endif |
| 216 |
|
| 217 |
xmlFuzzInjectFailure(0); |
| 218 |
xmlFuzzDataCleanup(); |
| 219 |
xmlResetLastError(); |
| 220 |
|
| 221 |
return(0); |
| 222 |
} |
| 223 |
|
| 224 |
size_t |
| 225 |
LLVMFuzzerCustomMutator(char *data, size_t size, size_t maxSize, |
| 226 |
unsigned seed) { |
| 227 |
static const xmlFuzzChunkDesc chunks[] = { |
| 228 |
{ 4, XML_FUZZ_PROB_ONE / 10 }, /* opts */ |
| 229 |
{ 4, XML_FUZZ_PROB_ONE / 10 }, /* failurePos */ |
| 230 |
{ 0, 0 } |
| 231 |
}; |
| 232 |
|
| 233 |
return xmlFuzzMutateChunks(chunks, data, size, maxSize, seed, |
| 234 |
LLVMFuzzerMutate); |
| 235 |
} |
| 236 |
|