Skip to content

Commit

Permalink
fix: Correct a use-after-free and fix some memory leaks.
Browse files Browse the repository at this point in the history
Also: use `find_package` to find gtest. This fixes the coverage build to
include unit tests.
  • Loading branch information
iphydf committed Dec 21, 2023
1 parent 4b1cfa3 commit 425216d
Show file tree
Hide file tree
Showing 24 changed files with 143 additions and 156 deletions.
2 changes: 2 additions & 0 deletions .github/scripts/flags-clang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ add_cxx_flag -Wno-c++98-compat-pedantic
add_cxx_flag -Wno-c99-extensions
# We're C-compatible, so use C style casts.
add_cxx_flag -Wno-old-style-cast
# GTest does this.
add_cxx_flag -Wno-global-constructors

# Downgrade to warning so we still see it.
add_flag -Wno-error=unreachable-code
Expand Down
2 changes: 1 addition & 1 deletion .github/scripts/flags-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ add_flag --coverage
add_c_flag -fno-inline -fno-omit-frame-pointer

# Show useful stack traces on crash.
add_flag -fsanitize=undefined -fno-sanitize-recover=all
add_flag -fsanitize=undefined -fno-sanitize-recover=all -D_DEBUG

# In test code (_test.cc and libgtest), throw away all debug information.
# We only care about stack frames inside toxcore (which is C). Without this,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ jobs:
with:
submodules: recursive
- name: Build, test, and upload coverage
run: .github/scripts/coverage-linux
run: other/docker/coverage/run

build-android:
runs-on: ubuntu-latest
Expand Down
42 changes: 26 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#
################################################################################

cmake_minimum_required(VERSION 3.5)
cmake_policy(VERSION 3.5)
cmake_minimum_required(VERSION 3.16)
cmake_policy(VERSION 3.16)
project(toxcore)

list(APPEND CMAKE_MODULE_PATH ${toxcore_SOURCE_DIR}/cmake)
Expand Down Expand Up @@ -76,6 +76,7 @@ if(APPLE)
endif()

enable_testing()
find_package(GTest)

set(CMAKE_MACOSX_RPATH ON)

Expand Down Expand Up @@ -419,23 +420,32 @@ install_module(toxcore DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tox)
#
################################################################################

include(CompileGTest)
function(unit_test subdir target)
add_executable(unit_${target}_test ${subdir}/${target}_test.cc)
target_link_modules(unit_${target}_test toxcore)
target_link_libraries(unit_${target}_test GTest::GTest GTest::Main)
set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}")
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test)
set_property(TEST ${target} PROPERTY ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw")
endfunction()

# The actual unit tests follow.
#
unit_test(toxav ring_buffer)
unit_test(toxav rtp)
unit_test(toxcore DHT)
unit_test(toxcore bin_pack)
unit_test(toxcore crypto_core)
unit_test(toxcore group_announce)
unit_test(toxcore group_moderation)
unit_test(toxcore list)
unit_test(toxcore mem)
unit_test(toxcore mono_time)
unit_test(toxcore ping_array)
unit_test(toxcore tox)
unit_test(toxcore util)
if(GTEST_FOUND)
unit_test(toxav ring_buffer)
unit_test(toxav rtp)
unit_test(toxcore DHT)
unit_test(toxcore bin_pack)
unit_test(toxcore crypto_core)
unit_test(toxcore group_announce)
unit_test(toxcore group_moderation)
unit_test(toxcore list)
unit_test(toxcore mem)
unit_test(toxcore mono_time)
unit_test(toxcore ping_array)
unit_test(toxcore tox)
unit_test(toxcore util)
endif()

add_subdirectory(testing)

Expand Down
4 changes: 4 additions & 0 deletions auto_tests/announce_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ static void test_store_data(void)
ck_assert(log != nullptr);
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
ck_assert(mono_time != nullptr);
Networking_Core *net = new_networking_no_udp(log, mem, ns);
ck_assert(net != nullptr);
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
ck_assert(dht != nullptr);
Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht);
ck_assert(forwarding != nullptr);
Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding);
ck_assert(announce != nullptr);

Expand Down
16 changes: 4 additions & 12 deletions auto_tests/invalid_tcp_proxy_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@
#include "auto_test_support.h"
#include "check_compat.h"

