Skip to content

Commit 60a891e

Browse files
committed
test: Add cake static analyser.
1 parent ac0287a commit 60a891e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+548
-325
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
strategy:
1818
fail-fast: false
1919
matrix:
20-
tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, freebsd, misra, modules, pkgsrc, rpm, slimcc, sparse, tcc, tokstyle]
20+
tool: [autotools, cake, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, freebsd, misra, modules, pkgsrc, rpm, slimcc, sparse, tcc, tokstyle]
2121
runs-on: ubuntu-latest
2222
steps:
2323
- name: Set up Docker Buildx

other/docker/cake/cake.Dockerfile

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
FROM alpine:3.19.0 AS cake
2+
3+
RUN apk add --no-cache \
4+
clang \
5+
compiler-rt \
6+
gdb \
7+
git \
8+
libsodium-dev \
9+
libvpx-dev \
10+
linux-headers \
11+
llvm \
12+
musl-dev \
13+
opus-dev \
14+
util-linux-dev
15+
16+
WORKDIR /src/workspace/cake
17+
ARG CAKE_COMMIT="9d50b5558db519a8b94e104210c55a11da384231"
18+
RUN ["git", "clone", "https://github.com/thradams/cake", "/src/workspace/cake"]
19+
RUN git checkout "$CAKE_COMMIT"
20+
21+
WORKDIR /src/workspace/cake/src
22+
RUN sed -i \
23+
-e 's/ -Wall / -std=gnu2x -Wall -Wno-multichar -Wno-int-conversion -Wno-unused-but-set-variable -Wno-incompatible-pointer-types-discards-qualifiers -Werror -static -ggdb3 /' \
24+
-e 's/RUN "amalgamator.exe/"echo amalgamator.exe/' \
25+
build.c \
26+
&& clang -DDEBUG build.c -o build \
27+
&& ./build
28+
29+
#ENV CAKEFLAGS="-Wno-conditional-constant -D__x86_64__ -I/src/workspace/cake/src/include -I/src/workspace/cake/src -I/usr/include"
30+
#ENV CAKEFLAGS="-Wno-conditional-constant -D__x86_64__ -I/src/workspace/cake/src/include -I/src/workspace/cake/src -I/usr/include -fanalyzer -Wno-analyzer-maybe-uninitialized"
31+
32+
#WORKDIR /src/workspace/c-toxcore
33+
#COPY . /src/workspace/c-toxcore/
34+
35+
#RUN for i in toxcore/*.c; do \
36+
# echo "$i"; \
37+
# OUT="$(/src/workspace/cake/src/cake $CAKEFLAGS "$i")"; \
38+
# echo "$OUT"; \
39+
# if echo "$OUT" | grep "warning:" >/dev/null; then exit 1; fi; \
40+
# if echo "$OUT" | grep " 0 files" >/dev/null; then exit 1; fi; \
41+
# done
42+
43+
# For creduce:
44+
FROM ubuntu:22.04
45+
46+
ENV DEBIAN_FRONTEND="noninteractive"
47+
RUN apt-get update \
48+
&& apt-get install -y --no-install-recommends \
49+
creduce \
50+
gdb \
51+
libopus-dev \
52+
libsodium-dev \
53+
libvpx-dev \
54+
linux-libc-dev \
55+
musl-dev \
56+
&& apt-get clean \
57+
&& rm -rf /var/lib/apt/lists/*
58+
59+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
60+
61+
COPY --from=cake /src/workspace/cake/src/ /src/workspace/cake/src/
62+
63+
WORKDIR /src/workspace/c-toxcore
64+
COPY . /src/workspace/c-toxcore/
65+
66+
ENV CAKEFLAGS="-Wno-conditional-constant -D__x86_64__ -I/src/workspace/cake/src/include -I/src/workspace/cake/src -I/usr/include/x86_64-linux-musl -I/usr/include"
67+
68+
#RUN /src/workspace/cake/src/cake $CAKEFLAGS -E toxcore/DHT.c | grep -Ev '^(Cake|/| [01])' >crash.c
69+
RUN cp toxcore/test.c crash.c
70+
RUN other/docker/cake/creduce.sh
71+
RUN creduce other/docker/cake/creduce.sh crash.c \
72+
|| creduce other/docker/cake/creduce.sh crash.c
73+
74+
RUN gdb -ex r -ex bt --args /src/workspace/cake/src/cake -fanalyzer crash.c
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ===== common =====
2+
# Ignore everything ...
3+
**/*
4+
# ... except sources
5+
!**/*.[ch]
6+
!**/*.cc
7+
!**/*.hh
8+
!CHANGELOG.md
9+
!LICENSE
10+
!README.md
11+
!auto_tests/data/*
12+
!other/bootstrap_daemon/bash-completion/**
13+
!other/bootstrap_daemon/tox-bootstrapd.*
14+
!other/proxy/*.mod
15+
!other/proxy/*.sum
16+
!other/proxy/*.go
17+
# ... and CMake build files (used by most builds).
18+
!**/CMakeLists.txt
19+
!.github/scripts/flags*.sh
20+
!cmake/*.cmake
21+
!other/pkgconfig/*
22+
!other/rpm/*
23+
!so.version
24+
# ===== custom =====
25+
!other/docker/cake/creduce.sh

other/docker/cake/creduce.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
WARNINGS=(-std=c99 -Wall -Wno-return-type -Wno-unused -Werror)
4+
5+
if ! gcc "${WARNINGS[@]}" -I/src/workspace/c-toxcore/toxcore -I/usr/include/x86_64-linux-musl -fsyntax-only crash.c; then
6+
exit 1
7+
fi
8+
/src/workspace/cake/src/cake -I/src/workspace/c-toxcore/toxcore -I/src/workspace/cake/src/include -I/src/workspace/cake/src -fanalyzer crash.c
9+
if [ $? != 139 ]; then
10+
exit 1
11+
fi

other/docker/cake/dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# ===== custom =====
2+
!other/docker/cake/creduce.sh

other/docker/cake/run

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
. "$(cd "$(dirname "${BASH_SOURCE[0]}")/../sources" && pwd)/run.sh"

toxcore/DHT.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ struct DHT {
110110
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
111111
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
112112

113-
DHT_Friend *friends_list;
113+
DHT_Friend *owner friends_list;
114114
uint16_t num_friends;
115115

116-
Node_format *loaded_nodes_list;
116+
Node_format *owner loaded_nodes_list;
117117
uint32_t loaded_num_nodes;
118118
unsigned int loaded_nodes_index;
119119

@@ -370,7 +370,7 @@ int dht_create_packet(const Memory *mem, const Random *rng,
370370
uint8_t *packet, size_t length)
371371
{
372372
uint8_t nonce[CRYPTO_NONCE_SIZE];
373-
uint8_t *encrypted = (uint8_t *)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE);
373+
uint8_t *owner encrypted = (uint8_t *owner)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE);
374374

375375
if (encrypted == nullptr) {
376376
return -1;
@@ -920,7 +920,7 @@ static void sort_client_list(const Memory *mem, Client_data *list, uint64_t cur_
920920
{
921921
// Pass comp_public_key to qsort with each Client_data entry, so the
922922
// comparison function can use it as the base of comparison.
923-
DHT_Cmp_Data *cmp_list = (DHT_Cmp_Data *)mem_valloc(mem, length, sizeof(DHT_Cmp_Data));
923+
DHT_Cmp_Data *owner cmp_list = (DHT_Cmp_Data *owner)mem_valloc(mem, length, sizeof(DHT_Cmp_Data));
924924

925925
if (cmp_list == nullptr) {
926926
return;
@@ -1591,7 +1591,8 @@ int dht_addfriend(DHT *dht, const uint8_t *public_key, dht_ip_cb *ip_callback,
15911591
return 0;
15921592
}
15931593

1594-
DHT_Friend *const temp = (DHT_Friend *)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends + 1, sizeof(DHT_Friend));
1594+
DHT_Friend *const owner temp =
1595+
(DHT_Friend *owner)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends + 1, sizeof(DHT_Friend));
15951596

15961597
if (temp == nullptr) {
15971598
return -1;
@@ -1641,7 +1642,8 @@ int dht_delfriend(DHT *dht, const uint8_t *public_key, uint32_t lock_token)
16411642
return 0;
16421643
}
16431644

1644-
DHT_Friend *const temp = (DHT_Friend *)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends, sizeof(DHT_Friend));
1645+
DHT_Friend *const owner temp =
1646+
(DHT_Friend *owner)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends, sizeof(DHT_Friend));
16451647

16461648
if (temp == nullptr) {
16471649
return -1;
@@ -1694,8 +1696,10 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
16941696
const uint64_t temp_time = mono_time_get(dht->mono_time);
16951697

16961698
uint32_t num_nodes = 0;
1697-
Client_data **client_list = (Client_data **)mem_valloc(dht->mem, list_count * 2, sizeof(Client_data *));
1698-
IPPTsPng **assoc_list = (IPPTsPng **)mem_valloc(dht->mem, list_count * 2, sizeof(IPPTsPng *));
1699+
Client_data **owner client_list =
1700+
(Client_data **owner)mem_valloc(dht->mem, list_count * 2, sizeof(Client_data *));
1701+
IPPTsPng **owner assoc_list =
1702+
(IPPTsPng **owner)mem_valloc(dht->mem, list_count * 2, sizeof(IPPTsPng *));
16991703
unsigned int sort = 0;
17001704
bool sort_ok = false;
17011705

@@ -2521,7 +2525,7 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
25212525
return nullptr;
25222526
}
25232527

2524-
DHT *const dht = (DHT *)mem_alloc(mem, sizeof(DHT));
2528+
DHT *const owner dht = (DHT *owner)mem_alloc(mem, sizeof(DHT));
25252529

25262530
if (dht == nullptr) {
25272531
LOGGER_ERROR(log, "failed to allocate DHT struct (%ld bytes)", (unsigned long)sizeof(DHT));
@@ -2620,7 +2624,7 @@ void do_dht(DHT *dht)
26202624
ping_iterate(dht->ping);
26212625
}
26222626

2623-
void kill_dht(DHT *dht)
2627+
void kill_dht(DHT *owner dht)
26242628
{
26252629
if (dht == nullptr) {
26262630
return;
@@ -2693,7 +2697,8 @@ void dht_save(const DHT *dht, uint8_t *data)
26932697
/* get right offset. we write the actual header later. */
26942698
data = state_write_section_header(data, DHT_STATE_COOKIE_TYPE, 0, 0);
26952699

