Skip to content

Commit

Permalink
test: Add cake static analyser.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 19, 2025
1 parent d9b8fa6 commit 4c136cc
Show file tree
Hide file tree
Showing 60 changed files with 541 additions and 328 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, freebsd, misra, modules, pkgsrc, rpm, slimcc, sparse, tcc, tokstyle]
tool: [autotools, cake, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, freebsd, misra, modules, pkgsrc, rpm, slimcc, sparse, tcc, tokstyle]
runs-on: ubuntu-22.04
steps:
- name: Set up Docker Buildx
Expand Down
75 changes: 75 additions & 0 deletions other/docker/cake/cake.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
FROM alpine:3.19.0 AS cake

RUN ["apk", "add", "--no-cache", \
"clang", \
"compiler-rt", \
"gdb", \
"git", \
"libsodium-dev", \
"libvpx-dev", \
"linux-headers", \
"llvm", \
"musl-dev", \
"opus-dev", \
"util-linux-dev"]

WORKDIR /src/workspace/cake
ARG CAKE_COMMIT="aee9bdec15f62db185f6f35ff3879f2d6d00321d"
RUN ["git", "clone", "https://github.com/thradams/cake", "/src/workspace/cake"]
RUN git checkout "$CAKE_COMMIT"

WORKDIR /src/workspace/cake/src
RUN sed -i \
-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 /' \
-e 's/RUN "amalgamator.exe/"echo amalgamator.exe/' \
build.c \
&& clang -DDEBUG build.c -o build \
&& ./build

# ENV CAKEFLAGS="-D__x86_64__ -I/src/workspace/cake/src/include -I/src/workspace/cake/src -I/usr/include"
ENV CAKEFLAGS="-D__x86_64__ -I/src/workspace/cake/src/include -I/src/workspace/cake/src -I/usr/include -fanalyzer"

WORKDIR /src/workspace/c-toxcore
COPY . /src/workspace/c-toxcore/

