Skip to content

Commit 5e7a651

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 5e7a651

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1138
-607
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: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};
4545

4646
static void test_basic(void)
4747
{
48-
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
4948
const Random *rng = system_random();
5049
ck_assert(rng != nullptr);
50+
const Network *ns = system_network();
51+
ck_assert(ns != nullptr);
52+
const Memory *mem = system_memory();
53+
ck_assert(mem != nullptr);
54+
55+
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
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);
@@ -70,7 +74,7 @@ static void test_basic(void)
7074
for (uint8_t i = 0; i < NUM_PORTS; i++) {
7175
sock = net_socket(ns, net_family_ipv6(), TOX_SOCK_STREAM, TOX_PROTO_TCP);
7276
localhost.port = net_htons(ports[i]);
73-
bool ret = net_connect(logger, sock, &localhost);
77+
bool ret = net_connect(mem, logger, sock, &localhost);
7478
ck_assert_msg(ret, "Failed to connect to created TCP relay server on port %d (%d).", ports[i], errno);
7579

7680
// Leave open one connection for the next test.
@@ -184,30 +188,32 @@ static void test_basic(void)
184188
kill_TCP_server(tcp_s);
185189

186190
logger_kill(logger);
187-
mono_time_free(mono_time);
191+
mono_time_free(mem, mono_time);
188192
}
189193

190194
struct sec_TCP_con {
191195
Socket sock;
192196
const Network *ns;
197+
const Memory *mem;
193198
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
194199
uint8_t recv_nonce[CRYPTO_NONCE_SIZE];
195200
uint8_t sent_nonce[CRYPTO_NONCE_SIZE];
196201
uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
197202
};
198203

199-
static struct sec_TCP_con *new_TCP_con(const Logger *logger, const Random *rng, const Network *ns, TCP_Server *tcp_s, Mono_Time *mono_time)
204+
static struct sec_TCP_con *new_TCP_con(const Logger *logger, const Memory *mem, const Random *rng, const Network *ns, TCP_Server *tcp_s, Mono_Time *mono_time)
200205
{
201206
struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con));
202207
ck_assert(sec_c != nullptr);
203208
sec_c->ns = ns;
209+
sec_c->mem = mem;
204210
Socket sock = net_socket(ns, net_family_ipv6(), TOX_SOCK_STREAM, TOX_PROTO_TCP);
205211

206212
IP_Port localhost;
207213
localhost.ip = get_loopback();
208214
localhost.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
209215

210-
bool ok = net_connect(logger, sock, &localhost);
216+
bool ok = net_connect(mem, logger, sock, &localhost);
211217
ck_assert_msg(ok, "Failed to connect to the test TCP relay server.");
212218

213219
uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -297,22 +303,26 @@ static int read_packet_sec_TCP(const Logger *logger, struct sec_TCP_con *con, ui
297303

298304
static void test_some(void)
299305
{
300-
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
301306
const Random *rng = system_random();
302307
ck_assert(rng != nullptr);
303-
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);
312+
313+
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
314+
Logger *logger = logger_new();
305315

306316
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
307317
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
308318
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);
319+
TCP_Server *tcp_s = new_TCP_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
310320
ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server");
311321
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports.");
312322

313-
struct sec_TCP_con *con1 = new_TCP_con(logger, rng, ns, tcp_s, mono_time);
314-
struct sec_TCP_con *con2 = new_TCP_con(logger, rng, ns, tcp_s, mono_time);
315-
struct sec_TCP_con *con3 = new_TCP_con(logger, rng, ns, tcp_s, mono_time);
323+
struct sec_TCP_con *con1 = new_TCP_con(logger, mem, rng, ns, tcp_s, mono_time);
324+
struct sec_TCP_con *con2 = new_TCP_con(logger, mem, rng, ns, tcp_s, mono_time);
325+
struct sec_TCP_con *con3 = new_TCP_con(logger, mem, rng, ns, tcp_s, mono_time);
316326

317327
uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE];
318328
requ_p[0] = TCP_PACKET_ROUTING_REQUEST;
@@ -402,7 +412,7 @@ static void test_some(void)
402412
kill_TCP_con(con3);
403413

404414
logger_kill(logger);
405-
mono_time_free(mono_time);
415+
mono_time_free(mem, mono_time);
406416
}
407417

408418
static int response_callback_good;
@@ -488,16 +498,20 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint
488498

489499
static void test_client(void)
490500
{
491-
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
492501
const Random *rng = system_random();
493502
ck_assert(rng != nullptr);
503+
const Network *ns = system_network();
504+
ck_assert(ns != nullptr);
505+
const Memory *mem = system_memory();
506+
ck_assert(mem != nullptr);
507+
494508
Logger *logger = logger_new();
509+
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
495510

496511
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
497512
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
498513
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);
514+
TCP_Server *tcp_s = new_TCP_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
501515
ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server.");
502516
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind the relay server to all ports.");
503517

@@ -509,8 +523,7 @@ static void test_client(void)
509523
ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
510524
ip_port_tcp_s.ip = get_loopback();
511525

512-
TCP_Client_Connection *conn = new_TCP_connection(logger, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f_public_key,
513-
f_secret_key, nullptr);
526+
TCP_Client_Connection *conn = new_TCP_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr);
514527
do_TCP_connection(logger, mono_time, conn, nullptr);
515528
c_sleep(50);
516529

