Skip to content

Commit 42b2d0e

Browse files
mem: add chance of failing flb_malloc when fuzzing (#4689)
* mem: add chance of failing flb_malloc when fuzzing this will increase coverage of fluent-bit code when fuzzing and also help us find places where there is no check on the value returned by flb_malloc. Signed-off-by: David Korczynski <[email protected]>
1 parent cd92cf3 commit 42b2d0e

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

include/fluent-bit/flb_mem.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,32 @@
4646
#define FLB_ALLOCSZ_ATTR(x,...)
4747
#endif
4848

49+
#ifdef FLB_HAVE_TESTS_OSSFUZZ
50+
/*
51+
* Return 1 or 0 based on a probability.
52+
*/
53+
int flb_malloc_p;
54+
55+
static inline int flb_fuzz_get_probability(int val) {
56+
flb_malloc_p += 1;
57+
flb_malloc_p = flb_malloc_p % 100;
58+
if (val > flb_malloc_p) {
59+
return 1;
60+
}
61+
return 0;
62+
}
63+
#endif
64+
4965
static inline FLB_ALLOCSZ_ATTR(1)
5066
void *flb_malloc(const size_t size) {
67+
68+
#ifdef FLB_HAVE_TESTS_OSSFUZZ
69+
// 1% chance of failure
70+
if (flb_fuzz_get_probability(1)) {
71+
return NULL;
72+
}
73+
#endif
74+
5175
if (size == 0) {
5276
return NULL;
5377
}

tests/internal/fuzzers/flb_fuzz_header.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ char *get_null_terminated(size_t size, const uint8_t **data,
2828
size_t *total_data_size)
2929
{
3030
char *tmp = flb_malloc(size+1);
31+
if (tmp == NULL) {
32+
tmp = malloc(size+1);
33+
}
3134
memcpy(tmp, *data, size);
3235
tmp[size] = '\0';
3336

tests/internal/fuzzers/utils_fuzzer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
3838
return 0;
3939
}
4040

41+
flb_malloc_p = 0;
42+
4143
uint64_t ran_hash = *(uint64_t *)data;
4244
char *null_terminated1 = get_null_terminated(25, &data, &size);
4345
char *null_terminated2 = get_null_terminated(25, &data, &size);

0 commit comments

Comments
 (0)