Skip to content

Commit

Permalink
test: Add a Null_System used in toxsave_harness.
Browse files Browse the repository at this point in the history
This does nothing but has a working simple RNG.
  • Loading branch information
iphydf committed Apr 14, 2022
1 parent 6b55792 commit debedb3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
76 changes: 74 additions & 2 deletions testing/fuzzing/fuzz_support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <sys/socket.h>

#include <algorithm>
#include <cerrno>
#include <cstring>
#include <memory>

Expand Down Expand Up @@ -117,8 +118,7 @@ static constexpr Random_Funcs fuzz_random_funcs = {
};

Fuzz_System::Fuzz_System(Fuzz_Data &input)
: clock(0)
, data(input)
: data(input)
, sys(std::make_unique<Tox_System>())
, ns(std::make_unique<Network>(Network{&fuzz_network_funcs, this}))
, rng(std::make_unique<Random>(Random{&fuzz_random_funcs, this}))
Expand All @@ -130,3 +130,75 @@ Fuzz_System::Fuzz_System(Fuzz_Data &input)
}

Fuzz_System::~Fuzz_System() { }

static constexpr Network_Funcs null_network_funcs = {
/* .close = */ [](void *obj, int sock) { return 0; },
/* .accept = */ [](void *obj, int sock) { return 2; },
/* .bind = */ [](void *obj, int sock, const Network_Addr *addr) { return 0; },
/* .listen = */ [](void *obj, int sock, int backlog) { return 0; },
/* .recvbuf = */ ![](Null_System *self, int sock) { return 0; },
/* .recv = */
![](Null_System *self, int sock, uint8_t *buf, size_t len) {
errno = ENOMEM;
return -1;
},
/* .recvfrom = */
![](Null_System *self, int sock, uint8_t *buf, size_t len, Network_Addr *addr) {
errno = ENOMEM;
return -1;
},
/* .send = */
[](void *obj, int sock, const uint8_t *buf, size_t len) {
// Always succeed.
return static_cast<int>(len);
},
/* .sendto = */
[](void *obj, int sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {
// Always succeed.
return static_cast<int>(len);
},
/* .socket = */ [](void *obj, int domain, int type, int proto) { return 1; },
/* .socket_nonblock = */ [](void *obj, int sock, bool nonblock) { return 0; },
/* .getsockopt = */
[](void *obj, int sock, int level, int optname, void *optval, size_t *optlen) {
memset(optval, 0, *optlen);
return 0;
},
/* .setsockopt = */
[](void *obj, int sock, int level, int optname, const void *optval, size_t optlen) {
return 0;
},
};

static uint64_t simple_rng(uint64_t &seed)
{
// https://nuclear.llnl.gov/CNP/rng/rngman/node4.html
seed = 2862933555777941757LL * seed + 3037000493LL;
return seed;
}

static constexpr Random_Funcs null_random_funcs = {
/* .random_bytes = */
![](Null_System *self, uint8_t *bytes, size_t length) {
for (size_t i = 0; i < length; ++i) {
bytes[i] = simple_rng(self->seed) & 0xff;
}
},
/* .random_uniform = */
![](Null_System *self, uint32_t upper_bound) {
return static_cast<uint32_t>(simple_rng(self->seed)) % upper_bound;
},
};

Null_System::Null_System()
: sys(std::make_unique<Tox_System>())
, ns(std::make_unique<Network>(Network{&null_network_funcs, this}))
, rng(std::make_unique<Random>(Random{&null_random_funcs, this}))
{
sys->mono_time_callback = ![](Fuzz_System *self) { return self->clock; };
sys->mono_time_user_data = this;
sys->ns = ns.get();
sys->rng = rng.get();
}

Null_System::~Null_System() { }
25 changes: 23 additions & 2 deletions testing/fuzzing/fuzz_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,36 @@ void fuzz_select_target(const uint8_t *data, std::size_t size, Args &&... args)
struct Network;
struct Random;

/**
* A Tox_System implementation that consumes fuzzer input to produce network
* inputs and random numbers. Once it runs out of fuzzer input, network receive
* functions return no more data and the random numbers are always zero.
*/
struct Fuzz_System {
uint64_t clock;
uint64_t clock = 0;
Fuzz_Data &data;
std::unique_ptr<Tox_System> sys;
std::unique_ptr<Network> ns;
std::unique_ptr<Random> rng;

Fuzz_System(Fuzz_Data &input);
explicit Fuzz_System(Fuzz_Data &input);
~Fuzz_System();
};

/**
* A Tox_System implementation that consumes no fuzzer input but still has a
* working and deterministic RNG. Network receive functions always fail, send
* always succeeds.
*/
struct Null_System {
uint64_t clock = 0;
uint64_t seed = 4; // chosen by fair dice roll. guaranteed to be random.
std::unique_ptr<Tox_System> sys;
std::unique_ptr<Network> ns;
std::unique_ptr<Random> rng;

Null_System();
~Null_System();
};

#endif // C_TOXCORE_TESTING_FUZZING_FUZZ_SUPPORT_H
2 changes: 1 addition & 1 deletion testing/fuzzing/toxsave_harness.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void TestSaveDataLoading(Fuzz_Data &input)
const size_t savedata_size = input.size;
CONSUME_OR_RETURN(const uint8_t *savedata, input, savedata_size);

Fuzz_System sys(input);
Null_System sys;
tox_options_set_operating_system(tox_options, sys.sys.get());

// pass test data to Tox
Expand Down

0 comments on commit debedb3

Please sign in to comment.