Skip to content

Commit

Permalink
cleanup: Reduce stack frame sizes to below 4096 bytes.
Browse files Browse the repository at this point in the history
This is currently only detectable in tests, because the main code uses
VLAs where stack frame size is unknown at compile time.
  • Loading branch information
iphydf committed Jan 5, 2025
1 parent fbe78f1 commit 79ef301
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
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;
}

0 comments on commit 79ef301

Please sign in to comment.