Skip to content

Commit

Permalink
Add a monolith_test that includes all toxcore sources.
Browse files Browse the repository at this point in the history
This requires that every symbol, even if static (file-scope), is unique.
The idea is that we can easily run "whole" program static analysis on
programs that include monolith.h ("whole" is in quotes, as we don't
include dependencies like libsodium in this static analysis).
  • Loading branch information
iphydf committed Jun 4, 2017
1 parent 03f99bd commit 575b11a
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 111 deletions.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ option(BUILD_TOXAV "Whether to build the tox AV library" ON)

include(Dependencies)

if (BUILD_TOXAV)
if(BUILD_TOXAV)
if(NOT OPUS_FOUND)
message(SEND_ERROR "Option BUILD_TOXAV is enabled but required library OPUS was not found.")
endif()
Expand Down Expand Up @@ -395,6 +395,15 @@ function(auto_test target)
endif()
endfunction()

if(BUILD_TOXAV)
add_c_executable(auto_monolith_test auto_tests/monolith_test.c)
target_link_modules(auto_monolith_test
${LIBSODIUM_LIBRARIES}
${OPUS_LIBRARIES}
${VPX_LIBRARIES})
add_test(NAME monolith COMMAND auto_monolith_test)
endif()

auto_test(TCP)
auto_test(conference)
auto_test(crypto)
Expand Down
15 changes: 15 additions & 0 deletions auto_tests/monolith_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* Auto Tests: One instance.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define _DARWIN_C_SOURCE
#define _XOPEN_SOURCE 600
#include "../other/monolith.h"

int main(int argc, char *argv[])
{
return 0;
}
36 changes: 36 additions & 0 deletions other/monolith.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "../toxcore/tox.c"

#include "../toxcore/crypto_core.c"
#include "../toxcore/crypto_core_mem.c"
#include "../toxcore/DHT.c"
#include "../toxcore/friend_connection.c"
#include "../toxcore/friend_requests.c"
#include "../toxcore/group.c"
#include "../toxcore/LAN_discovery.c"
#include "../toxcore/list.c"
#include "../toxcore/logger.c"
#include "../toxcore/Messenger.c"
#include "../toxcore/net_crypto.c"
#include "../toxcore/network.c"
#include "../toxcore/onion_announce.c"
#include "../toxcore/onion.c"
#include "../toxcore/onion_client.c"
#include "../toxcore/ping_array.c"
#include "../toxcore/ping.c"
#include "../toxcore/TCP_client.c"
#include "../toxcore/TCP_connection.c"
#include "../toxcore/TCP_server.c"
#include "../toxcore/tox_api.c"
#include "../toxcore/util.c"

#include "../toxav/audio.c"
#include "../toxav/bwcontroller.c"
#include "../toxav/groupav.c"
#include "../toxav/msi.c"
#include "../toxav/ring_buffer.c"
#include "../toxav/rtp.c"
#include "../toxav/toxav.c"
#include "../toxav/toxav_old.c"
#include "../toxav/video.c"