static uint8_t const key[] = {
0x15, 0xE9, 0xC3, 0x09, 0xCF, 0xCB, 0x79, 0xFD,
0xDF, 0x0E, 0xBA, 0x05, 0x7D, 0xAB, 0xB4, 0x9F,
0xE1, 0x5F, 0x38, 0x03, 0xB1, 0xBF, 0xF0, 0x65,
0x36, 0xAE, 0x2E, 0x5B, 0xA5, 0xE4, 0x69, 0x0E,
};

// Try to bootstrap for 30 seconds.
#define NUM_ITERATIONS (unsigned)(30.0 / (ITERATION_INTERVAL / 1000.0))
// Try to bootstrap for 20 seconds.
#define NUM_ITERATIONS (unsigned)(20.0 / (ITERATION_INTERVAL / 1000.0))

int main(void)
{
Expand All @@ -24,13 +17,12 @@ int main(void)
struct Tox_Options *opts = tox_options_new(nullptr);
tox_options_set_udp_enabled(opts, false);
tox_options_set_proxy_type(opts, TOX_PROXY_TYPE_SOCKS5);
tox_options_set_proxy_host(opts, "localhost");
tox_options_set_proxy_host(opts, "127.0.0.1");
tox_options_set_proxy_port(opts, 51724);
Tox *tox = tox_new_log(opts, nullptr, nullptr);
tox_options_free(opts);

tox_add_tcp_relay(tox, "tox.ngc.zone", 33445, key, nullptr);
tox_bootstrap(tox, "tox.ngc.zone", 33445, key, nullptr);
bootstrap_tox_live_network(tox, true);

printf("Waiting for connection...\n");

Expand Down
16 changes: 4 additions & 12 deletions auto_tests/invalid_udp_proxy_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@
#include "auto_test_support.h"
#include "check_compat.h"

static uint8_t const key[] = {
0x15, 0xE9, 0xC3, 0x09, 0xCF, 0xCB, 0x79, 0xFD,
0xDF, 0x0E, 0xBA, 0x05, 0x7D, 0xAB, 0xB4, 0x9F,
0xE1, 0x5F, 0x38, 0x03, 0xB1, 0xBF, 0xF0, 0x65,
0x36, 0xAE, 0x2E, 0x5B, 0xA5, 0xE4, 0x69, 0x0E,
};

// Try to bootstrap for 30 seconds.
#define NUM_ITERATIONS (unsigned)(30.0 / (ITERATION_INTERVAL / 1000.0))
// Try to bootstrap for 20 seconds.
#define NUM_ITERATIONS (unsigned)(20.0 / (ITERATION_INTERVAL / 1000.0))

int main(void)
{
Expand All @@ -24,13 +17,12 @@ int main(void)
struct Tox_Options *opts = tox_options_new(nullptr);
tox_options_set_udp_enabled(opts, true);
tox_options_set_proxy_type(opts, TOX_PROXY_TYPE_SOCKS5);
tox_options_set_proxy_host(opts, "localhost");
tox_options_set_proxy_host(opts, "127.0.0.1");
tox_options_set_proxy_port(opts, 51724);
Tox *tox = tox_new_log(opts, nullptr, nullptr);
tox_options_free(opts);

tox_add_tcp_relay(tox, "tox.ngc.zone", 33445, key, nullptr);
tox_bootstrap(tox, "tox.ngc.zone", 33445, key, nullptr);
bootstrap_tox_live_network(tox, true);

printf("Waiting for connection...");

Expand Down
1 change: 1 addition & 0 deletions auto_tests/send_message_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static void send_message_test(AutoTox *autotoxes)

const size_t msgs_len = tox_max_message_length() + 1;
uint8_t *msgs = (uint8_t *)malloc(msgs_len);
ck_assert(msgs != nullptr);
memset(msgs, MESSAGE_FILLER, msgs_len);

Tox_Err_Friend_Send_Message errm;
Expand Down
62 changes: 0 additions & 62 deletions cmake/CompileGTest.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f117aa0e2cf3f1b42f7e38beec55cb970025692c5753b1159aa22c6e52281393 /usr/local/bin/tox-bootstrapd
7923791feaca748f570f81dd79556c7763fa2f7611e3790129fe44ffa95cf916 /usr/local/bin/tox-bootstrapd
29 changes: 18 additions & 11 deletions other/docker/coverage/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
FROM toxchat/c-toxcore:sources AS src
FROM ubuntu:22.04 AS build
FROM ubuntu:20.04 AS build

RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
ca-certificates \
clang \
cmake \
curl \
gcc \
git \
golang \
golang-1.18 \
libconfig-dev \
libgtest-dev \
libopus-dev \
Expand All @@ -22,10 +24,16 @@ RUN apt-get update && \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& pip3 install --no-cache-dir gcovr
RUN ["strip", "-g",\
"/usr/lib/x86_64-linux-gnu/libgtest.a",\
"/usr/lib/x86_64-linux-gnu/libgtest_main.a"]
RUN ["curl", "-s", "https://codecov.io/bash", "-o", "/usr/local/bin/codecov"]
RUN ["chmod", "+x", "/usr/local/bin/codecov"]

ENV CC=clang \
CXX=clang++ \
PYTHONUNBUFFERED=1
PYTHONUNBUFFERED=1 \
PATH=$PATH:/usr/lib/go-1.18/bin
SHELL ["/bin/bash", "-c"]

WORKDIR /work
Expand All @@ -41,26 +49,27 @@ RUN source .github/scripts/flags-coverage.sh \
-DENABLE_SHARED=OFF \
-DMIN_LOGGER_LEVEL=TRACE \
-DMUST_BUILD_TOXAV=ON \
-DNON_HERMETIC_TESTS=ON \
-DNON_HERMETIC_TESTS=OFF \
-DSTRICT_ABI=ON \
-DAUTOTEST=ON \
-DPROXY_TEST=ON \
-DUSE_IPV6=OFF \
-DTEST_TIMEOUT_SECONDS=30 \
-DTEST_TIMEOUT_SECONDS=40 \
&& cmake --build _build --parallel 8 --target install

WORKDIR /work/_build
RUN /work/proxy_server \
& ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6
RUN /work/other/proxy/proxy_server \
& (ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 || \
ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6)

WORKDIR /work/mallocfail
RUN ["git", "clone", "--depth=1", "https://github.com/ralight/mallocfail", "/work/mallocfail"]
COPY run_mallocfail /usr/local/bin/
COPY syscall_funcs.c src/
RUN gcc -fPIC -shared -O2 -g3 -Wall -Ideps/uthash -Ideps/sha3 deps/*/*.c src/*.c -o mallocfail.so -ldl -lbacktrace \
&& install mallocfail.so /usr/local/lib/mallocfail.so

WORKDIR /work/_build
COPY run_mallocfail /usr/local/bin/
RUN ["run_mallocfail", "--ctest=2", "--jobs=8"]
RUN ["gcovr", \
"--sort-percentage", \
Expand All @@ -74,6 +83,4 @@ RUN ["gcovr", \
"--exclude=(.+/)?other/", \
"--exclude=(.+/)?testing/"]

FROM nginx:alpine
COPY --from=build /work/_build/html/coverage_details.html /usr/share/nginx/html/index.html
COPY --from=build /work/_build/html/ /usr/share/nginx/html/
WORKDIR /work
5 changes: 5 additions & 0 deletions other/docker/coverage/Dockerfile.nginx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# vim:ft=dockerfile
FROM toxchat/c-toxcore:coverage AS build
FROM nginx:alpine
COPY --from=build /work/_build/html/coverage_details.html /usr/share/nginx/html/index.html
COPY --from=build /work/_build/html/ /usr/share/nginx/html/
6 changes: 4 additions & 2 deletions other/docker/coverage/run
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/bin/sh
#!/usr/bin/env bash

set -eux

read -a ci_env <<<"$(bash <(curl -s https://codecov.io/env))"

docker build -t toxchat/c-toxcore:sources -f other/docker/sources/Dockerfile .
docker build -t toxchat/c-toxcore:coverage other/docker/coverage
docker run --name toxcore-coverage --rm -it -p "28192:80" toxchat/c-toxcore:coverage
docker run "${ci_env[@]}" -e CI=true --name toxcore-coverage --rm -t toxchat/c-toxcore:coverage /usr/local/bin/codecov -x "llvm-cov gcov"
Loading

0 comments on commit 425216d

Please sign in to comment.