Skip to content

Commit e004342

Browse files
committed
test: Add cake static analyser.
1 parent d9b8fa6 commit e004342

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

+561
-336
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-22.04
2222
steps:
2323
- name: Set up Docker Buildx

other/docker/cake/cake.Dockerfile

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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="aee9bdec15f62db185f6f35ff3879f2d6d00321d"
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 / -DNDEBUG -std=gnu2x -Wall -Wno-pointer-sign -Wno-multichar -Wno-int-conversion -Wno-unused-but-set-variable -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-function -Wno-unused-variable -Wno-unused-label -Wno-return-type -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="-D__x86_64__ -I/src/workspace/cake/src/include -I/src/workspace/cake/src -I/usr/include"
30+
ENV CAKEFLAGS="-D__x86_64__ -I/src/workspace/cake/src/include -I/src/workspace/cake/src -I/usr/include -fanalyzer"
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 "error:" >/dev/null; then exit 1; fi; \
40+
if echo "$OUT" | grep "warning:" >/dev/null; then exit 1; fi; \
41+
if echo "$OUT" | grep " 0 files" >/dev/null; then exit 1; fi; \
42+
done
43+
44+
# For creduce:
45+
#FROM ubuntu:22.04
46+
#
47+
#ENV DEBIAN_FRONTEND="noninteractive"
48+
#RUN apt-get update \
49+
# && apt-get install -y --no-install-recommends \
50+
# creduce \
51+
# gdb \
52+
# libopus-dev \
53+
# libsodium-dev \
54+
# libvpx-dev \
55+
# linux-libc-dev \
56+
# musl-dev \
57+
# && apt-get clean \
58+
# && rm -rf /var/lib/apt/lists/*
59+
#
60+
#SHELL ["/bin/bash", "-o", "pipefail", "-c"]
61+
#
62+
#COPY --from=cake /src/workspace/cake/src/ /src/workspace/cake/src/
63+
#
64+
#WORKDIR /src/workspace/c-toxcore
65+
#COPY . /src/workspace/c-toxcore/
66+
#
67+
#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"
68+
#
69+
##RUN /src/workspace/cake/src/cake $CAKEFLAGS -E toxcore/DHT.c | grep -Ev '^(Cake|/| [01])' >crash.c
70+
#RUN cp toxcore/test.c crash.c
71+
#RUN other/docker/cake/creduce.sh
72+
#RUN creduce other/docker/cake/creduce.sh crash.c \
73+
# || creduce other/docker/cake/creduce.sh crash.c
74+
#
75+
#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: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ struct DHT {
111111
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
112112
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
113113

114-
DHT_Friend *friends_list;
114+
DHT_Friend *owner friends_list;
115115
uint16_t num_friends;
116116

117-
Node_format *loaded_nodes_list;
117+
Node_format *owner loaded_nodes_list;
118118
uint32_t loaded_num_nodes;
119119
unsigned int loaded_nodes_index;
120120

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

376376
if (encrypted == nullptr) {
377377
return -1;
@@ -1637,13 +1637,14 @@ int dht_addfriend(DHT *dht, const uint8_t *public_key, dht_ip_cb *ip_callback,
16371637
return 0;
16381638
}
16391639

1640-
DHT_Friend *const temp = (DHT_Friend *)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends + 1, sizeof(DHT_Friend));
1640+
bool ok;
1641+
dht->friends_list =
1642+
(DHT_Friend *owner)mem_vresize(dht->mem, dht->friends_list, dht->num_friends + 1, sizeof(DHT_Friend), &ok);
16411643

1642-
if (temp == nullptr) {
1644+
if (!ok) {
16431645
return -1;
16441646
}
16451647

1646-
dht->friends_list = temp;
16471648
DHT_Friend *const dht_friend = &dht->friends_list[dht->num_friends];
16481649
*dht_friend = empty_dht_friend;
16491650
memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
@@ -1687,13 +1688,14 @@ int dht_delfriend(DHT *dht, const uint8_t *public_key, uint32_t lock_token)
16871688
return 0;
16881689
}
16891690

1690-
DHT_Friend *const temp = (DHT_Friend *)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends, sizeof(DHT_Friend));
1691+
bool ok;
1692+
dht->friends_list =
1693+
(DHT_Friend *owner)mem_vresize(dht->mem, dht->friends_list, dht->num_friends, sizeof(DHT_Friend), &ok);
16911694

1692-
if (temp == nullptr) {
1695+
if (!ok) {
16931696
return -1;
16941697
}
16951698

1696-
dht->friends_list = temp;
16971699
return 0;
16981700
}
16991701

@@ -1740,8 +1742,10 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
17401742
const uint64_t temp_time = mono_time_get(dht->mono_time);
17411743

17421744
uint32_t num_nodes = 0;
1743-
Client_data **client_list = (Client_data **)mem_valloc(dht->mem, list_count * 2, sizeof(Client_data *));
1744-
IPPTsPng **assoc_list = (IPPTsPng **)mem_valloc(dht->mem, list_count * 2, sizeof(IPPTsPng *));
1745+
Client_data **owner client_list =
1746+
(Client_data **owner)mem_valloc(dht->mem, list_count * 2, sizeof(Client_data *));
1747+
IPPTsPng **owner assoc_list =
1748+
(IPPTsPng **owner)mem_valloc(dht->mem, list_count * 2, sizeof(IPPTsPng *));
17451749
unsigned int sort = 0;
17461750
bool sort_ok = false;
17471751

