Skip to content

Commit 93c83fb

Browse files
committed
refactor: Use strong typedef instead of struct for Socket.
Sparse checks it. This is neater than using a struct, which has some slightly weird syntax at times. This also reduces the risk of someone adding another struct member.
1 parent 9fe18b1 commit 93c83fb

File tree

13 files changed

+262
-127
lines changed

13 files changed

+262
-127
lines changed

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ CheckOptions:
2121
- key: readability-identifier-naming.MacroDefinitionCase
2222
value: UPPER_CASE
2323
- key: readability-identifier-naming.MacroDefinitionIgnoredRegexp
24-
value: "^_.*|nullable|non_null|nullptr|static_assert|ck_.*"
24+
value: "^_.*|bitwise|force|nullable|non_null|nullptr|static_assert|ck_.*"
2525
- key: readability-identifier-naming.ParameterCase
2626
value: lower_case
2727
- key: readability-identifier-naming.StructCase

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ jobs:
1515

1616
analysis:
1717
strategy:
18+
fail-fast: false
1819
matrix:
19-
tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, misra, modules, rpm, slimcc, tcc, tokstyle]
20+
tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, misra, modules, rpm, slimcc, sparse, tcc, tokstyle]
2021
runs-on: ubuntu-latest
2122
steps:
2223
- name: Set up Docker Buildx

other/docker/sparse/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!/Makefile

