Apache HTTPD
xml_parsebuffer_fuzzer.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <assert.h>
18#include <stdint.h>
19#include <string.h>
20
21#include "expat.h"
22#include "siphash.h"
23
24// Macros to convert preprocessor macros to string literals. See
25// https://gcc.gnu.org/onlinedocs/gcc-3.4.3/cpp/Stringification.html
26#define xstr(s) str(s)
27#define str(s) #s
28
29// The encoder type that we wish to fuzz should come from the compile-time
30// definition `ENCODING_FOR_FUZZING`. This allows us to have a separate fuzzer
31// binary for
32#ifndef ENCODING_FOR_FUZZING
33# error "ENCODING_FOR_FUZZING was not provided to this fuzz target."
34#endif
35
36// 16-byte deterministic hash key.
37static unsigned char hash_key[16] = "FUZZING IS FUN!";
38
39static void XMLCALL
40start(void *userData, const XML_Char *name, const XML_Char **atts) {
41 (void)userData;
42 (void)name;
43 (void)atts;
44}
45static void XMLCALL
46end(void *userData, const XML_Char *name) {
47 (void)userData;
48 (void)name;
49}
50
51static void XMLCALL
52may_stop_character_handler(void *userData, const XML_Char *s, int len) {
53 XML_Parser parser = (XML_Parser)userData;
54 if (len > 1 && s[0] == 's') {
56 }
57}
58
59static void
61 // Set the hash salt using siphash to generate a deterministic hash.
62 struct sipkey *key = sip_keyof(hash_key);
63 XML_SetHashSalt(p, (unsigned long)siphash24(data, size, key));
64 (void)sip24_valid;
65
69 void *buf = XML_GetBuffer(p, size);
70 assert(buf);
74 if (buf == NULL) {
75 return;
76 }
80 }
82 if (size % 2) {
84 }
85}
86
87int
const char apr_size_t len
Definition ap_regex.h:187
const XML_LChar * XML_ErrorString(enum XML_Error code)
Definition xmlparse.c:2429
#define XML_FALSE
Definition expat.h:59
#define XML_STATUS_ERROR
Definition expat.h:76
void XML_SetElementHandler(XML_Parser parser, XML_StartElementHandler start, XML_EndElementHandler end)
Definition xmlparse.c:1691
void XML_SetCharacterDataHandler(XML_Parser parser, XML_CharacterDataHandler handler)
Definition xmlparse.c:1712
XML_Bool XML_ParserReset(XML_Parser parser, const XML_Char *encoding)
Definition xmlparse.c:1286
enum XML_Status XML_StopParser(XML_Parser parser, XML_Bool resumable)
Definition xmlparse.c:2234
enum XML_Error XML_GetErrorCode(XML_Parser parser)
Definition xmlparse.c:2318
XML_Parser XML_ParserCreate(const XML_Char *encoding)
Definition xmlparse.c:766
void XML_ParserFree(XML_Parser parser)
Definition xmlparse.c:1537
XML_Size XML_GetCurrentLineNumber(XML_Parser parser)
Definition xmlparse.c:2364
struct XML_ParserStruct * XML_Parser
Definition expat.h:55
void XML_SetUserData(XML_Parser parser, void *userData)
Definition xmlparse.c:1637
XML_Parser XML_ParserCreateNS(const XML_Char *encoding, XML_Char namespaceSeparator)
Definition xmlparse.c:771
enum XML_Status XML_ParseBuffer(XML_Parser parser, int len, int isFinal)
Definition xmlparse.c:2037
int XML_SetHashSalt(XML_Parser parser, unsigned long hash_salt)
Definition xmlparse.c:1912
void * XML_GetBuffer(XML_Parser parser, int len)
Definition xmlparse.c:2108
#define XML_TRUE
Definition expat.h:58
XML_Parser XML_ExternalEntityParserCreate(XML_Parser parser, const XML_Char *context, const XML_Char *encoding)
Definition xmlparse.c:1354
char XML_Char
#define XMLCALL
const unsigned char * buf
Definition util_md5.h:50
apr_file_t apr_off_t start
apr_xml_parser ** parser
Definition apr_xml.h:228
apr_size_t size
const char * key
void * data
const char char ** end
const char * s
Definition apr_strings.h:95
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
static uint64_t siphash24(const void *src, size_t len, const struct sipkey *key)
Definition siphash.h:270
static int sip24_valid(void)
Definition siphash.h:287
#define sip_keyof(k)
Definition siphash.h:144
char * name
static unsigned char hash_key[16]
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
static void ParseOneInput(XML_Parser p, const uint8_t *data, size_t size)
#define xstr(s)
static void XMLCALL may_stop_character_handler(void *userData, const XML_Char *s, int len)