Skip to content

Commit

Permalink
Expand fuzztest to cover memory release in stream case
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriAimonen committed Dec 1, 2024
1 parent 2b86c25 commit 2157b52
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions tests/fuzztest/fuzztest.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 101,20 @@ static bool do_decode(const uint8_t *buffer, size_t msglen, size_t structsize, c
assert(status);
}

pb_release(msgtype, msg);
if (status)
{
/* On error return, pb_release() should be called automatically. */
pb_release(msgtype, msg);
}

free_with_check(msg);
free_with_check(buf2);
assert(get_alloc_count() == initial_alloc_count);

return status;
}

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 bool do_stream_decode(const uint8_t *buffer, size_t msglen, size_t fail_after, size_t structsize, const pb_msgdesc_t *msgtype, unsigned flags, bool assert_success)
{
bool status;
flakystream_t stream;
Expand All @@ -119,7 124,7 @@ static bool do_stream_decode(const uint8_t *buffer, size_t msglen, size_t fail_a

memset(msg, 0, structsize);
flakystream_init(&stream, buffer, msglen, fail_after);
status = pb_decode(&stream.stream, msgtype, msg);
status = pb_decode_ex(&stream.stream, msgtype, msg, flags);

if (status)
{
Expand All @@ -132,7 137,12 @@ static bool do_stream_decode(const uint8_t *buffer, size_t msglen, size_t fail_a
assert(status);
}

pb_release(msgtype, msg);
if (status)
{
/* On error return, pb_release() should be called automatically. */
pb_release(msgtype, msg);
}

free_with_check(msg);
assert(get_alloc_count() == initial_alloc_count);

Expand Down Expand Up @@ -295,7 305,7 @@ void do_roundtrips(const uint8_t *data, size_t size, bool expect_valid)
if (do_decode(data, size, sizeof(alltypes_static_AllTypes), alltypes_static_AllTypes_fields, 0, expect_valid))
{
do_roundtrip(data, size, sizeof(alltypes_static_AllTypes), alltypes_static_AllTypes_fields);
do_stream_decode(data, size, SIZE_MAX, sizeof(alltypes_static_AllTypes), alltypes_static_AllTypes_fields, true);
do_stream_decode(data, size, SIZE_MAX, sizeof(alltypes_static_AllTypes), alltypes_static_AllTypes_fields, 0, true);
do_callback_decode(data, size, true);
}
#endif
Expand All @@ -304,23 314,23 @@ void do_roundtrips(const uint8_t *data, size_t size, bool expect_valid)
if (do_decode(data, size, sizeof(alltypes_proto3_static_AllTypes), alltypes_proto3_static_AllTypes_fields, 0, expect_valid))
{
do_roundtrip(data, size, sizeof(alltypes_proto3_static_AllTypes), alltypes_proto3_static_AllTypes_fields);
do_stream_decode(data, size, SIZE_MAX, sizeof(alltypes_proto3_static_AllTypes), alltypes_proto3_static_AllTypes_fields, true);
do_stream_decode(data, size, SIZE_MAX, sizeof(alltypes_proto3_static_AllTypes), alltypes_proto3_static_AllTypes_fields, 0, true);
}
#endif

#ifdef FUZZTEST_PROTO2_POINTER
if (do_decode(data, size, sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields, 0, expect_valid))
{
do_roundtrip(data, size, sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields);
do_stream_decode(data, size, SIZE_MAX, sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields, true);
do_stream_decode(data, size, SIZE_MAX, sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields, 0, true);
}
#endif

#ifdef FUZZTEST_PROTO3_POINTER
if (do_decode(data, size, sizeof(alltypes_proto3_pointer_AllTypes), alltypes_proto3_pointer_AllTypes_fields, 0, expect_valid))
{
do_roundtrip(data, size, sizeof(alltypes_proto3_pointer_AllTypes), alltypes_proto3_pointer_AllTypes_fields);
do_stream_decode(data, size, SIZE_MAX, sizeof(alltypes_proto3_pointer_AllTypes), alltypes_proto3_pointer_AllTypes_fields, true);
do_stream_decode(data, size, SIZE_MAX, sizeof(alltypes_proto3_pointer_AllTypes), alltypes_proto3_pointer_AllTypes_fields, 0, true);
}
#endif

Expand All @@ -332,8 342,9 @@ void do_roundtrips(const uint8_t *data, size_t size, bool expect_valid)
* Testing proto2 is enough for good coverage here, as it has a superset of the field types of proto3.
*/
set_max_alloc_bytes(get_alloc_bytes() 4096);
do_stream_decode(data, size, size - 16, sizeof(alltypes_static_AllTypes), alltypes_static_AllTypes_fields, false);
do_stream_decode(data, size, size - 16, sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields, false);
do_stream_decode(data, size, size - 16, sizeof(alltypes_static_AllTypes), alltypes_static_AllTypes_fields, 0, false);
do_stream_decode(data, size, size - 16, sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields, 0, false);
do_stream_decode(data, size, size - 16, sizeof(alltypes_pointer_AllTypes), alltypes_pointer_AllTypes_fields, PB_DECODE_DELIMITED, false);
set_max_alloc_bytes(orig_max_alloc_bytes);
}

Expand Down

0 comments on commit 2157b52

Please sign in to comment.