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 Mar 26, 2024
1 parent 2988770 commit 8553b00
Show file tree
Hide file tree
Showing 61 changed files with 556 additions and 332 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-latest
steps:
- name: Set up Docker Buildx
Expand Down
74 changes: 74 additions & 0 deletions other/docker/cake/cake.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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="b725baa328206cb8a11776b470255cec8efcb170"
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-multichar -Wno-int-conversion -Wno-unused-but-set-variable -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-function -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 -Wno-analyzer-maybe-uninitialized"

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 "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"
45 changes: 26 additions & 19 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,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 @@ -370,7 +370,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 @@ -920,7 +920,7 @@ static void sort_client_list(const Memory *mem, Client_data *list, uint64_t cur_
{
// Pass comp_public_key to qsort with each Client_data entry, so the
// comparison function can use it as the base of comparison.
DHT_Cmp_Data *cmp_list = (DHT_Cmp_Data *)mem_valloc(mem, length, sizeof(DHT_Cmp_Data));
DHT_Cmp_Data *owner cmp_list = (DHT_Cmp_Data *owner)mem_valloc(mem, length, sizeof(DHT_Cmp_Data));

if (cmp_list == nullptr) {
return;
Expand Down Expand Up @@ -1591,13 +1591,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 @@ -1641,13 +1642,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 @@ -1694,8 +1696,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 @@ -2513,15 +2517,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 @@ -2620,7 +2625,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 @@ -2693,7 +2698,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 @@ -2790,7 +2796,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 @@ -463,11 +463,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 @@ -53,15 +53,15 @@ struct Broadcast_Info {
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)

non_null()
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
static Broadcast_Info *owner fetch_broadcast_info(const Network *ns)
{
Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
Broadcast_Info *owner broadcast = (Broadcast_Info *owner)calloc(1, sizeof(Broadcast_Info));

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

IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
IP_ADAPTER_INFO *owner adapter_info = (IP_ADAPTER_INFO *owner)malloc(sizeof(IP_ADAPTER_INFO));

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

if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) {
free(adapter_info);
IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)malloc(out_buf_len);
IP_ADAPTER_INFO *owner new_adapter_info = (IP_ADAPTER_INFO *owner)malloc(out_buf_len);

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

non_null()
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
static Broadcast_Info *owner fetch_broadcast_info(const Network *ns)
{
Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
Broadcast_Info *owner broadcast = (Broadcast_Info *owner)calloc(1, sizeof(Broadcast_Info));

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

non_null()
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
static Broadcast_Info *owner fetch_broadcast_info(const Network *ns)
{
return (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
return (Broadcast_Info *owner)calloc(1, sizeof(Broadcast_Info));
}

#endif /* platforms */
Expand Down Expand Up @@ -375,12 +375,12 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc
return res;
}

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

void lan_discovery_kill(Broadcast_Info *broadcast)
void lan_discovery_kill(Broadcast_Info *owner broadcast)
{
free(broadcast);
}
4 changes: 2 additions & 2 deletions toxcore/LAN_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,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 Network *ns);
Broadcast_Info *owner lan_discovery_init(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 8553b00

Please sign in to comment.