Skip to content

Commit

Permalink
refactor: Allow overriding mono_time in tox_new.
Browse files Browse the repository at this point in the history
This makes it so if mono_time is overridden, no monotonic time-related
system call is invoked in tox_new.
  • Loading branch information
iphydf committed Apr 3, 2022
1 parent 6baabee commit 9410262
Show file tree
Hide file tree
Showing 28 changed files with 181 additions and 114 deletions.
12 changes: 6 additions & 6 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};

static void test_basic(void)
{
Mono_Time *mono_time = mono_time_new();
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
const Random *rng = system_random();
ck_assert(rng != nullptr);
Logger *logger = logger_new();
Expand Down Expand Up @@ -297,7 +297,7 @@ static int read_packet_sec_TCP(const Logger *logger, struct sec_TCP_con *con, ui

static void test_some(void)
{
Mono_Time *mono_time = mono_time_new();
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
const Random *rng = system_random();
ck_assert(rng != nullptr);
Logger *logger = logger_new();
Expand Down Expand Up @@ -488,9 +488,9 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint

static void test_client(void)
{
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
const Random *rng = system_random();
ck_assert(rng != nullptr);
Mono_Time *mono_time = mono_time_new();
Logger *logger = logger_new();

uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
Expand Down Expand Up @@ -619,9 +619,9 @@ static void test_client(void)
// Test how the client handles servers that don't respond.
static void test_client_invalid(void)
{
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
const Random *rng = system_random();
ck_assert(rng != nullptr);
Mono_Time *mono_time = mono_time_new();
Logger *logger = logger_new();
const Network *ns = system_network();

Expand Down Expand Up @@ -694,7 +694,7 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t

static void test_tcp_connection(void)
{
Mono_Time *mono_time = mono_time_new();
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
Logger *logger = logger_new();
const Random *rng = system_random();
ck_assert(rng != nullptr);
Expand Down Expand Up @@ -803,7 +803,7 @@ static int tcp_oobdata_callback(void *object, const uint8_t *public_key, unsigne

static void test_tcp_connection2(void)
{
Mono_Time *mono_time = mono_time_new();
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
Logger *logger = logger_new();
const Random *rng = system_random();
ck_assert(rng != nullptr);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/announce_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static void test_store_data(void)
Logger *log = logger_new();
ck_assert(log != nullptr);
logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new();
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
Networking_Core *net = new_networking_no_udp(log, ns);
DHT *dht = new_dht(log, rng, ns, mono_time, net, true, true);
Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void iterate_all_wait(AutoTox *autotoxes, uint32_t tox_count, uint32_t wait)
c_sleep(5);
}

static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data)
static uint64_t get_state_clock_callback(void *user_data)
{
const uint64_t *clock = (const uint64_t *)user_data;
return *clock;
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/forwarding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static Forwarding_Subtox *new_forwarding_subtox(bool no_udp, uint32_t *index, ui
subtox->log = logger_new();
ck_assert(subtox->log != nullptr);
logger_callback_log(subtox->log, (logger_cb *)print_debug_log, nullptr, index);
subtox->mono_time = mono_time_new();
subtox->mono_time = mono_time_new(nullptr, nullptr);

const Random *rng= system_random();
ck_assert(rng != nullptr);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/lan_discovery_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "../toxcore/tox_struct.h"
#include "auto_test_support.h"

static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data)
static uint64_t get_state_clock_callback(void *user_data)
{
const uint64_t *clock = (const uint64_t *)user_data;
return *clock;
Expand Down
8 changes: 4 additions & 4 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ static void test_basic(void)

const Random *rng = system_random();
ck_assert(rng != nullptr);
Mono_Time *mono_time1 = mono_time_new();
Mono_Time *mono_time2 = mono_time_new();
Mono_Time *mono_time1 = mono_time_new(nullptr, nullptr);
Mono_Time *mono_time2 = mono_time_new(nullptr, nullptr);

IP ip = get_loopback();
Onion *onion1 = new_onion(log1, mono_time1, rng, new_dht(log1, rng, ns, mono_time1, new_networking(log1, ns, &ip, 36567), true, false));
Expand Down Expand Up @@ -293,7 +293,7 @@ static void test_basic(void)
Logger *log3 = logger_new();
logger_callback_log(log3, (logger_cb *)print_debug_log, nullptr, &index[2]);

Mono_Time *mono_time3 = mono_time_new();
Mono_Time *mono_time3 = mono_time_new(nullptr, nullptr);

Onion *onion3 = new_onion(log3, mono_time3, rng, new_dht(log3, rng, ns, mono_time3, new_networking(log3, ns, &ip, 36569), true, false));
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");
Expand Down Expand Up @@ -380,7 +380,7 @@ static Onions *new_onions(const Random *rng, uint16_t port, uint32_t *index)

logger_callback_log(on->log, (logger_cb *)print_debug_log, nullptr, index);

on->mono_time = mono_time_new();
on->mono_time = mono_time_new(nullptr, nullptr);

if (!on->mono_time) {
logger_kill(on->log);
Expand Down
8 changes: 4 additions & 4 deletions auto_tests/save_load_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../testing/misc_tools.h"
#include "../toxcore/ccompat.h"
#include "../toxcore/tox.h"
#include "../toxcore/tox_struct.h"
#include "../toxcore/util.h"
#include "auto_test_support.h"
#include "check_compat.h"
Expand Down Expand Up @@ -109,7 +110,7 @@ typedef struct Time_Data {
uint64_t clock;
} Time_Data;

static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data)
static uint64_t get_state_clock_callback(void *user_data)
{
Time_Data *time_data = (Time_Data *)user_data;
pthread_mutex_lock(&time_data->lock);
Expand All @@ -127,8 +128,7 @@ static void increment_clock(Time_Data *time_data, uint64_t count)

static void set_current_time_callback(Tox *tox, Time_Data *time_data)
{
// TODO(iphydf): Don't rely on toxcore internals.
Mono_Time *mono_time = ((Messenger *)tox)->mono_time;
Mono_Time *mono_time = tox->mono_time;
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, time_data);
}

Expand Down Expand Up @@ -160,7 +160,7 @@ static void test_few_clients(void)

Time_Data time_data;
ck_assert_msg(pthread_mutex_init(&time_data.lock, nullptr) == 0, "Failed to init time_data mutex");
time_data.clock = current_time_monotonic(((Messenger *)tox1)->mono_time);
time_data.clock = current_time_monotonic(tox1->mono_time);
set_current_time_callback(tox1, &time_data);
set_current_time_callback(tox2, &time_data);
set_current_time_callback(tox3, &time_data);
Expand Down
11 changes: 5 additions & 6 deletions auto_tests/tox_events_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../testing/misc_tools.h"
#include "../toxcore/tox.h"
#include "../toxcore/tox_events.h"
#include "../toxcore/tox_struct.h"
#include "auto_test_support.h"
#include "check_compat.h"

Expand Down Expand Up @@ -37,7 +38,7 @@ static bool await_message(Tox **toxes)
return false;
}

static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data)
static uint64_t get_state_clock_callback(void *user_data)
{
const uint64_t *clock = (const uint64_t *)user_data;
return *clock;
Expand All @@ -58,14 +59,12 @@ static void test_tox_events(void)
ck_assert_msg(toxes[i] != nullptr, "failed to create tox instances %u", i);
}

// TODO(iphydf): Don't rely on toxcore internals.
uint64_t clock = current_time_monotonic(((Messenger *)toxes[0])->mono_time);
uint64_t clock = current_time_monotonic(toxes[0]->mono_time);
Mono_Time *mono_time;

// TODO(iphydf): Don't rely on toxcore internals.
mono_time = ((Messenger *)toxes[0])->mono_time;
mono_time = toxes[0]->mono_time;
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &clock);
mono_time = ((Messenger *)toxes[1])->mono_time;
mono_time = toxes[1]->mono_time;
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &clock);

uint8_t pk[TOX_PUBLIC_KEY_SIZE];
Expand Down
9 changes: 4 additions & 5 deletions auto_tests/toxav_many_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "../toxcore/crypto_core.h"
#include "../toxcore/logger.h"
#include "../toxcore/tox.h"
#include "../toxcore/tox_struct.h"
#include "../toxcore/util.h"
#include "auto_test_support.h"
#include "check_compat.h"
Expand Down Expand Up @@ -134,7 +135,7 @@ typedef struct Time_Data {
uint64_t clock;
} Time_Data;

static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data)
static uint64_t get_state_clock_callback(void *user_data)
{
Time_Data *time_data = (Time_Data *)user_data;
pthread_mutex_lock(&time_data->lock);
Expand All @@ -152,8 +153,7 @@ static void increment_clock(Time_Data *time_data, uint64_t count)

static void set_current_time_callback(Tox *tox, Time_Data *time_data)
{
// TODO(iphydf): Don't rely on toxcore internals.
Mono_Time *mono_time = ((Messenger *)tox)->mono_time;
Mono_Time *mono_time = tox->mono_time;
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, time_data);
}

Expand All @@ -174,8 +174,7 @@ static void test_av_three_calls(void)
bootstrap = tox_new_log(nullptr, &error, &index[0]);
ck_assert(error == TOX_ERR_NEW_OK);

// TODO(iphydf): Don't rely on toxcore internals.
time_data.clock = current_time_monotonic(((Messenger *)bootstrap)->mono_time);
time_data.clock = current_time_monotonic(bootstrap->mono_time);
set_current_time_callback(bootstrap, &time_data);

Alice = tox_new_log(nullptr, &error, &index[1]);
Expand Down
2 changes: 1 addition & 1 deletion other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ int main(int argc, char *argv[])
logger_callback_log(logger, print_log, nullptr, nullptr);
}

Mono_Time *mono_time = mono_time_new();
const Random *rng = system_random();
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
const uint16_t start_port = PORT;
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
const Network *ns = system_network();
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cfa2c61f92df59419af678ef9d6fcd4f5609a505235d77a54c1a4ec66f1c10af /usr/local/bin/tox-bootstrapd
5796f74532156d61d5c715914b5013ae0544892f7d6dca237b1f6a828f49fc9c /usr/local/bin/tox-bootstrapd
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ int main(int argc, char *argv[])
}
}