@@ -544,7 +557,7 @@ static void test_client(void)
544557
uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE];
545558
crypto_new_keypair(rng, f2_public_key, f2_secret_key);
546559
ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
547-
TCP_Client_Connection *conn2 = new_TCP_connection(logger, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f2_public_key,
560+
TCP_Client_Connection *conn2 = new_TCP_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f2_public_key,
548561
f2_secret_key, nullptr);
549562

550563
// The client should call this function (defined earlier) during the routing process.
@@ -613,17 +626,21 @@ static void test_client(void)
613626
kill_TCP_connection(conn2);
614627

615628
logger_kill(logger);
616-
mono_time_free(mono_time);
629+
mono_time_free(mem, mono_time);
617630
}
618631

619632
// Test how the client handles servers that don't respond.
620633
static void test_client_invalid(void)
621634
{
622-
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
623635
const Random *rng = system_random();
624636
ck_assert(rng != nullptr);
625-
Logger *logger = logger_new();
626637
const Network *ns = system_network();
638+
ck_assert(ns != nullptr);
639+
const Memory *mem = system_memory();
640+
ck_assert(mem != nullptr);
641+
642+
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
643+
Logger *logger = logger_new();
627644

628645
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
629646
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -636,7 +653,7 @@ static void test_client_invalid(void)
636653

637654
ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
638655
ip_port_tcp_s.ip = get_loopback();
639-
TCP_Client_Connection *conn = new_TCP_connection(logger, mono_time, rng, ns, &ip_port_tcp_s,
656+
TCP_Client_Connection *conn = new_TCP_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s,
640657
self_public_key, f_public_key, f_secret_key, nullptr);
641658

642659
// Run the client's main loop but not the server.
@@ -663,7 +680,7 @@ static void test_client_invalid(void)
663680
kill_TCP_connection(conn);
664681

665682
logger_kill(logger);
666-
mono_time_free(mono_time);
683+
mono_time_free(mem, mono_time);
667684
}
668685

669686
#include "../toxcore/TCP_connection.h"
@@ -694,27 +711,31 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t
694711

695712
static void test_tcp_connection(void)
696713
{
697-
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
698-
Logger *logger = logger_new();
699714
const Random *rng = system_random();
700715
ck_assert(rng != nullptr);
701716
const Network *ns = system_network();
717+
ck_assert(ns != nullptr);
718+
const Memory *mem = system_memory();
719+
ck_assert(mem != nullptr);
720+
721+
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
722+
Logger *logger = logger_new();
702723

703724
tcp_data_callback_called = 0;
704725
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
705726
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
706727
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);
728+
TCP_Server *tcp_s = new_TCP_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
708729
ck_assert_msg(pk_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key");
709730

710731
TCP_Proxy_Info proxy_info;
711732
proxy_info.proxy_type = TCP_PROXY_NONE;
712733
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);
734+
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
714735
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
715736

716737
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);
738+
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
718739
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
719740

720741
IP_Port ip_port_tcp_s;
@@ -777,7 +798,7 @@ static void test_tcp_connection(void)
777798
kill_tcp_connections(tc_2);
778799

779800
logger_kill(logger);
780-
mono_time_free(mono_time);
801+
mono_time_free(mem, mono_time);
781802
}
782803

783804
static bool tcp_oobdata_callback_called;
@@ -803,29 +824,33 @@ static int tcp_oobdata_callback(void *object, const uint8_t *public_key, unsigne
803824

804825
static void test_tcp_connection2(void)
805826
{
806-
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
807-
Logger *logger = logger_new();
808827
const Random *rng = system_random();
809828
ck_assert(rng != nullptr);
810829
const Network *ns = system_network();
830+
ck_assert(ns != nullptr);
831+
const Memory *mem = system_memory();
832+
ck_assert(mem != nullptr);
833+
834+
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
835+
Logger *logger = logger_new();
811836

812837
tcp_oobdata_callback_called = 0;
813838
tcp_data_callback_called = 0;
814839

815840
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
816841
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
817842
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);
843+
TCP_Server *tcp_s = new_TCP_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
819844
ck_assert_msg(pk_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key");
820845

821846
TCP_Proxy_Info proxy_info;
822847
proxy_info.proxy_type = TCP_PROXY_NONE;
823848
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);
849+
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
825850
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
826851

827852
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);
853+
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
829854
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
830855

831856
IP_Port ip_port_tcp_s;
@@ -881,7 +906,7 @@ static void test_tcp_connection2(void)
881906
kill_tcp_connections(tc_2);
882907

883908
logger_kill(logger);
884-
mono_time_free(mono_time);
909+
mono_time_free(mem, mono_time);
885910
}
886911

887912
static void TCP_suite(void)

auto_tests/announce_test.c

Lines changed: 8 additions & 5 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);
60-
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
61-
Networking_Core *net = new_networking_no_udp(log, ns);
62-
DHT *dht = new_dht(log, rng, ns, mono_time, net, true, true);
63+
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
64+
Networking_Core *net = new_networking_no_udp(log, mem, ns);
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: */
@@ -103,7 +106,7 @@ static void test_store_data(void)
103106
kill_forwarding(forwarding);
104107
kill_dht(dht);
105108
kill_networking(net);
106-
mono_time_free(mono_time);
109+
mono_time_free(mem, mono_time);
107110
logger_kill(log);
108111
}
109112

0 commit comments

Comments
 (0)