other/docker/sparse/Makefile

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
SOURCES := $(wildcard tox*/*.c tox*/*/*.c) \
2+
third_party/cmp/cmp.c
3+
OBJECTS := $(SOURCES:.c=.o)
4+
5+
CFLAGS := $(shell pkg-config --cflags libsodium opus vpx)
6+
CPPFLAGS := -DSPARSE -DTCP_SERVER_USE_EPOLL=1 -DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE
7+
8+
SPARSE_FLAGS := \
9+
-Wsparse-error \
10+
-Wpedantic \
11+
-Waddress \
12+
-Waddress-space \
13+
-Wbitwise \
14+
-Wbitwise-pointer \
15+
-Wcast-from-as \
16+
-Wcast-to-as \
17+
-Wcast-truncate \
18+
-Wconstant-suffix \
19+
-Wconstexpr-not-const \
20+
-Wcontext \
21+
-Wdecl \
22+
-Wdefault-bitfield-sign \
23+
-Wdesignated-init \
24+
-Wdo-while \
25+
-Wenum-mismatch \
26+
-Wexternal-function-has-definition \
27+
-Wflexible-array-array \
28+
-Wflexible-array-nested \
29+
-Wflexible-array-union \
30+
-Wimplicit-int \
31+
-Winit-cstring \
32+
-Wint-to-pointer-cast \
33+
-Wmemcpy-max-count \
34+
-Wnon-pointer-null \
35+
-Wnewline-eof \
36+
-Wold-initializer \
37+
-Wold-style-definition \
38+
-Wone-bit-signed-bitfield \
39+
-Woverride-init \
40+
-Woverride-init-all \
41+
-Wparen-string \
42+
-Wpast-deep-designator \
43+
-Wpedantic \
44+
-Wpointer-to-int-cast \
45+
-Wptr-subtraction-blows \
46+
-Wreturn-void \
47+
-Wshadow \
48+
-Wshift-count-negative \
49+
-Wshift-count-overflow \
50+
-Wsizeof-bool \
51+
-Wstrict-prototypes \
52+
-Wpointer-arith \
53+
-Wsparse-error \
54+
-Wtautological-compare \
55+
-Wtransparent-union \
56+
-Wtypesign \
57+
-Wundef \
58+
-Wuninitialized \
59+
-Wunion-cast \
60+
-Wvla
61+
62+
SMATCH_FLAGS := $(foreach i,$(shell smatch --show-checks | grep -o 'check_.*'),--enable=$i)
63+
64+
analyse: $(OBJECTS)
65+
66+
%.o: %.c
67+
@echo "Processing $<"
68+
@sparse $(CFLAGS) $(CPPFLAGS) $(SPARSE_FLAGS) $<
69+
# @smatch $(CFLAGS) $(CPPFLAGS) $(SMATCH_FLAGS) $<
70+
# @sparse-llvm $(CFLAGS) $(CPPFLAGS) $< > /dev/null

other/docker/sparse/local.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CFLAGS=-O3 -g -Wno-discarded-qualifiers -Wno-format-truncation -Wno-stringop-truncation -Wno-uninitialized -Wno-unused -Wno-unused-result

other/docker/sparse/run

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
set -eux
4+
BUILD=sparse
5+
other/docker/sources/build
6+
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
FROM toxchat/c-toxcore:sources AS sources
2+
FROM ubuntu:22.04
3+
4+
RUN apt-get update && \
5+
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
6+
ca-certificates \
7+
creduce \
8+
g++ \
9+
gcc \
10+
git \
11+
libc-dev \
12+
libopus-dev \
13+
libsodium-dev \
14+
libsqlite3-dev \
15+
libssl-dev \
16+
libvpx-dev \
17+
llvm-dev \
18+
make \
19+
pkg-config \
20+
&& apt-get clean \
21+
&& rm -rf /var/lib/apt/lists/*
22+
23+
WORKDIR /work/smatch
24+
RUN git clone --depth=1 https://repo.or.cz/smatch.git /work/smatch
25+
COPY other/docker/sparse/local.mk /work/smatch/local.mk
26+
RUN make install -j4 PREFIX=/usr/local
27+
28+
WORKDIR /work/c-toxcore
29+
COPY --from=sources /src/ /work/c-toxcore
30+
#COPY other/make_single_file /work/c-toxcore/other/
31+
#RUN other/make_single_file auto_tests/tox_new_test.c > crash.c
32+
#RUN sparsec $(pkg-config --cflags --libs libsodium opus vpx) crash.c
33+
34+
COPY other/docker/sparse/Makefile /work/c-toxcore/
35+
RUN make -j4

testing/fuzzing/fuzz_support.cc

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,25 @@ static constexpr Memory_Funcs fuzz_memory_funcs = {
107107
};
108108

109109
static constexpr Network_Funcs fuzz_network_funcs = {
110-
/* .close = */ ![](Fuzz_System *self, int sock) { return 0; },
111-
/* .accept = */ ![](Fuzz_System *self, int sock) { return 1337; },
112-
/* .bind = */ ![](Fuzz_System *self, int sock, const Network_Addr *addr) { return 0; },
113-
/* .listen = */ ![](Fuzz_System *self, int sock, int backlog) { return 0; },
110+
/* .close = */ ![](Fuzz_System *self, Socket sock) { return 0; },
111+
/* .accept = */ ![](Fuzz_System *self, Socket sock) { return Socket{1337}; },
112+
/* .bind = */ ![](Fuzz_System *self, Socket sock, const Network_Addr *addr) { return 0; },
113+
/* .listen = */ ![](Fuzz_System *self, Socket sock, int backlog) { return 0; },
114114
/* .recvbuf = */
115-
![](Fuzz_System *self, int sock) {
116-
assert(sock == 42 || sock == 1337);
115+
![](Fuzz_System *self, Socket sock) {
116+
assert(sock.value == 42 || sock.value == 1337);
117117
const size_t count = random_u16(self->rng.get());
118118
return static_cast<int>(std::min(count, self->data.size()));
119119
},
120120
/* .recv = */
121-
![](Fuzz_System *self, int sock, uint8_t *buf, size_t len) {
122-
assert(sock == 42 || sock == 1337);
121+
![](Fuzz_System *self, Socket sock, uint8_t *buf, size_t len) {
122+
assert(sock.value == 42 || sock.value == 1337);
123123
// Receive data from the fuzzer.
124124
return recv_common(self->data, buf, len);
125125
},
126126
/* .recvfrom = */
127-
![](Fuzz_System *self, int sock, uint8_t *buf, size_t len, Network_Addr *addr) {
128-
assert(sock == 42 || sock == 1337);
127+
![](Fuzz_System *self, Socket sock, uint8_t *buf, size_t len, Network_Addr *addr) {
128+
assert(sock.value == 42 || sock.value == 1337);
129129

130130
addr->addr = sockaddr_storage{};
131131
// Dummy Addr
@@ -140,26 +140,26 @@ static constexpr Network_Funcs fuzz_network_funcs = {
140140
return recv_common(self->data, buf, len);
141141
},
142142
/* .send = */
143-
![](Fuzz_System *self, int sock, const uint8_t *buf, size_t len) {
144-
assert(sock == 42 || sock == 1337);
143+
![](Fuzz_System *self, Socket sock, const uint8_t *buf, size_t len) {
144+
assert(sock.value == 42 || sock.value == 1337);
145145
// Always succeed.
146146
return static_cast<int>(len);
147147
},
148148
/* .sendto = */
149-
![](Fuzz_System *self, int sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {
150-
assert(sock == 42 || sock == 1337);
149+
![](Fuzz_System *self, Socket sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {
150+
assert(sock.value == 42 || sock.value == 1337);
151151
// Always succeed.
152152
return static_cast<int>(len);
153153
},
154-
/* .socket = */ ![](Fuzz_System *self, int domain, int type, int proto) { return 42; },
155-
/* .socket_nonblock = */ ![](Fuzz_System *self, int sock, bool nonblock) { return 0; },
154+
/* .socket = */ ![](Fuzz_System *self, int domain, int type, int proto) { return Socket{42}; },
155+
/* .socket_nonblock = */ ![](Fuzz_System *self, Socket sock, bool nonblock) { return 0; },
156156
/* .getsockopt = */
157-
![](Fuzz_System *self, int sock, int level, int optname, void *optval, size_t *optlen) {
157+
![](Fuzz_System *self, Socket sock, int level, int optname, void *optval, size_t *optlen) {
158158
std::memset(optval, 0, *optlen);
159159
return 0;
160160
},
161161
/* .setsockopt = */
162-
![](Fuzz_System *self, int sock, int level, int optname, const void *optval, size_t optlen) {
162+
![](Fuzz_System *self, Socket sock, int level, int optname, const void *optval, size_t optlen) {
163163
return 0;
164164
},
165165
};
@@ -221,42 +221,42 @@ static constexpr Memory_Funcs null_memory_funcs = {
221221
};
222222

223223
static constexpr Network_Funcs null_network_funcs = {
224-
/* .close = */ ![](Null_System *self, int sock) { return 0; },
225-
/* .accept = */ ![](Null_System *self, int sock) { return 1337; },
226-
/* .bind = */ ![](Null_System *self, int sock, const Network_Addr *addr) { return 0; },
227-
/* .listen = */ ![](Null_System *self, int sock, int backlog) { return 0; },
228-
/* .recvbuf = */ ![](Null_System *self, int sock) { return 0; },
224+
/* .close = */ ![](Null_System *self, Socket sock) { return 0; },
225+
/* .accept = */ ![](Null_System *self, Socket sock) { return Socket{1337}; },
226+
/* .bind = */ ![](Null_System *self, Socket sock, const Network_Addr *addr) { return 0; },
227+
/* .listen = */ ![](Null_System *self, Socket sock, int backlog) { return 0; },
228+
/* .recvbuf = */ ![](Null_System *self, Socket sock) { return 0; },
229229
/* .recv = */
230-
![](Null_System *self, int sock, uint8_t *buf, size_t len) {
230+
![](Null_System *self, Socket sock, uint8_t *buf, size_t len) {
231231
// Always fail.
232232
errno = ENOMEM;
233233
return -1;
234234
},
235235
/* .recvfrom = */
236-
![](Null_System *self, int sock, uint8_t *buf, size_t len, Network_Addr *addr) {
236+
![](Null_System *self, Socket sock, uint8_t *buf, size_t len, Network_Addr *addr) {
237237
// Always fail.
238238
errno = ENOMEM;
239239
return -1;
240240
},
241241
/* .send = */
242-
![](Null_System *self, int sock, const uint8_t *buf, size_t len) {
242+
![](Null_System *self, Socket sock, const uint8_t *buf, size_t len) {
243243
// Always succeed.
244244
return static_cast<int>(len);
245245
},
246246
/* .sendto = */
247-
![](Null_System *self, int sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {
247+
![](Null_System *self, Socket sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {
248248
// Always succeed.
249249
return static_cast<int>(len);
250250
},
251-
/* .socket = */ ![](Null_System *self, int domain, int type, int proto) { return 42; },
252-
/* .socket_nonblock = */ ![](Null_System *self, int sock, bool nonblock) { return 0; },
251+
/* .socket = */ ![](Null_System *self, int domain, int type, int proto) { return Socket{42}; },
252+
/* .socket_nonblock = */ ![](Null_System *self, Socket sock, bool nonblock) { return 0; },
253253
/* .getsockopt = */
254-
![](Null_System *self, int sock, int level, int optname, void *optval, size_t *optlen) {
254+
![](Null_System *self, Socket sock, int level, int optname, void *optval, size_t *optlen) {
255255
std::memset(optval, 0, *optlen);
256256
return 0;
257257
},
258258
/* .setsockopt = */
259-
![](Null_System *self, int sock, int level, int optname, const void *optval, size_t optlen) {
259+
![](Null_System *self, Socket sock, int level, int optname, const void *optval, size_t optlen) {
260260
return 0;
261261
},
262262
};
@@ -327,10 +327,10 @@ static constexpr Memory_Funcs record_memory_funcs = {
327327
};
328328

329329
static constexpr Network_Funcs record_network_funcs = {
330-
/* .close = */ ![](Record_System *self, int sock) { return 0; },
331-
/* .accept = */ ![](Record_System *self, int sock) { return 2; },
330+
/* .close = */ ![](Record_System *self, Socket sock) { return 0; },
331+
/* .accept = */ ![](Record_System *self, Socket sock) { return Socket{2}; },
332332
/* .bind = */
333-
![](Record_System *self, int sock, const Network_Addr *addr) {
333+
![](Record_System *self, Socket sock, const Network_Addr *addr) {
334334
const uint16_t port = get_port(addr);
335335
if (self->global_.bound.find(port) != self->global_.bound.end()) {
336336
errno = EADDRINUSE;
@@ -340,17 +340,17 @@ static constexpr Network_Funcs record_network_funcs = {
340340
self->port = port;
341341
return 0;
342342
},
343-
/* .listen = */ ![](Record_System *self, int sock, int backlog) { return 0; },
344-
/* .recvbuf = */ ![](Record_System *self, int sock) { return 0; },
343+
/* .listen = */ ![](Record_System *self, Socket sock, int backlog) { return 0; },
344+
/* .recvbuf = */ ![](Record_System *self, Socket sock) { return 0; },
345345
/* .recv = */
346-
![](Record_System *self, int sock, uint8_t *buf, size_t len) {
346+
![](Record_System *self, Socket sock, uint8_t *buf, size_t len) {
347347
// Always fail.
348348
errno = ENOMEM;
349349
return -1;
350350
},
351351
/* .recvfrom = */
352-
![](Record_System *self, int sock, uint8_t *buf, size_t len, Network_Addr *addr) {
353-
assert(sock == 42);
352+
![](Record_System *self, Socket sock, uint8_t *buf, size_t len, Network_Addr *addr) {
353+
assert(sock.value == 42);
354354
if (self->recvq.empty()) {
355355
self->push("\xff\xff");
356356
errno = EWOULDBLOCK;
@@ -385,29 +385,30 @@ static constexpr Network_Funcs record_network_funcs = {
385385
return static_cast<int>(recvlen);
386386
},
387387
/* .send = */
388-
![](Record_System *self, int sock, const uint8_t *buf, size_t len) {
388+
![](Record_System *self, Socket sock, const uint8_t *buf, size_t len) {
389389
// Always succeed.
390390
return static_cast<int>(len);
391391
},
392392
/* .sendto = */
393-
![](Record_System *self, int sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {
394-
assert(sock == 42);
393+
![](Record_System *self, Socket sock, const uint8_t *buf, size_t len,
394+
const Network_Addr *addr) {
395+
assert(sock.value == 42);
395396
auto backend = self->global_.bound.find(get_port(addr));
396397
assert(backend != self->global_.bound.end());
397398
backend->second->receive(self->port, buf, len);
398399
return static_cast<int>(len);
399400
},
400-
/* .socket = */ ![](Record_System *self, int domain, int type, int proto) { return 42; },
401-
/* .socket_nonblock = */ ![](Record_System *self, int sock, bool nonblock) { return 0; },
401+
/* .socket = */
402+
![](Record_System *self, int domain, int type, int proto) { return Socket{42}; },
403+
/* .socket_nonblock = */ ![](Record_System *self, Socket sock, bool nonblock) { return 0; },
402404
/* .getsockopt = */
403-
![](Record_System *self, int sock, int level, int optname, void *optval, size_t *optlen) {
405+
![](Record_System *self, Socket sock, int level, int optname, void *optval, size_t *optlen) {
404406
std::memset(optval, 0, *optlen);
405407
return 0;
406408
},
407409
/* .setsockopt = */
408-
![](Record_System *self, int sock, int level, int optname, const void *optval, size_t optlen) {
409-
return 0;
410-
},
410+
![](Record_System *self, Socket sock, int level, int optname, const void *optval,
411+
size_t optlen) { return 0; },
411412
};
412413

413414
static constexpr Random_Funcs record_random_funcs = {

toxcore/LAN_discovery.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
148148
ifc.ifc_buf = (char *)i_faces;
149149
ifc.ifc_len = sizeof(i_faces);
150150

151-
if (ioctl(sock.sock, SIOCGIFCONF, &ifc) < 0) {
151+
if (ioctl(net_socket_to_native(sock), SIOCGIFCONF, &ifc) < 0) {
152152
kill_sock(ns, sock);
153153
free(broadcast);
154154
return nullptr;
@@ -163,7 +163,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
163163

164164
for (int i = 0; i < n; ++i) {
165165
/* there are interfaces with are incapable of broadcast */
166-
if (ioctl(sock.sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) {
166+
if (ioctl(net_socket_to_native(sock), SIOCGIFBRDADDR, &i_faces[i]) < 0) {
167167
continue;
168168
}
169169

0 commit comments

Comments
 (0)