#include "../toxencryptsave/toxencryptsave.c"
6 changes: 3 additions & 3 deletions toxav/toxav_old.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
*/
int toxav_add_av_groupchat(struct Tox *tox, void (*audio_callback)(void *, int, int, const int16_t *, unsigned int,
int toxav_add_av_groupchat(Tox *tox, void (*audio_callback)(void *, int, int, const int16_t *, unsigned int,
uint8_t, unsigned int, void *), void *userdata)
{
Messenger *m = (Messenger *)tox;
Expand All @@ -54,7 +54,7 @@ int toxav_add_av_groupchat(struct Tox *tox, void (*audio_callback)(void *, int,
*
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
*/
int toxav_join_av_groupchat(struct Tox *tox, int32_t friendnumber, const uint8_t *data, uint16_t length,
int toxav_join_av_groupchat(Tox *tox, int32_t friendnumber, const uint8_t *data, uint16_t length,
void (*audio_callback)(void *, int, int, const int16_t *, unsigned int, uint8_t, unsigned int, void *),
void *userdata)
{
Expand All @@ -77,7 +77,7 @@ int toxav_join_av_groupchat(struct Tox *tox, int32_t friendnumber, const uint8_t
*
* Recommended values are: samples = 960, channels = 1, sample_rate = 48000
*/
int toxav_group_send_audio(struct Tox *tox, int groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels,
int toxav_group_send_audio(Tox *tox, int groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels,
unsigned int sample_rate)
{
Messenger *m = (Messenger *)tox;
Expand Down
18 changes: 9 additions & 9 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -798,13 +798,13 @@ int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *node
typedef struct {
const uint8_t *base_public_key;
Client_data entry;
} Cmp_data;
} DHT_Cmp_data;

static int cmp_dht_entry(const void *a, const void *b)
{
Cmp_data cmp1, cmp2;
memcpy(&cmp1, a, sizeof(Cmp_data));
memcpy(&cmp2, b, sizeof(Cmp_data));
DHT_Cmp_data cmp1, cmp2;
memcpy(&cmp1, a, sizeof(DHT_Cmp_data));
memcpy(&cmp2, b, sizeof(DHT_Cmp_data));
Client_data entry1 = cmp1.entry;
Client_data entry2 = cmp2.entry;
const uint8_t *cmp_public_key = cmp1.base_public_key;
Expand Down Expand Up @@ -871,14 +871,14 @@ static void sort_client_list(Client_data *list, unsigned int length, const uint8
{
// Pass comp_public_key to qsort with each Client_data entry, so the
// comparison function can use it as the base of comparison.
VLA(Cmp_data, cmp_list, length);
VLA(DHT_Cmp_data, cmp_list, length);

for (uint32_t i = 0; i < length; i++) {
cmp_list[i].base_public_key = comp_public_key;
cmp_list[i].entry = list[i];
}

qsort(cmp_list, length, sizeof(Cmp_data), cmp_dht_entry);
qsort(cmp_list, length, sizeof(DHT_Cmp_data), cmp_dht_entry);

for (uint32_t i = 0; i < length; i++) {
list[i] = cmp_list[i].entry;
Expand Down Expand Up @@ -2736,7 +2736,7 @@ uint32_t DHT_size(const DHT *dht)
return size32 + sizesubhead + (packed_node_size(AF_INET) * numv4) + (packed_node_size(AF_INET6) * numv6);
}

static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
static uint8_t *DHT_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
{
host_to_lendian32(data, len);
data += sizeof(uint32_t);
Expand All @@ -2757,7 +2757,7 @@ void DHT_save(DHT *dht, uint8_t *data)
uint8_t *old_data = data;

/* get right offset. we write the actual header later. */
data = z_state_save_subheader(data, 0, 0);
data = DHT_save_subheader(data, 0, 0);

Node_format clients[MAX_SAVED_DHT_NODES];

Expand Down Expand Up @@ -2793,7 +2793,7 @@ void DHT_save(DHT *dht, uint8_t *data)
}
}

z_state_save_subheader(old_data, pack_nodes(data, sizeof(Node_format) * num, clients, num), DHT_STATE_TYPE_NODES);
DHT_save_subheader(old_data, pack_nodes(data, sizeof(Node_format) * num, clients, num), DHT_STATE_TYPE_NODES);
}

/* Bootstrap from this number of nodes every time DHT_connect_after_load() is called */
Expand Down
44 changes: 22 additions & 22 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ static int send_offline_packet(Messenger *m, int friendcon_id)
sizeof(packet), 0) != -1;
}

static int handle_status(void *object, int i, uint8_t status, void *userdata);
static int handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata);
static int handle_custom_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length,
void *userdata);
static int m_handle_status(void *object, int i, uint8_t status, void *userdata);
static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata);
static int m_handle_custom_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length,
void *userdata);

static int32_t init_new_friend(Messenger *m, const uint8_t *real_pk, uint8_t status)
{
Expand Down Expand Up @@ -198,8 +198,8 @@ static int32_t init_new_friend(Messenger *m, const uint8_t *real_pk, uint8_t sta
m->friendlist[i].userstatus = USERSTATUS_NONE;
m->friendlist[i].is_typing = 0;
m->friendlist[i].message_id = 0;
friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &handle_status, &handle_packet,
&handle_custom_lossy_packet, m, i);
friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &m_handle_status, &m_handle_packet,
&m_handle_custom_lossy_packet, m, i);

if (m->numfriends == i) {
++m->numfriends;
Expand Down Expand Up @@ -1757,8 +1757,8 @@ int m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data,
return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length, 0);
}

static int handle_custom_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length,
void *userdata)
static int m_handle_custom_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length,
void *userdata)
{
Messenger *m = (Messenger *)object;

Expand Down Expand Up @@ -2080,7 +2080,7 @@ static void check_friend_request_timed_out(Messenger *m, uint32_t i, uint64_t t,
}
}

static int handle_status(void *object, int i, uint8_t status, void *userdata)
static int m_handle_status(void *object, int i, uint8_t status, void *userdata)
{
Messenger *m = (Messenger *)object;

Expand All @@ -2095,7 +2095,7 @@ static int handle_status(void *object, int i, uint8_t status, void *userdata)
return 0;
}

static int handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata)
static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata)
{
if (len == 0) {
return -1;
Expand Down Expand Up @@ -2902,7 +2902,7 @@ uint32_t messenger_size(const Messenger *m)
+ sizesubhead;
}

static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
static uint8_t *messenger_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
{
host_to_lendian32(data, len);
data += sizeof(uint32_t);
Expand All @@ -2928,69 +2928,69 @@ void messenger_save(const Messenger *m, uint8_t *data)
assert(sizeof(get_nospam(&m->fr)) == sizeof(uint32_t));
len = size32 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE;
type = MESSENGER_STATE_TYPE_NOSPAMKEYS;
data = z_state_save_subheader(data, len, type);
data = messenger_save_subheader(data, len, type);
*(uint32_t *)data = get_nospam(&(m->fr));
save_keys(m->net_crypto, data + size32);
data += len;

len = saved_friendslist_size(m);
type = MESSENGER_STATE_TYPE_FRIENDS;
data = z_state_save_subheader(data, len, type);
data = messenger_save_subheader(data, len, type);
friends_list_save(m, data);
data += len;

len = m->name_length;
type = MESSENGER_STATE_TYPE_NAME;
data = z_state_save_subheader(data, len, type);
data = messenger_save_subheader(data, len, type);
memcpy(data, m->name, len);
data += len;

len = m->statusmessage_length;
type = MESSENGER_STATE_TYPE_STATUSMESSAGE;
data = z_state_save_subheader(data, len, type);
data = messenger_save_subheader(data, len, type);
memcpy(data, m->statusmessage, len);
data += len;

len = 1;
type = MESSENGER_STATE_TYPE_STATUS;
data = z_state_save_subheader(data, len, type);
data = messenger_save_subheader(data, len, type);
*data = m->userstatus;
data += len;

len = DHT_size(m->dht);
type = MESSENGER_STATE_TYPE_DHT;
data = z_state_save_subheader(data, len, type);
data = messenger_save_subheader(data, len, type);
DHT_save(m->dht, data);
data += len;

Node_format relays[NUM_SAVED_TCP_RELAYS];
type = MESSENGER_STATE_TYPE_TCP_RELAY;
uint8_t *temp_data = data;
data = z_state_save_subheader(temp_data, 0, type);
data = messenger_save_subheader(temp_data, 0, type);
unsigned int num = copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS);
int l = pack_nodes(data, NUM_SAVED_TCP_RELAYS * packed_node_size(TCP_INET6), relays, num);

if (l > 0) {
len = l;
data = z_state_save_subheader(temp_data, len, type);
data = messenger_save_subheader(temp_data, len, type);
data += len;
}

Node_format nodes[NUM_SAVED_PATH_NODES];
type = MESSENGER_STATE_TYPE_PATH_NODE;
temp_data = data;
data = z_state_save_subheader(data, 0, type);
data = messenger_save_subheader(data, 0, type);
memset(nodes, 0, sizeof(nodes));
num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES);
l = pack_nodes(data, NUM_SAVED_PATH_NODES * packed_node_size(TCP_INET6), nodes, num);

if (l > 0) {
len = l;
data = z_state_save_subheader(temp_data, len, type);
data = messenger_save_subheader(temp_data, len, type);
data += len;
}

z_state_save_subheader(data, 0, MESSENGER_STATE_TYPE_END);
messenger_save_subheader(data, 0, MESSENGER_STATE_TYPE_END);
}

static int messenger_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
Expand Down
Loading

0 comments on commit 575b11a

Please sign in to comment.