Skip to content

Commit d194b00

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 a1e2458 commit d194b00

File tree

21 files changed

+236
-82
lines changed

21 files changed

+236
-82
lines changed

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: 4 additions & 2 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}}}};
130132
subtox->c = new_net_crypto(subtox->log, 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: 13 additions & 8 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);
@@ -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: 2 additions & 1 deletion
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();

other/bootstrap_daemon/src/tox-bootstrapd.c

Lines changed: 3 additions & 2 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");

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 = [

toxcore/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ cc_library(
1515
visibility = ["//c-toxcore:__subpackages__"],
1616
)
1717

18+
cc_library(
19+
name = "mem",
20+
srcs = ["mem.c"],
21+
hdrs = ["mem.h"],
22+
visibility = ["//c-toxcore:__subpackages__"],
23+
deps = [":attributes"],
24+
)
25+
1826
cc_library(
1927
name = "ccompat",
2028
srcs = ["ccompat.c"],
@@ -315,6 +323,7 @@ cc_library(
315323
":ccompat",
316324
":crypto_core",
317325
":logger",
326+
":mem",
318327
":mono_time",
319328
":network",
320329
":ping_array",
@@ -766,6 +775,7 @@ cc_library(
766775
":group",
767776
":group_moderation",
768777
":logger",
778+
":mem",
769779
":mono_time",
770780
":network",
771781
"//c-toxcore/toxencryptsave:defines",

toxcore/DHT.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ struct DHT {
9191
const Logger *log;
9292
const Network *ns;
9393
Mono_Time *mono_time;
94+
const Memory *mem;
9495
const Random *rng;
9596
Networking_Core *net;
9697

@@ -414,12 +415,13 @@ int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_
414415
}
415416
}
416417

417-
int dht_create_packet(const Random *rng, const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
418+
int dht_create_packet(const Memory *mem, const Random *rng,
419+
const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
418420
const uint8_t *shared_key, const uint8_t type,
419421
const uint8_t *plain, size_t plain_length,
420422
uint8_t *packet, size_t length)
421423
{
422-
uint8_t *encrypted = (uint8_t *)malloc(plain_length + CRYPTO_MAC_SIZE);
424+
uint8_t *encrypted = (uint8_t *)mem_malloc(mem, plain_length + CRYPTO_MAC_SIZE);
423425
uint8_t nonce[CRYPTO_NONCE_SIZE];
424426

425427
if (encrypted == nullptr) {
@@ -431,12 +433,12 @@ int dht_create_packet(const Random *rng, const uint8_t public_key[CRYPTO_PUBLIC_
431433
const int encrypted_length = encrypt_data_symmetric(shared_key, nonce, plain, plain_length, encrypted);
432434

433435
if (encrypted_length == -1) {
434-
free(encrypted);
436+
mem_free(mem, encrypted);
435437
return -1;
436438
}
437439

438440
if (length < 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + encrypted_length) {
439-
free(encrypted);
441+
mem_free(mem, encrypted);
440442
return -1;
441443
}
442444

@@ -445,7 +447,7 @@ int dht_create_packet(const Random *rng, const uint8_t public_key[CRYPTO_PUBLIC_
445447
memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE);
446448
memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, encrypted, encrypted_length);
447449

448-
free(encrypted);
450+
mem_free(mem, encrypted);
449451
return 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + encrypted_length;
450452
}
451453

@@ -937,7 +939,8 @@ static bool send_announce_ping(DHT *dht, const uint8_t *public_key, const IP_Por
937939

938940
uint8_t request[1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + sizeof(plain) + CRYPTO_MAC_SIZE];
939941

940-
if (dht_create_packet(dht->rng, dht->self_public_key, shared_key, NET_PACKET_DATA_SEARCH_REQUEST,
942+
if (dht_create_packet(dht->mem, dht->rng,
943+
dht->self_public_key, shared_key, NET_PACKET_DATA_SEARCH_REQUEST,
941944
plain, sizeof(plain), request, sizeof(request)) != sizeof(request)) {
942945
return false;
943946
}
@@ -1392,7 +1395,7 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c
13921395

13931396
const uint8_t *shared_key = dht_get_shared_key_sent(dht, public_key);
13941397

1395-
const int len = dht_create_packet(dht->rng,
1398+
const int len = dht_create_packet(dht->mem, dht->rng,
13961399
dht->self_public_key, shared_key, NET_PACKET_GET_NODES,
13971400
plain, sizeof(plain), data, sizeof(data));
13981401

@@ -1442,7 +1445,7 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t
14421445
const uint32_t crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE;
14431446
VLA(uint8_t, data, 1 + nodes_length + length + crypto_size);
14441447

1445-
const int len = dht_create_packet(dht->rng,
1448+
const int len = dht_create_packet(dht->mem, dht->rng,
14461449
dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6,
14471450
plain, 1 + nodes_length + length, data, SIZEOF_VLA(data));
14481451

@@ -2606,7 +2609,8 @@ static int handle_LANdiscovery(void *object, const IP_Port *source, const uint8_
26062609

26072610
/*----------------------------------------------------------------------------------*/
26082611

2609-
DHT *new_dht(const Logger *log, const Random *rng, const Network *ns, Mono_Time *mono_time, Networking_Core *net,
2612+
DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
2613+
Mono_Time *mono_time, Networking_Core *net,
26102614
bool hole_punching_enabled, bool lan_discovery_enabled)
26112615
{
26122616
if (net == nullptr) {
@@ -2625,6 +2629,7 @@ DHT *new_dht(const Logger *log, const Random *rng, const Network *ns, Mono_Time
26252629
dht->log = log;
26262630
dht->net = net;
26272631
dht->rng = rng;
2632+
dht->mem = mem;
26282633

26292634
dht->hole_punching_enabled = hole_punching_enabled;
26302635
dht->lan_discovery_enabled = lan_discovery_enabled;

toxcore/DHT.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "attributes.h"
1515
#include "crypto_core.h"
1616
#include "logger.h"
17+
#include "mem.h"
1718
#include "mono_time.h"
1819
#include "network.h"
1920
#include "ping_array.h"
@@ -219,7 +220,7 @@ int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_
219220
* @retval -1 on failure.
220221
*/
221222
non_null()
222-
int dht_create_packet(const Random *rng,
223+
int dht_create_packet(const Memory *mem, const Random *rng,
223224
const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
224225
const uint8_t *shared_key, const uint8_t type,
225226
const uint8_t *plain, size_t plain_length,
@@ -494,8 +495,8 @@ int dht_load(DHT *dht, const uint8_t *data, uint32_t length);
494495

495496
/** Initialize DHT. */
496497
non_null()
497-
DHT *new_dht(const Logger *log, const Random *rng, const Network *ns, Mono_Time *mono_time, Networking_Core *net,
498-
bool hole_punching_enabled, bool lan_discovery_enabled);
498+
DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
499+
Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled);
499500

500501
nullable(1)
501502
void kill_dht(DHT *dht);

0 commit comments

Comments
 (0)