@@ -2559,15 +2563,16 @@ static int handle_lan_discovery(void *object, const IP_Port *source, const uint8
25592563

25602564
/*----------------------------------------------------------------------------------*/
25612565

2562-
DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
2563-
Mono_Time *mono_time, Networking_Core *net,
2564-
bool hole_punching_enabled, bool lan_discovery_enabled)
2566+
DHT *owner new_dht(
2567+
const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
2568+
Mono_Time *mono_time, Networking_Core *net,
2569+
bool hole_punching_enabled, bool lan_discovery_enabled)
25652570
{
25662571
if (net == nullptr) {
25672572
return nullptr;
25682573
}
25692574

2570-
DHT *const dht = (DHT *)mem_alloc(mem, sizeof(DHT));
2575+
DHT *const owner dht = (DHT *owner)mem_alloc(mem, sizeof(DHT));
25712576

25722577
if (dht == nullptr) {
25732578
LOGGER_ERROR(log, "failed to allocate DHT struct (%ld bytes)", (unsigned long)sizeof(DHT));
@@ -2666,7 +2671,7 @@ void do_dht(DHT *dht)
26662671
ping_iterate(dht->ping);
26672672
}
26682673

2669-
void kill_dht(DHT *dht)
2674+
void kill_dht(DHT *owner dht)
26702675
{
26712676
if (dht == nullptr) {
26722677
return;
@@ -2739,7 +2744,8 @@ void dht_save(const DHT *dht, uint8_t *data)
27392744
/* get right offset. we write the actual header later. */
27402745
data = state_write_section_header(data, DHT_STATE_COOKIE_TYPE, 0, 0);
27412746

2742-
Node_format *clients = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
2747+
Node_format *owner clients =
2748+
(Node_format *owner)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
27432749

27442750
if (clients == nullptr) {
27452751
LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);
@@ -2836,7 +2842,8 @@ static State_Load_Status dht_load_state_callback(void *outer, const uint8_t *dat
28362842
mem_delete(dht->mem, dht->loaded_nodes_list);
28372843

28382844
// Copy to loaded_clients_list
2839-
Node_format *nodes = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
2845+
Node_format *owner nodes =
2846+
(Node_format *owner)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
28402847

28412848
if (nodes == nullptr) {
28422849
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
@@ -464,11 +464,12 @@ int dht_load(DHT *dht, const uint8_t *data, uint32_t length);
464464

465465
/** Initialize DHT. */
466466
non_null()
467-
DHT *new_dht(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);
467+
DHT *owner new_dht(
468+
const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
469+
Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled);
469470

470471
nullable(1)
471-
void kill_dht(DHT *dht);
472+
void kill_dht(DHT *owner dht);
472473

473474
/**
474475
* @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
@@ -54,17 +54,17 @@ struct Broadcast_Info {
5454
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
5555

5656
non_null()
57-
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
57+
static Broadcast_Info *owner fetch_broadcast_info(const Memory *mem, const Network *ns)
5858
{
59-
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
59+
Broadcast_Info *owner broadcast = (Broadcast_Info *owner)mem_alloc(mem, sizeof(Broadcast_Info));
6060

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

6565
broadcast->mem = mem;
6666

67-
IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, sizeof(IP_ADAPTER_INFO));
67+
IP_ADAPTER_INFO *owner adapter_info = (IP_ADAPTER_INFO *owner)mem_balloc(mem, sizeof(IP_ADAPTER_INFO));
6868

6969
if (adapter_info == nullptr) {
7070
mem_delete(mem, broadcast);
@@ -75,7 +75,7 @@ static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns
7575

7676
if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) {
7777
mem_delete(mem, adapter_info);
78-
IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, out_buf_len);
78+
IP_ADAPTER_INFO *owner new_adapter_info = (IP_ADAPTER_INFO *owner)mem_balloc(mem, out_buf_len);
7979

8080
if (new_adapter_info == nullptr) {
8181
mem_delete(mem, broadcast);
@@ -128,9 +128,9 @@ non_null()
128128
static bool ip4_is_local(const IP4 *ip4);
129129

130130
non_null()
131-
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
131+
static Broadcast_Info *owner fetch_broadcast_info(const Memory *mem, const Network *ns)
132132
{
133-
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
133+
Broadcast_Info *owner broadcast = (Broadcast_Info *owner)mem_alloc(mem, sizeof(Broadcast_Info));
134134

135135
if (broadcast == nullptr) {
136136
return nullptr;
@@ -223,9 +223,9 @@ static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns
223223
#else // TODO(irungentoo): Other platforms?
224224

225225
non_null()
226-
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
226+
static Broadcast_Info *owner fetch_broadcast_info(const Memory *mem, const Network *ns)
227227
{
228-
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
228+
Broadcast_Info *broadcast = (Broadcast_Info *owner)mem_alloc(mem, sizeof(Broadcast_Info));
229229

230230
if (broadcast == nullptr) {
231231
return nullptr;
@@ -409,12 +409,12 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc
409409
return res;
410410
}
411411

412-
Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns)
412+
Broadcast_Info *owner lan_discovery_init(const Memory *mem, const Network *ns)
413413
{
414414
return fetch_broadcast_info(mem, ns);
415415
}
416416

417-
void lan_discovery_kill(Broadcast_Info *broadcast)
417+
void lan_discovery_kill(Broadcast_Info *owner broadcast)
418418
{
419419
if (broadcast == nullptr) {
420420
return;

toxcore/LAN_discovery.h

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

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

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

0 commit comments

Comments
 (0)