Skip to content

Commit eb51056

Browse files
committed
refactor: Add mem module to allow tests to override allocators.
This will allow us to do more interesting things with memory allocation within toxcore, and allow fuzzers to explore various allocation failure paths.
1 parent 8ed47f3 commit eb51056

34 files changed

+450
-155
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ set(toxcore_SOURCES
268268
toxcore/logger.h
269269
toxcore/Messenger.c
270270
toxcore/Messenger.h
271+
toxcore/mem.c
272+
toxcore/mem.h
271273
toxcore/mono_time.c
272274
toxcore/mono_time.h
273275
toxcore/net_crypto.c
@@ -434,6 +436,7 @@ unit_test(toxcore bin_pack)
434436
unit_test(toxcore crypto_core)
435437
unit_test(toxcore group_announce)
436438
unit_test(toxcore group_moderation)
439+
unit_test(toxcore mem)
437440
unit_test(toxcore mono_time)
438441
unit_test(toxcore ping_array)
439442
unit_test(toxcore tox)

auto_tests/TCP_test.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ static void test_basic(void)
4848
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
4949
const Random *rng = system_random();
5050
ck_assert(rng != nullptr);
51+
const Network *ns = system_network();
52+
ck_assert(ns != nullptr);
53+
const Memory *mem = system_memory();
54+
ck_assert(mem != nullptr);
55+
5156
Logger *logger = logger_new();
5257
logger_callback_log(logger, print_debug_logger, nullptr, nullptr);
5358

5459
// Attempt to create a new TCP_Server instance.
5560
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
5661
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
5762
crypto_new_keypair(rng, self_public_key, self_secret_key);
58-
const Network *ns = system_network();
59-
TCP_Server *tcp_s = new_TCP_server(logger, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
63+
TCP_Server *tcp_s = new_TCP_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
6064
ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server.");
6165
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS,
6266
"Failed to bind a TCP relay server to all %d attempted ports.", NUM_PORTS);
@@ -302,11 +306,14 @@ static void test_some(void)
302306
ck_assert(rng != nullptr);
303307
Logger *logger = logger_new();
304308
const Network *ns = system_network();
309+
ck_assert(ns != nullptr);
310+
const Memory *mem = system_memory();
311+
ck_assert(mem != nullptr);
305312

306313
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
307314
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
308315
crypto_new_keypair(rng, self_public_key, self_secret_key);
309-
TCP_Server *tcp_s = new_TCP_server(logger, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
316+
TCP_Server *tcp_s = new_TCP_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
310317
ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server");
311318
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports.");
312319

@@ -492,12 +499,15 @@ static void test_client(void)
492499
const Random *rng = system_random();
493500
ck_assert(rng != nullptr);
494501
Logger *logger = logger_new();
502+
const Network *ns = system_network();
503+
ck_assert(ns != nullptr);
504+
const Memory *mem = system_memory();
505+
ck_assert(mem != nullptr);
495506

496507
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
497508
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
498509
crypto_new_keypair(rng, self_public_key, self_secret_key);
499-
const Network *ns = system_network();
500-
TCP_Server *tcp_s = new_TCP_server(logger, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
510+
TCP_Server *tcp_s = new_TCP_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
501511
ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server.");
502512
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind the relay server to all ports.");
503513

@@ -699,22 +709,25 @@ static void test_tcp_connection(void)
699709
const Random *rng = system_random();
700710
ck_assert(rng != nullptr);
701711
const Network *ns = system_network();
712+
ck_assert(ns != nullptr);
713+
const Memory *mem = system_memory();
714+
ck_assert(mem != nullptr);
702715

703716
tcp_data_callback_called = 0;
704717
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
705718
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
706719
crypto_new_keypair(rng, self_public_key, self_secret_key);
707-
TCP_Server *tcp_s = new_TCP_server(logger, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
720+
TCP_Server *tcp_s = new_TCP_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
708721
ck_assert_msg(pk_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key");
709722

710723
TCP_Proxy_Info proxy_info;
711724
proxy_info.proxy_type = TCP_PROXY_NONE;
712725
crypto_new_keypair(rng, self_public_key, self_secret_key);
713-
TCP_Connections *tc_1 = new_tcp_connections(logger, rng, ns, mono_time, self_secret_key, &proxy_info);
726+
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
714727
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
715728

716729
crypto_new_keypair(rng, self_public_key, self_secret_key);
717-
TCP_Connections *tc_2 = new_tcp_connections(logger, rng, ns, mono_time, self_secret_key, &proxy_info);
730+
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
718731
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
719732

720733
IP_Port ip_port_tcp_s;
@@ -808,24 +821,27 @@ static void test_tcp_connection2(void)
808821
const Random *rng = system_random();
809822
ck_assert(rng != nullptr);
810823
const Network *ns = system_network();
824+
ck_assert(ns != nullptr);
825+
const Memory *mem = system_memory();
826+
ck_assert(mem != nullptr);
811827

812828
tcp_oobdata_callback_called = 0;
813829
tcp_data_callback_called = 0;
814830

815831
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
816832
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
817833
crypto_new_keypair(rng, self_public_key, self_secret_key);
818-
TCP_Server *tcp_s = new_TCP_server(logger, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
834+
TCP_Server *tcp_s = new_TCP_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
819835
ck_assert_msg(pk_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key");
820836

821837
TCP_Proxy_Info proxy_info;
822838
proxy_info.proxy_type = TCP_PROXY_NONE;
823839
crypto_new_keypair(rng, self_public_key, self_secret_key);
824-
TCP_Connections *tc_1 = new_tcp_connections(logger, rng, ns, mono_time, self_secret_key, &proxy_info);
840+
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
825841
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
826842

827843
crypto_new_keypair(rng, self_public_key, self_secret_key);
828-
TCP_Connections *tc_2 = new_tcp_connections(logger, rng, ns, mono_time, self_secret_key, &proxy_info);
844+
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
829845
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
830846

831847
IP_Port ip_port_tcp_s;

auto_tests/announce_test.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,17 @@ static void test_store_data(void)
5454
ck_assert(rng != nullptr);
5555
const Network *ns = system_network();
5656
ck_assert(ns != nullptr);
57+
const Memory *mem = system_memory();
58+
ck_assert(mem != nullptr);
59+
5760
Logger *log = logger_new();
5861
ck_assert(log != nullptr);
5962
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
6063
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
6164
Networking_Core *net = new_networking_no_udp(log, ns);
62-
DHT *dht = new_dht(log, rng, ns, mono_time, net, true, true);
65+
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
6366
Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht);
64-
Announcements *announce = new_announcements(log, rng, mono_time, forwarding);
67+
Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding);
6568
ck_assert(announce != nullptr);
6669

6770
/* Just to prevent CI from complaining that set_synch_offset is unused: */

auto_tests/forwarding_test.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ static Forwarding_Subtox *new_forwarding_subtox(bool no_udp, uint32_t *index, ui
116116
ck_assert(rng != nullptr);
117117
const Network *ns = system_network();
118118
ck_assert(ns != nullptr);
119+
const Memory *mem = system_memory();
120+
ck_assert(mem != nullptr);
119121

120122
if (no_udp) {
121123
subtox->net = new_networking_no_udp(subtox->log, ns);
@@ -124,15 +126,15 @@ static Forwarding_Subtox *new_forwarding_subtox(bool no_udp, uint32_t *index, ui
124126
subtox->net = new_networking_ex(subtox->log, ns, &ip, port, port, nullptr);
125127
}
126128

127-
subtox->dht = new_dht(subtox->log, rng, ns, subtox->mono_time, subtox->net, true, true);
129+
subtox->dht = new_dht(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, true, true);
128130

129131
const TCP_Proxy_Info inf = {{{{0}}}};
130-
subtox->c = new_net_crypto(subtox->log, rng, ns, subtox->mono_time, subtox->dht, &inf);
132+
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf);
131133

132134
subtox->forwarding = new_forwarding(subtox->log, rng, subtox->mono_time, subtox->dht);
133135
ck_assert(subtox->forwarding != nullptr);
134136

135-
subtox->announce = new_announcements(subtox->log, rng, subtox->mono_time, subtox->forwarding);
137+
subtox->announce = new_announcements(subtox->log, mem, rng, subtox->mono_time, subtox->forwarding);
136138
ck_assert(subtox->announce != nullptr);
137139

138140
return subtox;

auto_tests/onion_test.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,23 @@ static void test_basic(void)
224224
{
225225
uint32_t index[] = { 1, 2, 3 };
226226
const Network *ns = system_network();
227+
ck_assert(ns != nullptr);
228+
const Memory *mem = system_memory();
229+
ck_assert(mem != nullptr);
230+
const Random *rng = system_random();
231+
ck_assert(rng != nullptr);
227232

228233
Logger *log1 = logger_new();
229234
logger_callback_log(log1, print_debug_logger, nullptr, &index[0]);
230235
Logger *log2 = logger_new();
231236
logger_callback_log(log2, print_debug_logger, nullptr, &index[1]);
232237

233-
const Random *rng = system_random();
234-
ck_assert(rng != nullptr);
235238
Mono_Time *mono_time1 = mono_time_new(nullptr, nullptr);
236239
Mono_Time *mono_time2 = mono_time_new(nullptr, nullptr);
237240

238241
IP ip = get_loopback();
239-
Onion *onion1 = new_onion(log1, mono_time1, rng, new_dht(log1, rng, ns, mono_time1, new_networking(log1, ns, &ip, 36567), true, false));
240-
Onion *onion2 = new_onion(log2, mono_time2, rng, new_dht(log2, rng, ns, mono_time2, new_networking(log2, ns, &ip, 36568), true, false));
242+
Onion *onion1 = new_onion(log1, mono_time1, rng, new_dht(log1, mem, rng, ns, mono_time1, new_networking(log1, ns, &ip, 36567), true, false));
243+
Onion *onion2 = new_onion(log2, mono_time2, rng, new_dht(log2, mem, rng, ns, mono_time2, new_networking(log2, ns, &ip, 36568), true, false));
241244
ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
242245
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2);
243246

@@ -333,7 +336,7 @@ static void test_basic(void)
333336

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

336-
Onion *onion3 = new_onion(log3, mono_time3, rng, new_dht(log3, rng, ns, mono_time3, new_networking(log3, ns, &ip, 36569), true, false));
339+
Onion *onion3 = new_onion(log3, mono_time3, rng, new_dht(log3, mem, rng, ns, mono_time3, new_networking(log3, ns, &ip, 36569), true, false));
337340
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");
338341

339342
random_nonce(rng, nonce);
@@ -400,7 +403,7 @@ typedef struct {
400403
Onion_Client *onion_c;
401404
} Onions;
402405

403-
static Onions *new_onions(const Random *rng, uint16_t port, uint32_t *index)
406+
static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, uint32_t *index)
404407
{
405408
IP ip = get_loopback();
406409
ip.ip.v6.uint8[15] = 1;
@@ -437,7 +440,7 @@ static Onions *new_onions(const Random *rng, uint16_t port, uint32_t *index)
437440
return nullptr;
438441
}
439442

440-
DHT *dht = new_dht(on->log, rng, ns, on->mono_time, net, true, false);
443+
DHT *dht = new_dht(on->log, mem, rng, ns, on->mono_time, net, true, false);
441444

442445
if (!dht) {
443446
kill_networking(net);
@@ -471,7 +474,7 @@ static Onions *new_onions(const Random *rng, uint16_t port, uint32_t *index)
471474
}
472475

473476
TCP_Proxy_Info inf = {{{{0}}}};
474-
on->onion_c = new_onion_client(on->log, rng, on->mono_time, new_net_crypto(on->log, rng, ns, on->mono_time, dht, &inf));
477+
on->onion_c = new_onion_client(on->log, rng, on->mono_time, new_net_crypto(on->log, mem, rng, ns, on->mono_time, dht, &inf));
475478

476479
if (!on->onion_c) {
477480
kill_onion_announce(on->onion_a);
@@ -576,10 +579,12 @@ static void test_announce(void)
576579
Onions *onions[NUM_ONIONS];
577580
const Random *rng = system_random();
578581
ck_assert(rng != nullptr);
582+
const Memory *mem = system_memory();
583+
ck_assert(mem != nullptr);
579584

580585
for (i = 0; i < NUM_ONIONS; ++i) {
581586
index[i] = i + 1;
582-
onions[i] = new_onions(rng, i + 36655, &index[i]);
587+
onions[i] = new_onions(mem, rng, i + 36655, &index[i]);
583588
ck_assert_msg(onions[i] != nullptr, "Failed to create onions. %u", i);
584589
}
585590

other/DHT_bootstrap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ int main(int argc, char *argv[])
148148
const uint16_t start_port = PORT;
149149
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
150150
const Network *ns = system_network();
151-
DHT *dht = new_dht(logger, rng, ns, mono_time, new_networking_ex(logger, ns, &ip, start_port, end_port, nullptr), true, true);
151+
const Memory *mem = system_memory();
152+
DHT *dht = new_dht(logger, mem, rng, ns, mono_time, new_networking_ex(logger, ns, &ip, start_port, end_port, nullptr), true, true);
152153
Onion *onion = new_onion(logger, mono_time, rng, dht);
153154
Forwarding *forwarding = new_forwarding(logger, rng, mono_time, dht);
154155
GC_Announces_List *gc_announces_list = new_gca_list();
@@ -173,7 +174,7 @@ int main(int argc, char *argv[])
173174
#ifdef TCP_RELAY_ENABLED
174175
#define NUM_PORTS 3
175176
uint16_t ports[NUM_PORTS] = {443, 3389, PORT};
176-
TCP_Server *tcp_s = new_TCP_server(logger, rng, ns, ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion, forwarding);
177+
TCP_Server *tcp_s = new_TCP_server(logger, mem, rng, ns, ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion, forwarding);
177178

178179
if (tcp_s == nullptr) {
179180
printf("TCP server failed to initialize.\n");
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
23e142729a72618462018b25c830dbad5b023d0fa3de9ae53d376f43dc46b481 /usr/local/bin/tox-bootstrapd
1+
94b8f80261340bb6debb79ff6cfeed6c238aa304ffee8297791e7153f42b9536 /usr/local/bin/tox-bootstrapd

other/bootstrap_daemon/src/tox-bootstrapd.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,9 @@ int main(int argc, char *argv[])
322322

323323
mono_time_update(mono_time);
324324

325+
const Memory *mem = system_memory();
325326
const Random *rng = system_random();
326-
DHT *const dht = new_dht(logger, rng, ns, mono_time, net, true, enable_lan_discovery);
327+
DHT *const dht = new_dht(logger, mem, rng, ns, mono_time, net, true, enable_lan_discovery);
327328

328329
if (dht == nullptr) {
329330
log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n");
@@ -350,7 +351,7 @@ int main(int argc, char *argv[])
350351
return 1;
351352
}
352353

353-
Announcements *announce = new_announcements(logger, rng, mono_time, forwarding);
354+
Announcements *announce = new_announcements(logger, mem, rng, mono_time, forwarding);
354355

355356
if (announce == nullptr) {
356357
log_write(LOG_LEVEL_ERROR, "Couldn't initialize DHT announcements. Exiting.\n");
@@ -476,7 +477,8 @@ int main(int argc, char *argv[])
476477
return 1;
477478
}
478479

479-
tcp_server = new_TCP_server(logger, rng, ns, enable_ipv6, tcp_relay_port_count, tcp_relay_ports,
480+
tcp_server = new_TCP_server(logger, mem, rng, ns, enable_ipv6,
481+
tcp_relay_port_count, tcp_relay_ports,
480482
dht_get_self_secret_key(dht), onion, forwarding);
481483

482484
free(tcp_relay_ports);

testing/Messenger_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ int main(int argc, char *argv[])
102102
Messenger_Options options = {0};
103103
options.ipv6enabled = ipv6enabled;
104104
Messenger_Error err;
105-
m = new_messenger(mono_time, system_random(), system_network(), &options, &err);
105+
m = new_messenger(mono_time, system_memory(), system_random(), system_network(), &options, &err);
106106

107107
if (!m) {
108108
fprintf(stderr, "Failed to allocate messenger datastructure: %d\n", err);

testing/fuzzing/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cc_library(
2828

2929
cc_fuzz_test(
3030
name = "bootstrap_fuzz_test",
31+
#size = "small",
3132
srcs = ["bootstrap_harness.cc"],
3233
copts = ["-UNDEBUG"],
3334
corpus = ["//tools/toktok-fuzzer/corpus:bootstrap_fuzzer"],
@@ -42,6 +43,7 @@ cc_fuzz_test(
4243

4344
cc_fuzz_test(
4445
name = "e2e_fuzz_test",
46+
#size = "small",
4547
srcs = ["e2e_fuzz_test.cc"],
4648
copts = ["-UNDEBUG"],
4749
corpus = ["//tools/toktok-fuzzer/corpus:e2e_fuzz_test"],
@@ -57,6 +59,7 @@ cc_fuzz_test(
5759

5860
cc_fuzz_test(
5961
name = "toxsave_fuzz_test",
62+
#size = "small",
6063
srcs = ["toxsave_harness.cc"],
6164
copts = ["-UNDEBUG"],
6265
corpus = ["//tools/toktok-fuzzer/corpus:toxsave_fuzzer"],
@@ -89,6 +92,7 @@ fuzzing_binary(
8992

9093
cc_fuzz_test(
9194
name = "protodump_reduce",
95+
#size = "small",
9296
srcs = ["protodump_reduce.cc"],
9397
copts = ["-UNDEBUG"],
9498
deps = [

0 commit comments

Comments
 (0)