Skip to content

Commit 4a2cb37

Browse files
committed
fix: Fix some uninitialised memory errors found by valgrind and msan.
Also added a valgrind build to run it on every pull request. I've had to disable a few tests because valgrind makes those run infinitely slowly, consistently timing them out.
1 parent 46a443f commit 4a2cb37

File tree

20 files changed

+162
-84
lines changed

20 files changed

+162
-84
lines changed

.circleci/config.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ workflows:
88
# Dynamic analysis
99
- asan
1010
- tsan
11+
- msan
1112
# Static analysis
1213
- clang-tidy
1314
- infer
@@ -47,6 +48,17 @@ jobs:
4748
- run: *apt_install
4849
- run: CC=clang .circleci/cmake-tsan
4950

51+
msan:
52+
working_directory: ~/work
53+
docker:
54+
- image: toxchat/toktok-stack:0.0.31-msan
55+
56+
steps:
57+
- checkout
58+
- run: rm -rf /src/workspace/c-toxcore/* && mv * /src/workspace/c-toxcore/
59+
# TODO(iphydf): Remove "|| true" once this works.
60+
- run: cd /src/workspace && bazel test //c-toxcore/auto_tests:lossless_packet_test || true
61+
5062
infer:
5163
working_directory: ~/work
5264
docker:

.cirrus.yml

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,22 @@ bazel-asan_task:
5050
//c-toxcore/...
5151
-//c-toxcore/auto_tests:tcp_relay_test # TODO(robinlinden): Why does this pass locally but not in Cirrus?
5252

53-
# TODO(iphydf): Get msan to work properly.
54-
#bazel-msan_task:
55-
# container:
56-
# image: toxchat/toktok-stack:0.0.31-msan
57-
# cpu: 2
58-
# memory: 4G
59-
# configure_script:
60-
# - /src/workspace/tools/inject-repo c-toxcore
61-
# test_all_script:
62-
# - cd /src/workspace && bazel test -k
63-
# --remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST
64-
# --build_tag_filters=-haskell
65-
# --test_tag_filters=-haskell
66-
# --remote_download_minimal
67-
# --
68-
# //c-toxcore/...
69-
# -//c-toxcore/auto_tests:tcp_relay_test # TODO(robinlinden): Why does this pass locally but not in Cirrus?
53+
bazel-msan_task:
54+
container:
55+
image: toxchat/toktok-stack:0.0.31-msan
56+
cpu: 2
57+
memory: 4G
58+
configure_script:
59+
- /src/workspace/tools/inject-repo c-toxcore
60+
test_all_script:
61+
- cd /src/workspace && bazel test -k
62+
--remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST
63+
--build_tag_filters=-haskell
64+
--test_tag_filters=-haskell
65+
--remote_download_minimal
66+
--
67+
//c-toxcore/...
68+
-//c-toxcore/auto_tests:tcp_relay_test || true # TODO(robinlinden): Why does this pass locally but not in Cirrus?
7069

7170
# TODO(iphydf): Fix test timeouts.
7271
bazel-tsan_task:
@@ -92,6 +91,33 @@ bazel-tsan_task:
9291
-//c-toxcore/auto_tests:tcp_relay_test
9392
-//c-toxcore/auto_tests:tox_many_test
9493

94+
# TODO(iphydf): Fix timeouts so we can run more of the tests disabled below.
95+
bazel-valgrind_task:
96+
container:
97+
image: toxchat/toktok-stack:0.0.31-release
98+
cpu: 2
99+
memory: 4G
100+
configure_script:
101+
- /src/workspace/tools/inject-repo c-toxcore
102+
test_all_script:
103+
- cd /src/workspace && bazel test -k
104+
--remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST
105+
--build_tag_filters=-haskell
106+
--test_tag_filters=-haskell
107+
--remote_download_minimal
108+
--config=valgrind
109+
--
110+
//c-toxcore/...
111+
-//c-toxcore/auto_tests:conference_av_test
112+
-//c-toxcore/auto_tests:conference_test
113+
-//c-toxcore/auto_tests:dht_test
114+
-//c-toxcore/auto_tests:encryptsave_test
115+
-//c-toxcore/auto_tests:file_transfer_test
116+
-//c-toxcore/auto_tests:onion_test
117+
-//c-toxcore/auto_tests:tcp_relay_test
118+
-//c-toxcore/auto_tests:tox_many_tcp_test
119+
-//c-toxcore/auto_tests:tox_many_test
120+
95121
cimple_task:
96122
container:
97123
image: toxchat/toktok-stack:0.0.31-release

.github/settings.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ branches:
2525
- "build-win32"
2626
- "build-win64"
2727
- "CodeFactor"
28-
- "codecov/project"
2928
- "coverage-linux"
3029
- "ci/circleci: asan"
3130
- "ci/circleci: clang-tidy"

.github/workflows/ci.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ on:
55
branches: [master]
66

77
jobs:
8+
build-msan:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- name: Pull toxchat/toktok-stack:0.0.31-msan
13+
run: docker pull toxchat/toktok-stack:0.0.31-msan
14+
- name: Run tests under MemorySanitizer
15+
# TODO(iphydf): Remove "|| true" once this works correctly.
16+
run: docker run --rm -v $PWD:/src/workspace/c-toxcore toxchat/toktok-stack:0.0.31-msan
17+
bazel test //c-toxcore/auto_tests:lossless_packet_test || true
18+
819
build-nacl:
920
runs-on: ubuntu-latest
1021
steps:
@@ -18,8 +29,6 @@ jobs:
1829
build-win32:
1930
runs-on: ubuntu-latest
2031
steps:
21-
- name: Set up Docker Buildx
22-
uses: docker/setup-buildx-action@v1
2332
- uses: actions/checkout@v2
2433
- name: Docker Build
2534
run: .github/scripts/cmake-win32 install
@@ -29,8 +38,6 @@ jobs:
2938
build-win64:
3039
runs-on: ubuntu-latest
3140
steps:
32-
- name: Set up Docker Buildx
33-
uses: docker/setup-buildx-action@v1
3441
- uses: actions/checkout@v2
3542
- name: Docker Build
3643
run: .github/scripts/cmake-win64 install

BUILD.bazel

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,25 @@ project()
88
genrule(
99
name = "copy_headers",
1010
srcs = [
11-
"//c-toxcore/toxav:public_headers",
12-
"//c-toxcore/toxcore:public_headers",
13-
"//c-toxcore/toxencryptsave:public_headers",
11+
"//c-toxcore/toxav:toxav.h",
12+
"//c-toxcore/toxcore:tox.h",
13+
"//c-toxcore/toxencryptsave:toxencryptsave.h",
1414
],
1515
outs = [
1616
"tox/toxav.h",
1717
"tox/tox.h",
1818
"tox/toxencryptsave.h",
1919
],
2020
cmd = """
21-
cp $(location //c-toxcore/toxav:public_headers) $(GENDIR)/c-toxcore/tox/toxav.h
22-
cp $(location //c-toxcore/toxcore:public_headers) $(GENDIR)/c-toxcore/tox/tox.h
23-
cp $(location //c-toxcore/toxencryptsave:public_headers) $(GENDIR)/c-toxcore/tox/toxencryptsave.h
21+
cp $(location //c-toxcore/toxav:toxav.h) $(GENDIR)/c-toxcore/tox/toxav.h
22+
cp $(location //c-toxcore/toxcore:tox.h) $(GENDIR)/c-toxcore/tox/tox.h
23+
cp $(location //c-toxcore/toxencryptsave:toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave.h
2424
""",
2525
)
2626

2727
cc_library(
2828
name = "c-toxcore",
29-
hdrs = [
30-
"tox/tox.h",
31-
"tox/toxav.h",
32-
"tox/toxencryptsave.h",
33-
],
29+
hdrs = [":copy_headers"],
3430
includes = ["."],
3531
visibility = ["//visibility:public"],
3632
deps = [

auto_tests/conference_av_test.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static bool toxes_are_disconnected_from_group(uint32_t tox_count, Tox **toxes,
107107
num_disconnected += disconnected[i];
108108
}
109109

110-
for (uint32_t i = 0; i < tox_count; i++) {
110+
for (uint32_t i = 0; i < tox_count; ++i) {
111111
if (disconnected[i]) {
112112
continue;
113113
}
@@ -152,7 +152,7 @@ static void disconnect_toxes(uint32_t tox_count, Tox **toxes, State *state,
152152

153153
static bool all_connected_to_group(uint32_t tox_count, Tox **toxes)
154154
{
155-
for (uint32_t i = 0; i < tox_count; i++) {
155+
for (uint32_t i = 0; i < tox_count; ++i) {
156156
if (tox_conference_peer_count(toxes[i], 0, nullptr) < tox_count) {
157157
return false;
158158
}
@@ -215,11 +215,11 @@ static bool test_audio(Tox **toxes, State *state, const bool *disabled, bool qui
215215
printf("testing sending and receiving audio\n");
216216
}
217217

218-
int16_t PCM[GROUP_AV_TEST_SAMPLES];
218+
const int16_t PCM[GROUP_AV_TEST_SAMPLES] = {0};
219219

220220
reset_received_audio(toxes, state);
221221

222-
for (uint32_t n = 0; n < GROUP_AV_AUDIO_ITERATIONS; n++) {
222+
for (uint32_t n = 0; n < GROUP_AV_AUDIO_ITERATIONS; ++n) {
223223
for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
224224
if (disabled[i]) {
225225
continue;
@@ -266,7 +266,7 @@ static void test_eventual_audio(Tox **toxes, State *state, const bool *disabled,
266266

267267
static void do_audio(Tox **toxes, State *state, uint32_t iterations)
268268
{
269-
int16_t PCM[GROUP_AV_TEST_SAMPLES];
269+
const int16_t PCM[GROUP_AV_TEST_SAMPLES] = {0};
270270
printf("running audio for %u iterations\n", iterations);
271271

272272
for (uint32_t f = 0; f < iterations; ++f) {

auto_tests/encryptsave_test.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,16 @@ static void test_keys(void)
153153
int ciphertext_length2a = size_large + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
154154
int plaintext_length2a = size_large;
155155
uint8_t *encrypted2a = (uint8_t *)malloc(ciphertext_length2a);
156+
ck_assert(encrypted2a != nullptr);
156157
uint8_t *in_plaintext2a = (uint8_t *)malloc(plaintext_length2a);
158+
ck_assert(in_plaintext2a != nullptr);
159+
random_bytes(in_plaintext2a, plaintext_length2a);
157160
ret = tox_pass_encrypt(in_plaintext2a, plaintext_length2a, key_char, 12, encrypted2a, &encerr);
158161
ck_assert_msg(ret, "tox_pass_encrypt failure 2a: %d", encerr);
159162

160163
// Decryption of same message.
161-
uint8_t *out_plaintext2a = (uint8_t *) malloc(plaintext_length2a);
164+
uint8_t *out_plaintext2a = (uint8_t *)malloc(plaintext_length2a);
165+
ck_assert(out_plaintext2a != nullptr);
162166
ret = tox_pass_decrypt(encrypted2a, ciphertext_length2a, key_char, 12, out_plaintext2a, &decerr);
163167
ck_assert_msg(ret, "tox_pass_decrypt failure 2a: %d", decerr);
164168
ck_assert_msg(memcmp(in_plaintext2a, out_plaintext2a, plaintext_length2a) == 0, "Large message decryption failed");

other/astyle/format-source

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ readarray -t CC_SOURCES <<<"$(find . '(' -name '*.cc' ')')"
2525
CC_SOURCES+=(toxcore/crypto_core.c)
2626
CC_SOURCES+=(toxcore/ping_array.c)
2727

28-
for bin in clang-format-6.0 clang-format-5.0 clang-format; do
28+
for bin in clang-format-11 clang-format-7 clang-format-6.0 clang-format-5.0 clang-format; do
2929
if which "$bin"; then
3030
"$bin" -i -style='{BasedOnStyle: Google, ColumnLimit: 100}' "${CC_SOURCES[@]}"
3131
break
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bf2b6ce150f5dc3ed95e8636dd025a13015c98bbf922b7602969560d310045f7 /usr/local/bin/tox-bootstrapd
1+
7f4c96481e30156e3c2e7e448e70faaaa12911694390374ae09bd53d5d7b4f9c /usr/local/bin/tox-bootstrapd

toxav/BUILD.bazel

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ load("//tools:no_undefined.bzl", "cc_library")
33

44
package(features = ["layering_check"])
55

6-
filegroup(
7-
name = "public_headers",
6+
exports_files(
87
srcs = ["toxav.h"],
98
visibility = ["//c-toxcore:__pkg__"],
109
)
1110

11+
# Private library with the public API header in it because in toxav, lots of
12+
# things depend on the public API header.
1213
cc_library(
13-
name = "public",
14-
hdrs = [":public_headers"],
14+
name = "public_api",
15+
hdrs = ["toxav.h"],
1516
)
1617

1718
cc_library(
@@ -86,7 +87,7 @@ cc_library(
8687
srcs = ["audio.c"],
8788
hdrs = ["audio.h"],
8889
deps = [
89-
":public",
90+
":public_api",
9091
":rtp",
9192
"//c-toxcore/toxcore:logger",
9293
"//c-toxcore/toxcore:mono_time",
@@ -107,7 +108,7 @@ cc_library(
107108
],
108109
deps = [
109110
":audio",
110-
":public",
111+
":public_api",
111112
":ring_buffer",
112113
":rtp",
113114
"//c-toxcore/toxcore:logger",

0 commit comments

Comments
 (0)