21#include "alltypes_static.pb.h"
22#include "alltypes_pointer.pb.h"
23#include "alltypes_callback.pb.h"
24#include "alltypes_proto3_static.pb.h"
25#include "alltypes_proto3_pointer.pb.h"
28#ifndef FUZZTEST_BUFSIZE
29#define FUZZTEST_BUFSIZE 256*1024
31#ifndef FUZZTEST_MAX_STANDALONE_BUFSIZE
32#define FUZZTEST_MAX_STANDALONE_BUFSIZE 16384
39#if !defined(FUZZTEST_PROTO2_STATIC) && \
40 !defined(FUZZTEST_PROTO3_STATIC) && \
41 !defined(FUZZTEST_PROTO2_POINTER) && \
42 !defined(FUZZTEST_PROTO3_POINTER) && \
43 !defined(FUZZTEST_IO_ERRORS)
44#define FUZZTEST_PROTO2_STATIC
45#define FUZZTEST_PROTO3_STATIC
46#define FUZZTEST_PROTO2_POINTER
47#define FUZZTEST_PROTO3_POINTER
48#define FUZZTEST_IO_ERRORS
55 for (; len > 0; len--)
57 checksum ^= checksum << 13;
58 checksum ^= checksum >> 17;
59 checksum ^= checksum << 5;
65static bool do_decode(
const uint8_t *
buffer,
size_t msglen,
size_t structsize,
const pb_msgdesc_t *msgtype,
unsigned flags,
bool assert_success)
72 alltypes_static_TestExtension extmsg = alltypes_static_TestExtension_init_zero;
76 memset(msg, 0, structsize);
77 ext.type = &alltypes_static_TestExtension_testextension;
81 if (msgtype == alltypes_static_AllTypes_fields)
83 ((alltypes_static_AllTypes*)msg)->extensions = &ext;
85 else if (msgtype == alltypes_pointer_AllTypes_fields)
87 ((alltypes_pointer_AllTypes*)msg)->extensions = &ext;
120 memset(msg, 0, structsize);
165 alltypes_callback_AllTypes *msg =
malloc_with_check(
sizeof(alltypes_callback_AllTypes));
168 memset(msg, 0,
sizeof(alltypes_callback_AllTypes));
182 status =
pb_decode(&stream, alltypes_callback_AllTypes_fields, msg);
190 pb_release(alltypes_callback_AllTypes_fields, msg);
202 size_t msglen2, msglen3;
207 alltypes_static_TestExtension extmsg = alltypes_static_TestExtension_init_zero;
209 pb_extension_t **ext_field =
NULL;
210 ext.type = &alltypes_static_TestExtension_testextension;
216 if (msgtype == alltypes_static_AllTypes_fields)
218 ext_field = &((alltypes_static_AllTypes*)msg)->extensions;
220 else if (msgtype == alltypes_pointer_AllTypes_fields)
222 ext_field = &((alltypes_pointer_AllTypes*)msg)->extensions;
230 memset(msg, 0, structsize);
231 if (ext_field) *ext_field = &ext;
232 status =
pb_decode(&stream, msgtype, msg);
241 status =
pb_encode(&stream, msgtype, msg);
251 msglen2 = stream.bytes_written;
261 memset(msg, 0, structsize);
262 if (ext_field) *ext_field = &ext;
263 status =
pb_decode(&stream, msgtype, msg);
273 status =
pb_encode(&stream, msgtype, msg);
276 msglen3 = stream.bytes_written;
279 assert(msglen2 == msglen3);
280 assert(checksum2 == checksum3);
294#ifdef FUZZTEST_PROTO2_STATIC
295 if (
do_decode(data,
size,
sizeof(alltypes_static_AllTypes), alltypes_static_AllTypes_fields, 0, expect_valid))
297 do_roundtrip(data,
size,
sizeof(alltypes_static_AllTypes), alltypes_static_AllTypes_fields);
298 do_stream_decode(data,
size, SIZE_MAX,
sizeof(alltypes_static_AllTypes), alltypes_static_AllTypes_fields,
true);
303#ifdef FUZZTEST_PROTO3_STATIC
304 if (
do_decode(data,
size,
sizeof(alltypes_proto3_static_AllTypes), alltypes_proto3_static_AllTypes_fields, 0, expect_valid))
306 do_roundtrip(data,
size,
sizeof(alltypes_proto3_static_AllTypes), alltypes_proto3_static_AllTypes_fields);
307 do_stream_decode(data,
size, SIZE_MAX,
sizeof(alltypes_proto3_static_AllTypes), alltypes_proto3_static_AllTypes_fields,
true);
311#ifdef FUZZTEST_PROTO2_POINTER
312 if (
do_decode(data,
size,
sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields, 0, expect_valid))
314 do_roundtrip(data,
size,
sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields);
315 do_stream_decode(data,
size, SIZE_MAX,
sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields,
true);
319#ifdef FUZZTEST_PROTO3_POINTER
320 if (
do_decode(data,
size,
sizeof(alltypes_proto3_pointer_AllTypes), alltypes_proto3_pointer_AllTypes_fields, 0, expect_valid))
322 do_roundtrip(data,
size,
sizeof(alltypes_proto3_pointer_AllTypes), alltypes_proto3_pointer_AllTypes_fields);
323 do_stream_decode(data,
size, SIZE_MAX,
sizeof(alltypes_proto3_pointer_AllTypes), alltypes_proto3_pointer_AllTypes_fields,
true);
327#ifdef FUZZTEST_IO_ERRORS
336 do_stream_decode(data,
size,
size - 16,
sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields,
false);
368 static const alltypes_static_AllTypes initval = alltypes_static_AllTypes_init_default;
371 alltypes_static_AllTypes *msg =
malloc_with_check(
sizeof(alltypes_static_AllTypes));
372 memcpy(msg, &initval,
sizeof(initval));
378 msg->extensions =
NULL;
381 status =
pb_encode(&stream, alltypes_static_AllTypes_fields, msg);
382 assert(stream.bytes_written <=
g_bufsize);
383 assert(stream.bytes_written <= alltypes_static_AllTypes_size);
385 *msglen = stream.bytes_written;
386 pb_release(alltypes_static_AllTypes_fields, msg);
436 iterations = (
argc >= 3) ? atol(
argv[2]) : 10000;
438 for (i = 0; i < iterations; i++)
440 printf(
"Iteration %d/%d, seed %lu\n", i, iterations, (
unsigned long)
random_get_seed());
460 fprintf(
stderr,
"Warning: input message too long\n");
void flakystream_init(flakystream_t *stream, const uint8_t *buffer, size_t msglen, size_t fail_after)
bool do_callback_decode(const uint8_t *buffer, size_t msglen, bool assert_success)
static bool generate_base_message(uint8_t *buffer, size_t *msglen)
static bool field_callback(pb_istream_t *stream, const pb_field_t *field, void **arg)
#define FUZZTEST_MAX_STANDALONE_BUFSIZE
static void run_iteration()
void do_roundtrip(const uint8_t *buffer, size_t msglen, size_t structsize, const pb_msgdesc_t *msgtype)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
static bool submsg_callback(pb_istream_t *stream, const pb_field_t *field, void **arg)
static bool do_stream_decode(const uint8_t *buffer, size_t msglen, size_t fail_after, size_t structsize, const pb_msgdesc_t *msgtype, bool assert_success)
static uint32_t xor32_checksum(const void *data, size_t len)
void do_roundtrips(const uint8_t *data, size_t size, bool expect_valid)
MOSAPI s32 strcmp(const char *str1, const char *str2)
size_t fread(void *__restrict ptr, size_t size, size_t nmemb, FILE *__restrict stream)
MOSAPI unsigned long strtoul(const char *nptr, char **endptr, int base)
void * malloc_with_check(size_t size)
void free_with_check(void *mem)
size_t get_max_alloc_bytes()
void set_max_alloc_bytes(size_t max_bytes)
#define PB_GET_ERROR(stream)
pb_field_iter_t pb_field_t
#define pb_extension_init_zero
bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
#define PB_DECODE_DELIMITED
#define PB_DECODE_NULLTERMINATED
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
static void * memcpy(void *s1, const void *s2, size_t n)
static void * memset(void *s, int c, size_t n)
void random_set_seed(uint32_t seed)
uint32_t random_get_seed()
void rand_fill(uint8_t *buf, size_t count)
void rand_mess(uint8_t *buf, size_t count)
int rand_int(int min, int max)
void rand_protobuf_noise(uint8_t *buffer, size_t bufsize, size_t *msglen)
#define SET_BINARY_MODE(file)
void validate_message(const void *msg, size_t structsize, const pb_msgdesc_t *msgtype)