Squashed 'external/toxcore/c-toxcore/' changes from 6d634674a9..73d9b845a3
73d9b845a3 cleanup: Remove old type-ordered event getters. b0840cc02d feat: add ngc events 7df9a51349 refactor: Make event dispatch ordered by receive time. bcb6592af5 test: Add C++ classes wrapping system interfaces. 4cea4f9ca4 fix: Make all the fuzzers work again, and add a test for protodump. c4e209ea1d refactor: Factor out malloc+memcpy into memdup. 87bcc4322d fix: Remove fatal error for non-erroneous case REVERT: 6d634674a9 cleanup: Remove old type-ordered event getters. REVERT: d1d48d1dfc feat: add ngc events REVERT: 994ffecc6b refactor: Make event dispatch ordered by receive time. git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 73d9b845a310c3f56d2d6d77ed56b93d84256d6e
This commit is contained in:
@ -20,10 +20,6 @@
|
||||
#include "../../toxcore/tox_private.h"
|
||||
#include "func_conversion.h"
|
||||
|
||||
const bool DEBUG = false;
|
||||
|
||||
static constexpr tox_mono_time_cb *get_self_clock =  { return self->clock; };
|
||||
|
||||
// TODO(iphydf): Put this somewhere shared.
|
||||
struct Network_Addr {
|
||||
struct sockaddr_storage addr;
|
||||
@ -44,58 +40,67 @@ System::~System() { }
|
||||
|
||||
static int recv_common(Fuzz_Data &input, uint8_t *buf, size_t buf_len)
|
||||
{
|
||||
if (input.size < 2) {
|
||||
if (input.size() < 2) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
const size_t fuzz_len = (input.data[0] << 8) | input.data[1];
|
||||
input.data += 2;
|
||||
input.size -= 2;
|
||||
CONSUME_OR_ABORT(const uint8_t *fuzz_len_bytes, input, 2);
|
||||
const std::size_t fuzz_len = (fuzz_len_bytes[0] << 8) | fuzz_len_bytes[1];
|
||||
|
||||
if (fuzz_len == 0xffff) {
|
||||
errno = EWOULDBLOCK;
|
||||
if (DEBUG) {
|
||||
if (Fuzz_Data::DEBUG) {
|
||||
std::printf("recvfrom: no data for tox1\n");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (DEBUG) {
|
||||
if (Fuzz_Data::DEBUG) {
|
||||
std::printf(
|
||||
"recvfrom: %zu (%02x, %02x) for tox1\n", fuzz_len, input.data[-2], input.data[-1]);
|
||||
"recvfrom: %zu (%02x, %02x) for tox1\n", fuzz_len, input.data()[-2], input.data()[-1]);
|
||||
}
|
||||
const size_t res = std::min(buf_len, std::min(fuzz_len, input.size));
|
||||
const size_t res = std::min(buf_len, std::min(fuzz_len, input.size()));
|
||||
|
||||
std::copy(input.data, input.data + res, buf);
|
||||
input.data += res;
|
||||
input.size -= res;
|
||||
CONSUME_OR_ABORT(const uint8_t *data, input, res);
|
||||
std::copy(data, data + res, buf);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
static void *alloc_common(Fuzz_Data &data, F func)
|
||||
static void *report_alloc(const char *name, const char *func, std::size_t size, void *ptr)
|
||||
{
|
||||
CONSUME1_OR_RETURN_VAL(const uint8_t, want_alloc, data, func());
|
||||
if (Fuzz_Data::DEBUG) {
|
||||
printf("%s: %s(%zu): %s\n", name, func, size, ptr == nullptr ? "false" : "true");
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
template <typename F, F Func, typename... Args>
|
||||
static void *alloc_common(const char *func, std::size_t size, Fuzz_Data &data, Args... args)
|
||||
{
|
||||
CONSUME1_OR_RETURN_VAL(
|
||||
const bool, want_alloc, data, report_alloc("tox1", func, size, Func(args...)));
|
||||
if (!want_alloc) {
|
||||
return nullptr;
|
||||
}
|
||||
return func();
|
||||
return report_alloc("tox1", func, size, Func(args...));
|
||||
}
|
||||
|
||||
static constexpr Memory_Funcs fuzz_memory_funcs = {
|
||||
/* .malloc = */
|
||||
 {
|
||||
return alloc_common(self->data, [=]() { return std::malloc(size); });
|
||||
return alloc_common<decltype(std::malloc), std::malloc>("malloc", size, self->data, size);
|
||||
},
|
||||
/* .calloc = */
|
||||
 {
|
||||
return alloc_common(self->data, [=]() { return std::calloc(nmemb, size); });
|
||||
return alloc_common<decltype(std::calloc), std::calloc>(
|
||||
"calloc", nmemb * size, self->data, nmemb, size);
|
||||
},
|
||||
/* .realloc = */
|
||||
 {
|
||||
return alloc_common(self->data, [=]() { return std::realloc(ptr, size); });
|
||||
return alloc_common<decltype(std::realloc), std::realloc>(
|
||||
"realloc", size, self->data, ptr, size);
|
||||
},
|
||||
/* .free = */
|
||||
 { std::free(ptr); },
|
||||
@ -110,7 +115,7 @@ static constexpr Network_Funcs fuzz_network_funcs = {
|
||||
 {
|
||||
assert(sock == 42 || sock == 1337);
|
||||
const size_t count = random_u16(self->rng.get());
|
||||
return static_cast<int>(std::min(count, self->data.size));
|
||||
return static_cast<int>(std::min(count, self->data.size()));
|
||||
},
|
||||
/* .recv = */
|
||||
 {
|
||||
@ -163,14 +168,17 @@ static constexpr Random_Funcs fuzz_random_funcs = {
|
||||
/* .random_bytes = */
|
||||
 {
|
||||
// Amount of data is limited
|
||||
const size_t bytes_read = std::min(length, self->data.size);
|
||||
const size_t bytes_read = std::min(length, self->data.size());
|
||||
// Initialize everything to make MSAN and others happy
|
||||
std::memset(bytes, 0, length);
|
||||
std::copy(self->data.data, self->data.data + bytes_read, bytes);
|
||||
self->data.data += bytes_read;
|
||||
self->data.size -= bytes_read;
|
||||
if (DEBUG) {
|
||||
std::printf("rng: %02x..%02x[%zu] -> tox1\n", bytes[0], bytes[length - 1], length);
|
||||
CONSUME_OR_ABORT(const uint8_t *data, self->data, bytes_read);
|
||||
std::copy(data, data + bytes_read, bytes);
|
||||
if (Fuzz_Data::DEBUG) {
|
||||
if (length == 1) {
|
||||
std::printf("rng: %d (0x%02x)\n", bytes[0], bytes[0]);
|
||||
} else {
|
||||
std::printf("rng: %02x..%02x[%zu]\n", bytes[0], bytes[length - 1], length);
|
||||
}
|
||||
}
|
||||
},
|
||||
/* .random_uniform = */
|
||||
@ -194,7 +202,7 @@ Fuzz_System::Fuzz_System(Fuzz_Data &input)
|
||||
}
|
||||
, data(input)
|
||||
{
|
||||
sys->mono_time_callback = get_self_clock;
|
||||
sys->mono_time_callback = [](void *self) { return static_cast<Fuzz_System *>(self)->clock; };
|
||||
sys->mono_time_user_data = this;
|
||||
sys->mem = mem.get();
|
||||
sys->ns = ns.get();
|
||||
@ -281,7 +289,7 @@ Null_System::Null_System()
|
||||
std::make_unique<Random>(Random{&null_random_funcs, this}),
|
||||
}
|
||||
{
|
||||
sys->mono_time_callback = get_self_clock;
|
||||
sys->mono_time_callback = [](void *self) { return static_cast<Null_System *>(self)->clock; };
|
||||
sys->mono_time_user_data = this;
|
||||
sys->mem = mem.get();
|
||||
sys->ns = ns.get();
|
||||
@ -298,7 +306,25 @@ static uint16_t get_port(const Network_Addr *addr)
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr Memory_Funcs record_memory_funcs = null_memory_funcs;
|
||||
static constexpr Memory_Funcs record_memory_funcs = {
|
||||
/* .malloc = */
|
||||
 {
|
||||
self->push(true);
|
||||
return report_alloc(self->name_, "malloc", size, std::malloc(size));
|
||||
},
|
||||
/* .calloc = */
|
||||
 {
|
||||
self->push(true);
|
||||
return report_alloc(self->name_, "calloc", nmemb * size, std::calloc(nmemb, size));
|
||||
},
|
||||
/* .realloc = */
|
||||
 {
|
||||
self->push(true);
|
||||
return report_alloc(self->name_, "realloc", size, std::realloc(ptr, size));
|
||||
},
|
||||
/* .free = */
|
||||
 { std::free(ptr); },
|
||||
};
|
||||
|
||||
static constexpr Network_Funcs record_network_funcs = {
|
||||
/* .close = */  { return 0; },
|
||||
@ -326,11 +352,10 @@ static constexpr Network_Funcs record_network_funcs = {
|
||||
 {
|
||||
assert(sock == 42);
|
||||
if (self->recvq.empty()) {
|
||||
self->recording.push_back(0xff);
|
||||
self->recording.push_back(0xff);
|
||||
self->push("\xff\xff");
|
||||
errno = EWOULDBLOCK;
|
||||
if (DEBUG) {
|
||||
std::printf("recvfrom: no data for %s\n", self->name_);
|
||||
if (Fuzz_Data::DEBUG) {
|
||||
std::printf("%s: recvfrom: no data\n", self->name_);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@ -350,13 +375,13 @@ static constexpr Network_Funcs record_network_funcs = {
|
||||
addr->size = sizeof(struct sockaddr);
|
||||
|
||||
assert(recvlen > 0 && recvlen <= INT_MAX);
|
||||
self->recording.push_back(recvlen >> 8);
|
||||
self->recording.push_back(recvlen & 0xff);
|
||||
if (DEBUG) {
|
||||
std::printf("recvfrom: %zu (%02x, %02x) for %s\n", recvlen, self->recording.end()[-2],
|
||||
self->recording.end()[-1], self->name_);
|
||||
self->push(uint8_t(recvlen >> 8));
|
||||
self->push(uint8_t(recvlen & 0xff));
|
||||
if (Fuzz_Data::DEBUG) {
|
||||
std::printf("%s: recvfrom: %zu (%02x, %02x)\n", self->name_, recvlen,
|
||||
self->recording().end()[-2], self->recording().end()[-1]);
|
||||
}
|
||||
self->recording.insert(self->recording.end(), buf, buf + recvlen);
|
||||
self->push(buf, recvlen);
|
||||
return static_cast<int>(recvlen);
|
||||
},
|
||||
/* .send = */
|
||||
@ -390,11 +415,11 @@ static constexpr Random_Funcs record_random_funcs = {
|
||||
 {
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
bytes[i] = simple_rng(self->seed_) & 0xff;
|
||||
self->recording.push_back(bytes[i]);
|
||||
self->push(bytes[i]);
|
||||
}
|
||||
if (DEBUG) {
|
||||
if (Fuzz_Data::DEBUG) {
|
||||
std::printf(
|
||||
"rng: %02x..%02x[%zu] -> %s\n", bytes[0], bytes[length - 1], length, self->name_);
|
||||
"%s: rng: %02x..%02x[%zu]\n", self->name_, bytes[0], bytes[length - 1], length);
|
||||
}
|
||||
},
|
||||
/* .random_uniform = */
|
||||
@ -412,7 +437,7 @@ Record_System::Record_System(Global &global, uint64_t seed, const char *name)
|
||||
, seed_(seed)
|
||||
, name_(name)
|
||||
{
|
||||
sys->mono_time_callback = get_self_clock;
|
||||
sys->mono_time_callback = [](void *self) { return static_cast<Record_System *>(self)->clock; };
|
||||
sys->mono_time_user_data = this;
|
||||
sys->mem = mem.get();
|
||||
sys->ns = ns.get();
|
||||
|
Reference in New Issue
Block a user