RUN for i in toxcore/*.c; do \

Check warning on line 35 in other/docker/cake/cake.Dockerfile

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

other/docker/cake/cake.Dockerfile#L35

Set the SHELL option -o pipefail before RUN with a pipe in it. If you are using /bin/sh in an alpine image or if your shell is symlinked to busybox then consider explicitly setting your SHELL to /bin/ash, or disable this check
echo "$i"; \
OUT="$(/src/workspace/cake/src/cake $CAKEFLAGS "$i")"; \
echo "$OUT"; \
if echo "$OUT" | grep "error:" >/dev/null; then exit 1; fi; \
if echo "$OUT" | grep "warning:" >/dev/null; then exit 1; fi; \
if echo "$OUT" | grep " 0 files" >/dev/null; then exit 1; fi; \
done

# For creduce:
#FROM ubuntu:22.04
#
#ENV DEBIAN_FRONTEND="noninteractive"
#RUN apt-get update \
# && apt-get install -y --no-install-recommends \
# creduce \
# gdb \
# libopus-dev \
# libsodium-dev \
# libvpx-dev \
# linux-libc-dev \
# musl-dev \
# && apt-get clean \
# && rm -rf /var/lib/apt/lists/*
#
#SHELL ["/bin/bash", "-o", "pipefail", "-c"]
#
#COPY --from=cake /src/workspace/cake/src/ /src/workspace/cake/src/
#
#WORKDIR /src/workspace/c-toxcore
#COPY . /src/workspace/c-toxcore/
#
#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"
#
##RUN /src/workspace/cake/src/cake $CAKEFLAGS -E toxcore/DHT.c | grep -Ev '^(Cake|/| [01])' >crash.c
#RUN cp toxcore/test.c crash.c
#RUN other/docker/cake/creduce.sh
#RUN creduce other/docker/cake/creduce.sh crash.c \
# || creduce other/docker/cake/creduce.sh crash.c
#
#RUN gdb -ex r -ex bt --args /src/workspace/cake/src/cake -fanalyzer crash.c
25 changes: 25 additions & 0 deletions other/docker/cake/cake.Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ===== common =====
# Ignore everything ...
**/*
# ... except sources
!**/*.[ch]
!**/*.cc
!**/*.hh
!CHANGELOG.md
!LICENSE
!README.md
!auto_tests/data/*
!other/bootstrap_daemon/bash-completion/**
!other/bootstrap_daemon/tox-bootstrapd.*
!other/proxy/*.mod
!other/proxy/*.sum
!other/proxy/*.go
# ... and CMake build files (used by most builds).
!**/CMakeLists.txt
!.github/scripts/flags*.sh
!cmake/*.cmake
!other/pkgconfig/*
!other/rpm/*
!so.version
# ===== custom =====
!other/docker/cake/creduce.sh
11 changes: 11 additions & 0 deletions other/docker/cake/creduce.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

WARNINGS=(-std=c99 -Wall -Wno-return-type -Wno-unused -Werror)

if ! gcc "${WARNINGS[@]}" -I/src/workspace/c-toxcore/toxcore -I/usr/include/x86_64-linux-musl -fsyntax-only crash.c; then
exit 1
fi
/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
if [ $? != 139 ]; then
exit 1
fi
2 changes: 2 additions & 0 deletions other/docker/cake/dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ===== custom =====
!other/docker/cake/creduce.sh
3 changes: 3 additions & 0 deletions other/docker/cake/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

. "$(cd "$(dirname "${BASH_SOURCE[0]}")/../sources" && pwd)/run.sh"
43 changes: 25 additions & 18 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ struct DHT {
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];

DHT_Friend *friends_list;
DHT_Friend *owner friends_list;
uint16_t num_friends;

Node_format *loaded_nodes_list;
Node_format *owner loaded_nodes_list;
uint32_t loaded_num_nodes;
unsigned int loaded_nodes_index;

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

if (encrypted == nullptr) {
return -1;
Expand Down Expand Up @@ -1637,13 +1637,14 @@ int dht_addfriend(DHT *dht, const uint8_t *public_key, dht_ip_cb *ip_callback,
return 0;
}

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

if (temp == nullptr) {
if (!ok) {
return -1;
}

dht->friends_list = temp;
DHT_Friend *const dht_friend = &dht->friends_list[dht->num_friends];
*dht_friend = empty_dht_friend;
memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
Expand Down Expand Up @@ -1687,13 +1688,14 @@ int dht_delfriend(DHT *dht, const uint8_t *public_key, uint32_t lock_token)
return 0;
}

DHT_Friend *const temp = (DHT_Friend *)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends, sizeof(DHT_Friend));
bool ok;
dht->friends_list =
(DHT_Friend *owner)mem_vresize(dht->mem, dht->friends_list, dht->num_friends, sizeof(DHT_Friend), &ok);

if (temp == nullptr) {
if (!ok) {
return -1;
}

dht->friends_list = temp;
return 0;
}

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

uint32_t num_nodes = 0;
Client_data **client_list = (Client_data **)mem_valloc(dht->mem, list_count * 2, sizeof(Client_data *));
IPPTsPng **assoc_list = (IPPTsPng **)mem_valloc(dht->mem, list_count * 2, sizeof(IPPTsPng *));
Client_data **owner client_list =
(Client_data **owner)mem_valloc(dht->mem, list_count * 2, sizeof(Client_data *));
IPPTsPng **owner assoc_list =
(IPPTsPng **owner)mem_valloc(dht->mem, list_count * 2, sizeof(IPPTsPng *));
unsigned int sort = 0;
bool sort_ok = false;

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

/*----------------------------------------------------------------------------------*/

DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
Mono_Time *mono_time, Networking_Core *net,
bool hole_punching_enabled, bool lan_discovery_enabled)
DHT *owner new_dht(
const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
Mono_Time *mono_time, Networking_Core *net,
bool hole_punching_enabled, bool lan_discovery_enabled)
{
if (net == nullptr) {
return nullptr;
}

DHT *const dht = (DHT *)mem_alloc(mem, sizeof(DHT));
DHT *const owner dht = (DHT *owner)mem_alloc(mem, sizeof(DHT));

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

void kill_dht(DHT *dht)
void kill_dht(DHT *owner dht)
{
if (dht == nullptr) {
return;
Expand Down Expand Up @@ -2739,7 +2744,8 @@ void dht_save(const DHT *dht, uint8_t *data)
/* get right offset. we write the actual header later. */
data = state_write_section_header(data, DHT_STATE_COOKIE_TYPE, 0, 0);

Node_format *clients = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
Node_format *owner clients =
(Node_format *owner)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));

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

// Copy to loaded_clients_list
Node_format *nodes = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
Node_format *owner nodes =
(Node_format *owner)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));

if (nodes == nullptr) {
LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);
Expand Down
7 changes: 4 additions & 3 deletions toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,12 @@ int dht_load(DHT *dht, const uint8_t *data, uint32_t length);

/** Initialize DHT. */
non_null()
DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled);
DHT *owner new_dht(
const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled);

nullable(1)
void kill_dht(DHT *dht);
void kill_dht(DHT *owner dht);

/**
* @retval false if we are not connected to the DHT.
Expand Down
20 changes: 10 additions & 10 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ struct Broadcast_Info {
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)

non_null()
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
static Broadcast_Info *owner fetch_broadcast_info(const Memory *mem, const Network *ns)
{
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
Broadcast_Info *owner broadcast = (Broadcast_Info *owner)mem_alloc(mem, sizeof(Broadcast_Info));

if (broadcast == nullptr) {
return nullptr;
}

broadcast->mem = mem;

IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, sizeof(IP_ADAPTER_INFO));
IP_ADAPTER_INFO *owner adapter_info = (IP_ADAPTER_INFO *owner)mem_balloc(mem, sizeof(IP_ADAPTER_INFO));

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

if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) {
mem_delete(mem, adapter_info);
IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, out_buf_len);
IP_ADAPTER_INFO *owner new_adapter_info = (IP_ADAPTER_INFO *owner)mem_balloc(mem, out_buf_len);

if (new_adapter_info == nullptr) {
mem_delete(mem, broadcast);
Expand Down Expand Up @@ -128,9 +128,9 @@ non_null()
static bool ip4_is_local(const IP4 *ip4);

non_null()
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
static Broadcast_Info *owner fetch_broadcast_info(const Memory *mem, const Network *ns)
{
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
Broadcast_Info *owner broadcast = (Broadcast_Info *owner)mem_alloc(mem, sizeof(Broadcast_Info));

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

non_null()
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
static Broadcast_Info *owner fetch_broadcast_info(const Memory *mem, const Network *ns)
{
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
Broadcast_Info *broadcast = (Broadcast_Info *owner)mem_alloc(mem, sizeof(Broadcast_Info));

if (broadcast == nullptr) {
return nullptr;
Expand Down Expand Up @@ -409,12 +409,12 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc
return res;
}

Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns)
Broadcast_Info *owner lan_discovery_init(const Memory *mem, const Network *ns)
{
return fetch_broadcast_info(mem, ns);
}

void lan_discovery_kill(Broadcast_Info *broadcast)
void lan_discovery_kill(Broadcast_Info *owner broadcast)
{
if (broadcast == nullptr) {
return;
Expand Down
4 changes: 2 additions & 2 deletions toxcore/LAN_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc
* Discovers broadcast devices and IP addresses.
*/
non_null()
Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns);
Broadcast_Info *owner lan_discovery_init(const Memory *mem, const Network *ns);

/**
* Free all resources associated with the broadcast info.
*/
nullable(1)
void lan_discovery_kill(Broadcast_Info *broadcast);
void lan_discovery_kill(Broadcast_Info *owner broadcast);

/**
* Is IP a local ip or not.
Expand Down
Loading

0 comments on commit 4c136cc

Please sign in to comment.