2696-
Node_format *clients = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
2700+
Node_format *owner clients =
2701+
(Node_format *owner)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
26972702

26982703
if (clients == nullptr) {
26992704
LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);
@@ -2790,7 +2795,8 @@ static State_Load_Status dht_load_state_callback(void *outer, const uint8_t *dat
27902795
mem_delete(dht->mem, dht->loaded_nodes_list);
27912796

27922797
// Copy to loaded_clients_list
2793-
Node_format *nodes = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
2798+
Node_format *owner nodes =
2799+
(Node_format *owner)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
27942800

27952801
if (nodes == nullptr) {
27962802
LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);

toxcore/DHT.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,12 @@ int dht_load(DHT *dht, const uint8_t *data, uint32_t length);
463463

464464
/** Initialize DHT. */
465465
non_null()
466-
DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
467-
Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled);
466+
DHT *owner new_dht(
467+
const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
468+
Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled);
468469

469470
nullable(1)
470-
void kill_dht(DHT *dht);
471+
void kill_dht(DHT *owner dht);
471472

472473
/**
473474
* @retval false if we are not connected to the DHT.

toxcore/LAN_discovery.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ struct Broadcast_Info {
5353
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
5454

5555
non_null()
56-
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
56+
static Broadcast_Info *owner fetch_broadcast_info(const Network *ns)
5757
{
58-
Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
58+
Broadcast_Info *owner broadcast = (Broadcast_Info *owner)calloc(1, sizeof(Broadcast_Info));
5959

6060
if (broadcast == nullptr) {
6161
return nullptr;
6262
}
6363

64-
IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
64+
IP_ADAPTER_INFO *owner adapter_info = (IP_ADAPTER_INFO *owner)malloc(sizeof(IP_ADAPTER_INFO));
6565

6666
if (adapter_info == nullptr) {
6767
free(broadcast);
@@ -72,7 +72,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
7272

7373
if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) {
7474
free(adapter_info);
75-
IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)malloc(out_buf_len);
75+
IP_ADAPTER_INFO *owner new_adapter_info = (IP_ADAPTER_INFO *owner)malloc(out_buf_len);
7676

7777
if (new_adapter_info == nullptr) {
7878
free(broadcast);
@@ -122,9 +122,9 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
122122
#elif !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && (defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__))
123123

124124
non_null()
125-
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
125+
static Broadcast_Info *owner fetch_broadcast_info(const Network *ns)
126126
{
127-
Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
127+
Broadcast_Info *owner broadcast = (Broadcast_Info *owner)calloc(1, sizeof(Broadcast_Info));
128128

129129
if (broadcast == nullptr) {
130130
return nullptr;
@@ -197,9 +197,9 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
197197
#else // TODO(irungentoo): Other platforms?
198198

199199
non_null()
200-
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
200+
static Broadcast_Info *owner fetch_broadcast_info(const Network *ns)
201201
{
202-
return (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
202+
return (Broadcast_Info *owner)calloc(1, sizeof(Broadcast_Info));
203203
}
204204

205205
#endif /* platforms */
@@ -375,12 +375,12 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc
375375
return res;
376376
}
377377

378-
Broadcast_Info *lan_discovery_init(const Network *ns)
378+
Broadcast_Info *owner lan_discovery_init(const Network *ns)
379379
{
380380
return fetch_broadcast_info(ns);
381381
}
382382

383-
void lan_discovery_kill(Broadcast_Info *broadcast)
383+
void lan_discovery_kill(Broadcast_Info *owner broadcast)
384384
{
385385
free(broadcast);
386386
}

toxcore/LAN_discovery.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc
3232
* Discovers broadcast devices and IP addresses.
3333
*/
3434
non_null()
35-
Broadcast_Info *lan_discovery_init(const Network *ns);
35+
Broadcast_Info *owner lan_discovery_init(const Network *ns);
3636

3737
/**
3838
* Free all resources associated with the broadcast info.
3939
*/
4040
nullable(1)
41-
void lan_discovery_kill(Broadcast_Info *broadcast);
41+
void lan_discovery_kill(Broadcast_Info *owner broadcast);
4242

4343
/**
4444
* Is IP a local ip or not.

0 commit comments

Comments
 (0)