Skip to content

Commit 7603170

Browse files
committed
refactor: Use tox memory in group connection allocations.
1 parent 5bd8a85 commit 7603170

File tree

4 files changed

+57
-57
lines changed

4 files changed

+57
-57
lines changed

.cirrus.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bazel-msan_task:
3939
timeout_in: 10m
4040
container:
4141
image: toxchat/toktok-stack:latest-msan
42-
cpu: 2
42+
cpu: 4
4343
memory: 2G
4444
configure_script:
4545
- git submodule update --init --recursive

toxcore/group_chats.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5330,7 +5330,7 @@ static int handle_gc_message_ack(const GC_Chat *chat, GC_Connection *gconn, cons
53305330
const Group_Message_Ack_Type type = (Group_Message_Ack_Type) data[0];
53315331

53325332
if (type == GR_ACK_RECV) {
5333-
if (!gcc_handle_ack(chat->log, gconn, message_id)) {
5333+
if (!gcc_handle_ack(chat->log, chat->mem, gconn, message_id)) {
53345334
return -2;
53355335
}
53365336

@@ -6207,8 +6207,7 @@ static bool handle_gc_lossless_packet(const GC_Session *c, GC_Chat *chat, const
62076207
return false;
62086208
}
62096209

6210-
const int lossless_ret = gcc_handle_received_message(chat->log, chat->mono_time, gconn, data, (uint16_t) len,
6211-
packet_type, message_id, direct_conn);
6210+
const int lossless_ret = gcc_handle_received_message(chat->log, chat->mem, chat->mono_time, gconn, data, (uint16_t) len, packet_type, message_id, direct_conn);
62126211

62136212
if (packet_type == GP_INVITE_REQUEST && !gconn->handshaked) { // Both peers sent request at same time
62146213
mem_delete(chat->mem, data);
@@ -6720,7 +6719,7 @@ static bool peer_delete(const GC_Session *c, GC_Chat *chat, uint32_t peer_number
67206719
assert(nick_length <= MAX_GC_NICK_SIZE);
67216720
memcpy(nick, peer->nick, nick_length);
67226721

6723-
gcc_peer_cleanup(&peer->gconn);
6722+
gcc_peer_cleanup(chat->mem, &peer->gconn);
67246723

67256724
--chat->numpeers;
67266725

toxcore/group_connection.c

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <assert.h>
1313
#include <stdint.h>
14-
#include <stdlib.h>
1514
#include <string.h>
1615

1716
#include "DHT.h"
@@ -22,6 +21,7 @@
2221
#include "group_chats.h"
2322
#include "group_common.h"
2423
#include "logger.h"
24+
#include "mem.h"
2525
#include "mono_time.h"
2626
#include "network.h"
2727
#include "util.h"
@@ -39,9 +39,9 @@ static bool array_entry_is_empty(const GC_Message_Array_Entry *array_entry)
3939

4040
/** @brief Clears an array entry. */
4141
non_null()
42-
static void clear_array_entry(GC_Message_Array_Entry *const array_entry)
42+
static void clear_array_entry(const Memory *mem, GC_Message_Array_Entry *const array_entry)
4343
{
44-
free(array_entry->data);
44+
mem_delete(mem, array_entry->data);
4545

4646
*array_entry = (GC_Message_Array_Entry) {
4747
nullptr
@@ -54,14 +54,14 @@ static void clear_array_entry(GC_Message_Array_Entry *const array_entry)
5454
* to `start_id`.
5555
*/
5656
non_null()
57-
static void clear_send_queue_id_range(GC_Connection *gconn, uint64_t start_id, uint64_t end_id)
57+
static void clear_send_queue_id_range(const Memory *mem, GC_Connection *gconn, uint64_t start_id, uint64_t end_id)
5858
{
5959
const uint16_t start_idx = gcc_get_array_index(start_id);
6060
const uint16_t end_idx = gcc_get_array_index(end_id);
6161

6262
for (uint16_t i = start_idx; i != end_idx; i = (i + 1) % GCC_BUFFER_SIZE) {
6363
GC_Message_Array_Entry *entry = &gconn->send_array[i];
64-
clear_array_entry(entry);
64+
clear_array_entry(mem, entry);
6565
}
6666

6767
gconn->send_message_id = start_id;
@@ -90,8 +90,8 @@ void gcc_set_recv_message_id(GC_Connection *gconn, uint64_t id)
9090
*
9191
* Return true on success.
9292
*/
93-
non_null(1, 2, 3) nullable(4)
94-
static bool create_array_entry(const Logger *log, const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry,
93+
non_null(1, 2, 3, 4) nullable(5)
94+
static bool create_array_entry(const Logger *log, const Memory *mem, const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry,
9595
const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id)
9696
{
9797
if (!array_entry_is_empty(array_entry)) {
@@ -109,7 +109,7 @@ static bool create_array_entry(const Logger *log, const Mono_Time *mono_time, GC
109109
return false;
110110
}
111111

112-
uint8_t *entry_data = (uint8_t *)malloc(length);
112+
uint8_t *entry_data = (uint8_t *)mem_balloc(mem, length);
113113

114114
if (entry_data == nullptr) {
115115
return false;
@@ -134,9 +134,9 @@ static bool create_array_entry(const Logger *log, const Mono_Time *mono_time, GC
134134
*
135135
* Returns true and increments gconn's send_message_id on success.
136136
*/
137-
non_null(1, 2, 3) nullable(4)
138-
static bool add_to_send_array(const Logger *log, const Mono_Time *mono_time, GC_Connection *gconn, const uint8_t *data,
139-
uint16_t length, uint8_t packet_type)
137+
non_null(1, 2, 3, 4) nullable(5)
138+
static bool add_to_send_array(const Logger *log, const Memory *mem, const Mono_Time *mono_time, GC_Connection *gconn,
139+
const uint8_t *data, uint16_t length, uint8_t packet_type)
140140
{
141141
/* check if send_array is full */
142142
if ((gconn->send_message_id % GCC_BUFFER_SIZE) == (uint16_t)(gconn->send_array_start - 1)) {
@@ -147,7 +147,7 @@ static bool add_to_send_array(const Logger *log, const Mono_Time *mono_time, GC_
147147
const uint16_t idx = gcc_get_array_index(gconn->send_message_id);
148148
GC_Message_Array_Entry *array_entry = &gconn->send_array[idx];
149149

150-
if (!create_array_entry(log, mono_time, array_entry, data, length, packet_type, gconn->send_message_id)) {
150+
if (!create_array_entry(log, mem, mono_time, array_entry, data, length, packet_type, gconn->send_message_id)) {
151151
return false;
152152
}
153153

@@ -161,7 +161,7 @@ int gcc_send_lossless_packet(const GC_Chat *chat, GC_Connection *gconn, const ui
161161
{
162162
const uint64_t message_id = gconn->send_message_id;
163163

164-
if (!add_to_send_array(chat->log, chat->mono_time, gconn, data, length, packet_type)) {
164+
if (!add_to_send_array(chat->log, chat->mem, chat->mono_time, gconn, data, length, packet_type)) {
165165
LOGGER_WARNING(chat->log, "Failed to add payload to send array: (type: 0x%02x, length: %d)", packet_type, length);
166166
return -1;
167167
}
@@ -172,7 +172,7 @@ int gcc_send_lossless_packet(const GC_Chat *chat, GC_Connection *gconn, const ui
172172
if (gcc_encrypt_and_send_lossless_packet(chat, gconn, data, length, message_id, packet_type) == -1) {
173173
const uint16_t idx = gcc_get_array_index(message_id);
174174
GC_Message_Array_Entry *array_entry = &gconn->send_array[idx];
175-
clear_array_entry(array_entry);
175+
clear_array_entry(chat->mem, array_entry);
176176
gconn->send_message_id = message_id;
177177
LOGGER_ERROR(chat->log, "Failed to encrypt payload: (type: 0x%02x, length: %d)", packet_type, length);
178178
return -2;
@@ -196,7 +196,7 @@ bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gcon
196196
chunk[0] = packet_type;
197197
memcpy(chunk + 1, data, MAX_GC_PACKET_CHUNK_SIZE - 1);
198198

199-
if (!add_to_send_array(chat->log, chat->mono_time, gconn, chunk, MAX_GC_PACKET_CHUNK_SIZE, GP_FRAGMENT)) {
199+
if (!add_to_send_array(chat->log, chat->mem, chat->mono_time, gconn, chunk, MAX_GC_PACKET_CHUNK_SIZE, GP_FRAGMENT)) {
200200
return false;
201201
}
202202

@@ -209,15 +209,15 @@ bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gcon
209209
memcpy(chunk, data + processed, chunk_len);
210210
processed += chunk_len;
211211

212-
if (!add_to_send_array(chat->log, chat->mono_time, gconn, chunk, chunk_len, GP_FRAGMENT)) {
213-
clear_send_queue_id_range(gconn, start_id, gconn->send_message_id);
212+
if (!add_to_send_array(chat->log, chat->mem, chat->mono_time, gconn, chunk, chunk_len, GP_FRAGMENT)) {
213+
clear_send_queue_id_range(chat->mem, gconn, start_id, gconn->send_message_id);
214214
return false;
215215
}
216216
}
217217

218218
// empty packet signals the end of the sequence
219-
if (!add_to_send_array(chat->log, chat->mono_time, gconn, nullptr, 0, GP_FRAGMENT)) {
220-
clear_send_queue_id_range(gconn, start_id, gconn->send_message_id);
219+
if (!add_to_send_array(chat->log, chat->mem, chat->mono_time, gconn, nullptr, 0, GP_FRAGMENT)) {
220+
clear_send_queue_id_range(chat->mem, gconn, start_id, gconn->send_message_id);
221221
return false;
222222
}
223223

@@ -241,7 +241,7 @@ bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gcon
241241
return true;
242242
}
243243

244-
bool gcc_handle_ack(const Logger *log, GC_Connection *gconn, uint64_t message_id)
244+
bool gcc_handle_ack(const Logger *log, const Memory *mem, GC_Connection *gconn, uint64_t message_id)
245245
{
246246
uint16_t idx = gcc_get_array_index(message_id);
247247
GC_Message_Array_Entry *array_entry = &gconn->send_array[idx];
@@ -255,7 +255,7 @@ bool gcc_handle_ack(const Logger *log, GC_Connection *gconn, uint64_t message_id
255255
return false;
256256
}
257257

258-
clear_array_entry(array_entry);
258+
clear_array_entry(mem, array_entry);
259259

260260
/* Put send_array_start in proper position */
261261
if (idx == gconn->send_array_start) {
@@ -336,15 +336,15 @@ int gcc_save_tcp_relay(const Random *rng, GC_Connection *gconn, const Node_forma
336336
*
337337
* Return true on success.
338338
*/
339-
non_null(1, 2, 3) nullable(4)
340-
static bool store_in_recv_array(const Logger *log, const Mono_Time *mono_time, GC_Connection *gconn,
341-
const uint8_t *data,
339+
non_null(1, 2, 3, 4) nullable(5)
340+
static bool store_in_recv_array(const Logger *log, const Memory *mem, const Mono_Time *mono_time,
341+
GC_Connection *gconn, const uint8_t *data,
342342
uint16_t length, uint8_t packet_type, uint64_t message_id)
343343
{
344344
const uint16_t idx = gcc_get_array_index(message_id);
345345
GC_Message_Array_Entry *ary_entry = &gconn->recv_array[idx];
346346

347-
return create_array_entry(log, mono_time, ary_entry, data, length, packet_type, message_id);
347+
return create_array_entry(log, mem, mono_time, ary_entry, data, length, packet_type, message_id);
348348
}
349349

350350
/**
@@ -358,8 +358,8 @@ static bool store_in_recv_array(const Logger *log, const Mono_Time *mono_time, G
358358
* Return the length of the fully reassembled packet on success.
359359
* Return 0 on failure.
360360
*/
361-
non_null(1, 3) nullable(2)
362-
static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8_t **payload, uint64_t message_id)
361+
non_null(1, 2, 4) nullable(3)
362+
static uint16_t reassemble_packet(const Logger *log, const Memory *mem, GC_Connection *gconn, uint8_t **payload, uint64_t message_id)
363363
{
364364
uint16_t end_idx = gcc_get_array_index(message_id - 1);
365365
uint16_t start_idx = end_idx;
@@ -395,7 +395,7 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8
395395
return 0;
396396
}
397397

398-
uint8_t *tmp_payload = (uint8_t *)malloc(packet_length);
398+
uint8_t *tmp_payload = (uint8_t *)mem_balloc(mem, packet_length);
399399

400400
if (tmp_payload == nullptr) {
401401
LOGGER_ERROR(log, "Failed to allocate %u bytes for payload buffer", packet_length);
@@ -414,7 +414,7 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8
414414
memcpy(tmp_payload + processed, entry->data, entry->data_length);
415415
processed += entry->data_length;
416416

417-
clear_array_entry(entry);
417+
clear_array_entry(mem, entry);
418418
}
419419

420420
assert(*payload == nullptr);
@@ -428,7 +428,7 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer
428428
uint64_t message_id, void *userdata)
429429
{
430430
if (length > 0) {
431-
if (!store_in_recv_array(chat->log, chat->mono_time, gconn, chunk, length, packet_type, message_id)) {
431+
if (!store_in_recv_array(chat->log, chat->mem, chat->mono_time, gconn, chunk, length, packet_type, message_id)) {
432432
return -1;
433433
}
434434

@@ -442,15 +442,15 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer
442442
memcpy(sender_pk, get_enc_key(&gconn->addr.public_key), ENC_PUBLIC_KEY_SIZE);
443443

444444
uint8_t *payload = nullptr;
445-
const uint16_t processed_len = reassemble_packet(chat->log, gconn, &payload, message_id);
445+
const uint16_t processed_len = reassemble_packet(chat->log, chat->mem, gconn, &payload, message_id);
446446

447447
if (processed_len == 0) {
448-
free(payload);
448+
mem_delete(chat->mem, payload);
449449
return -1;
450450
}
451451

452452
if (!handle_gc_lossless_helper(c, chat, peer_number, payload + 1, processed_len - 1, payload[0], userdata)) {
453-
free(payload);
453+
mem_delete(chat->mem, payload);
454454
return -1;
455455
}
456456

@@ -459,19 +459,19 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer
459459
gconn = get_gc_connection(chat, peer_number);
460460

461461
if (gconn == nullptr) {
462-
free(payload);
462+
mem_delete(chat->mem, payload);
463463
return 0;
464464
}
465465

466466
gcc_set_recv_message_id(gconn, gconn->received_message_id + 1);
467467
gconn->last_chunk_id = 0;
468468

469-
free(payload);
469+
mem_delete(chat->mem, payload);
470470

471471
return 0;
472472
}
473473

474-
int gcc_handle_received_message(const Logger *log, const Mono_Time *mono_time, GC_Connection *gconn,
474+
int gcc_handle_received_message(const Logger *log, const Memory *mem, const Mono_Time *mono_time, GC_Connection *gconn,
475475
const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id,
476476
bool direct_conn)
477477
{
@@ -490,7 +490,7 @@ int gcc_handle_received_message(const Logger *log, const Mono_Time *mono_time, G
490490

491491
/* we're missing an older message from this peer so we store it in recv_array */
492492
if (message_id > gconn->received_message_id + 1) {
493-
if (!store_in_recv_array(log, mono_time, gconn, data, length, packet_type, message_id)) {
493+
if (!store_in_recv_array(log, mem, mono_time, gconn, data, length, packet_type, message_id)) {
494494
return -1;
495495
}
496496

@@ -522,7 +522,7 @@ static bool process_recv_array_entry(const GC_Session *c, GC_Chat *chat, GC_Conn
522522
peer_number = get_peer_number_of_enc_pk(chat, sender_pk, false);
523523
gconn = get_gc_connection(chat, peer_number);
524524

525-
clear_array_entry(array_entry);
525+
clear_array_entry(chat->mem, array_entry);
526526

527527
if (gconn == nullptr) {
528528
return true;
@@ -621,7 +621,7 @@ int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connectio
621621
uint16_t length, uint64_t message_id, uint8_t packet_type)
622622
{
623623
const uint16_t packet_size = gc_get_wrapped_packet_size(length, NET_PACKET_GC_LOSSLESS);
624-
uint8_t *packet = (uint8_t *)malloc(packet_size);
624+
uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_size);
625625

626626
if (packet == nullptr) {
627627
LOGGER_ERROR(chat->log, "Failed to allocate memory for packet buffer");
@@ -634,17 +634,17 @@ int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connectio
634634

635635
if (enc_len < 0) {
636636
LOGGER_ERROR(chat->log, "Failed to wrap packet (type: 0x%02x, error: %d)", packet_type, enc_len);
637-
free(packet);
637+
mem_delete(chat->mem, packet);
638638
return -1;
639639
}
640640

641641
if (!gcc_send_packet(chat, gconn, packet, (uint16_t)enc_len)) {
642642
LOGGER_DEBUG(chat->log, "Failed to send packet (type: 0x%02x, enc_len: %d)", packet_type, enc_len);
643-
free(packet);
643+
mem_delete(chat->mem, packet);
644644
return -2;
645645
}
646646

647-
free(packet);
647+
mem_delete(chat->mem, packet);
648648

649649
return 0;
650650
}
@@ -686,15 +686,15 @@ void gcc_mark_for_deletion(GC_Connection *gconn, TCP_Connections *tcp_conn, Grou
686686
}
687687
}
688688

689-
void gcc_peer_cleanup(GC_Connection *gconn)
689+
void gcc_peer_cleanup(const Memory *mem, GC_Connection *gconn)
690690
{
691691
for (size_t i = 0; i < GCC_BUFFER_SIZE; ++i) {
692-
free(gconn->send_array[i].data);
693-
free(gconn->recv_array[i].data);
692+
mem_delete(mem, gconn->send_array[i].data);
693+
mem_delete(mem, gconn->recv_array[i].data);
694694
}
695695

696-
free(gconn->recv_array);
697-
free(gconn->send_array);
696+
mem_delete(mem, gconn->recv_array);
697+
mem_delete(mem, gconn->send_array);
698698

699699
crypto_memunlock(gconn->session_secret_key, sizeof(gconn->session_secret_key));
700700
crypto_memunlock(gconn->session_shared_key, sizeof(gconn->session_shared_key));
@@ -707,6 +707,6 @@ void gcc_cleanup(const GC_Chat *chat)
707707
GC_Connection *gconn = get_gc_connection(chat, i);
708708
assert(gconn != nullptr);
709709

710-
gcc_peer_cleanup(gconn);
710+
gcc_peer_cleanup(chat->mem, gconn);
711711
}
712712
}

0 commit comments

Comments
 (0)