Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: Reduce stack frame sizes to below 4096 bytes. #2823

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,18 @@ static void test_basic(void)
}

// Receiving the second response and verifying its validity
uint8_t packet_resp[4096];
const size_t max_packet_size = 4096;
uint8_t *packet_resp = (uint8_t *)malloc(max_packet_size);
ck_assert(packet_resp != nullptr);
int recv_data_len = net_recv(ns, logger, sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, &localhost);
ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE,
"Failed to receive server response to request. %d", recv_data_len);
memcpy(&size, packet_resp, 2);
ck_assert_msg(net_ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE,
"Wrong packet size for request response.");

uint8_t packet_resp_plain[4096];
uint8_t *packet_resp_plain = (uint8_t *)malloc(max_packet_size);
ck_assert(packet_resp_plain != nullptr);
ret = decrypt_data_symmetric(mem, f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain);
ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response.");
increment_nonce(f_nonce_r);
Expand All @@ -183,6 +186,9 @@ static void test_basic(void)
ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection.");
ck_assert_msg(pk_equal(packet_resp_plain + 2, f_public_key), "Server sent the wrong public key.");

free(packet_resp_plain);
free(packet_resp);

// Closing connections.
kill_sock(ns, sock);
kill_tcp_server(tcp_s);
Expand Down Expand Up @@ -337,7 +343,8 @@ static void test_some(void)
do_tcp_server_delay(tcp_s, mono_time, 50);

// Testing response from connection 1
uint8_t data[2048];
const size_t max_packet_size = 4096;
uint8_t *data = (uint8_t *)malloc(max_packet_size);
int len = read_packet_sec_tcp(logger, con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE);
ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len);
ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]);
Expand All @@ -351,7 +358,7 @@ static void test_some(void)
ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key.");
ck_assert_msg(pk_equal(data + 2, con1->public_key), "Key in response packet wrong.");

uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet????
const uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet????

write_packet_tcp_test_connection(logger, con3, test_packet, sizeof(test_packet));
write_packet_tcp_test_connection(logger, con3, test_packet, sizeof(test_packet));
Expand Down Expand Up @@ -406,6 +413,8 @@ static void test_some(void)
ck_assert_msg(data[0] == TCP_PACKET_PONG, "wrong packet id %u", data[0]);
ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data");

free(data);

// Kill off the connections
kill_tcp_server(tcp_s);
kill_tcp_con(con1);
Expand Down
25 changes: 18 additions & 7 deletions auto_tests/save_compatibility_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ static bool is_little_endian(void)
// cppcheck-suppress constParameter
int main(int argc, char *argv[])
{
char base_path[4096] = {0};
const size_t base_path_size = 4096;
char *base_path = (char *)malloc(base_path_size);
ck_assert(base_path != nullptr);
memset(base_path, 0, 4096);

if (argc <= 1) {
const char *srcdir = getenv("srcdir");
Expand All @@ -154,21 +157,29 @@ int main(int argc, char *argv[])
srcdir = ".";
}

snprintf(base_path, sizeof(base_path), "%s", srcdir);
snprintf(base_path, base_path_size, "%s", srcdir);
} else {
snprintf(base_path, sizeof(base_path), "%s", argv[1]);
snprintf(base_path, base_path_size, "%s", argv[1]);
base_path[strrchr(base_path, '/') - base_path] = '\0';
}

if (is_little_endian()) {
char save_path[4096 + sizeof(LOADED_SAVE_FILE_LITTLE)];
snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE_LITTLE);
const size_t save_path_size = 4096 + sizeof(LOADED_SAVE_FILE_LITTLE);
char *save_path = (char *)malloc(save_path_size);
ck_assert(save_path != nullptr);
snprintf(save_path, save_path_size, "%s/%s", base_path, LOADED_SAVE_FILE_LITTLE);
test_save_compatibility(save_path);
free(save_path);
} else {
char save_path[4096 + sizeof(LOADED_SAVE_FILE_BIG)];
snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE_BIG);
const size_t save_path_size = 4096 + sizeof(LOADED_SAVE_FILE_BIG);
char *save_path = (char *)malloc(save_path_size);
ck_assert(save_path != nullptr);
snprintf(save_path, save_path_size, "%s/%s", base_path, LOADED_SAVE_FILE_BIG);
test_save_compatibility(save_path);
free(save_path);
}

free(base_path);

return 0;
}
Loading