Skip to content

Commit

Permalink
move hacky code to fuzz_adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
sudden6 committed Dec 19, 2021
1 parent dcaf9f1 commit 349be5c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
9 changes: 9 additions & 0 deletions testing/fuzzing/fuzz_adapter.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "fuzz_adapter.h"

struct fuzz_buf {
/* Monotonic counter for time replacement */
uint64_t counter;
/* Fuzz data buffer */
const uint8_t *cur;
const uint8_t *end;
};
Expand All @@ -14,6 +17,7 @@ static struct fuzz_buf data;

void network_adapter_init(const uint8_t* buf, size_t length)
{
data.counter = 0;
data.cur = buf;
data.end = buf + length;
}
Expand Down Expand Up @@ -83,3 +87,8 @@ void fuzz_random_bytes(uint8_t *rnd, size_t length)
memcpy(rnd, data.cur, rd);
data.cur += rd;
}

uint64_t fuzz_get_cnt()
{
return data.counter++;
}
6 changes: 5 additions & 1 deletion testing/fuzzing/fuzz_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ ssize_t fuzz_recvfrom (int __fd, void *__restrict __buf, size_t __n,

ssize_t fuzz_recv (int __fd, void *__buf, size_t __n, int __flags);

/* The followig functions intercept generation of random data */
/* The following functions intercept generation of random data */
void fuzz_random_bytes(uint8_t *rnd, size_t length);

/* The following function replaces all time bases with a monotonic counter */

uint64_t fuzz_get_cnt();

#ifdef __cplusplus
}
#endif
Expand Down
13 changes: 6 additions & 7 deletions toxcore/mono_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

#include "ccompat.h"

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
#include "../testing/fuzzing/fuzz_adapter.h"
#endif

/* don't call into system billions of times for no reason */
struct Mono_Time {
uint64_t time;
Expand Down Expand Up @@ -97,20 +101,14 @@ static uint64_t current_time_monotonic_default(Mono_Time *mono_time, void *user_


#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
static uint64_t fuzz_time = 0;
static uint64_t current_time_monotonic_dummy(Mono_Time *mono_time, void *user_data)
{
return fuzz_time++;
return fuzz_get_cnt();
}

#endif

Mono_Time *mono_time_new(void)
{
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
fuzz_time = 0;
#endif

Mono_Time *mono_time = (Mono_Time *)malloc(sizeof(Mono_Time));

if (mono_time == nullptr) {
Expand Down Expand Up @@ -194,6 +192,7 @@ void mono_time_update(Mono_Time *mono_time)
uint64_t mono_time_get(const Mono_Time *mono_time)
{
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
// Fuzzing is only single thread for now, no locking needed */
return mono_time->time;
#else
uint64_t time = 0;
Expand Down

0 comments on commit 349be5c

Please sign in to comment.