Mono_Time *const mono_time = mono_time_new();
Mono_Time *const mono_time = mono_time_new(nullptr, nullptr);

if (mono_time == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize monotonic timer. Exiting.\n");
Expand Down
2 changes: 1 addition & 1 deletion testing/Messenger_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int main(int argc, char *argv[])
exit(0);
}

Mono_Time *const mono_time = mono_time_new();
Mono_Time *const mono_time = mono_time_new(nullptr, nullptr);

if (mono_time == nullptr) {
fputs("Failed to allocate monotonic timer datastructure\n", stderr);
Expand Down
11 changes: 9 additions & 2 deletions testing/fuzzing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cc_library(
srcs = ["fuzz_support.cc"],
hdrs = ["fuzz_support.h"],
visibility = ["//c-toxcore:__subpackages__"],
deps = ["//c-toxcore/toxcore:tox"],
)

cc_library(
Expand All @@ -27,13 +28,19 @@ cc_fuzz_test(
srcs = ["bootstrap_harness.cc"],
copts = ["-UNDEBUG"],
corpus = ["//tools/toktok-fuzzer/corpus:bootstrap_fuzzer"],
deps = ["//c-toxcore/toxcore:tox"],
deps = [
":fuzz_support",
"//c-toxcore/toxcore:tox",
],
)

cc_fuzz_test(
name = "toxsave_fuzz_test",
srcs = ["toxsave_harness.cc"],
copts = ["-UNDEBUG"],
corpus = ["//tools/toktok-fuzzer/corpus:toxsave_fuzzer"],
deps = ["//c-toxcore/toxcore:tox"],
deps = [
":fuzz_support",
"//c-toxcore/toxcore:tox",
],
)
23 changes: 15 additions & 8 deletions testing/fuzzing/bootstrap_harness.cc
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
#include <cassert>
#include <cstring>
#include <memory>

#include "../../toxcore/tox.h"
#include "../../toxcore/tox_struct.h"
#include "../../toxcore/tox_private.h"
#include "fuzz_adapter.h"
#include "fuzz_support.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
network_adapter_init(data, size);

Tox_Err_New error_new;
Tox *tox = tox_new(nullptr, &error_new);

uint64_t clock = 0;
mono_time_set_current_time_callback(
tox->mono_time,
[](Mono_Time *mono_time, void *user_data) { return *static_cast<uint64_t *>(user_data); },
&clock);
auto sys = fuzz_system(clock);
assert(sys->mono_time_callback != nullptr);
assert(sys->mono_time_user_data != nullptr);

Tox_Options *opts = tox_options_new(nullptr);
assert(opts != nullptr);
tox_options_set_operating_system(opts, sys.get());

Tox_Err_New error_new;
Tox *tox = tox_new(opts, &error_new);

assert(tox != nullptr);
assert(error_new == TOX_ERR_NEW_OK);

tox_options_free(opts);

uint8_t pub_key[TOX_PUBLIC_KEY_SIZE] = {0};

const bool success = tox_bootstrap(tox, "127.0.0.1", 12345, pub_key, nullptr);
Expand Down
16 changes: 16 additions & 0 deletions testing/fuzzing/fuzz_support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,19 @@
*/

#include "fuzz_support.h"

#include <memory>

#include "../../toxcore/crypto_core.h"
#include "../../toxcore/network.h"
#include "../../toxcore/tox_private.h"

std::unique_ptr<Tox_System> fuzz_system(uint64_t &clock)
{
auto sys = std::make_unique<Tox_System>();
sys->mono_time_callback = [](void *user_data) { return *static_cast<uint64_t *>(user_data); };
sys->mono_time_user_data = &clock;
sys->rng = system_random(); // TODO(iphydf): Put fuzz_random here.
sys->ns = system_network(); // TODO(iphydf): Put fuzz_network here.
return sys;
}
5 changes: 5 additions & 0 deletions testing/fuzzing/fuzz_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

#include <cstdint>
#include <cstdlib>
#include <memory>
#include <utility>

#include "../../toxcore/tox.h"

struct Fuzz_Data {
const uint8_t *data;
std::size_t size;
Expand Down Expand Up @@ -94,4 +97,6 @@ void fuzz_select_target(const uint8_t *data, std::size_t size, Args &&... args)
return fuzz_select_target(selector, input, std::forward<Args>(args)...);
}

std::unique_ptr<Tox_System> fuzz_system(uint64_t &clock);

#endif // C_TOXCORE_TESTING_FUZZING_FUZZ_SUPPORT_H
6 changes: 6 additions & 0 deletions testing/fuzzing/toxsave_harness.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <vector>

#include "../../toxcore/tox.h"
#include "../../toxcore/tox_private.h"
#include "fuzz_support.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Expand All @@ -14,6 +16,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
assert(tox_options != nullptr);
assert(error_options == TOX_ERR_OPTIONS_NEW_OK);

uint64_t clock = 0;
auto sys = fuzz_system(clock);
tox_options_set_operating_system(tox_options, sys.get());

// pass test data to Tox
tox_options_set_savedata_data(tox_options, data, size);
tox_options_set_savedata_type(tox_options, TOX_SAVEDATA_TYPE_TOX_SAVE);
Expand Down
Loading

0 comments on commit 9410262

Please sign in to comment.