Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use strong typedef instead of struct for Socket. #2640

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CheckOptions:
- key: readability-identifier-naming.MacroDefinitionCase
value: UPPER_CASE
- key: readability-identifier-naming.MacroDefinitionIgnoredRegexp
value: "^_.*|nullable|non_null|nullptr|static_assert|ck_.*"
value: "^_.*|bitwise|force|nullable|non_null|nullptr|static_assert|ck_.*"
- key: readability-identifier-naming.ParameterCase
value: lower_case
- key: readability-identifier-naming.StructCase
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ jobs:

analysis:
strategy:
fail-fast: false
matrix:
tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, misra, modules, rpm, slimcc, tcc, tokstyle]
tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, misra, modules, rpm, slimcc, sparse, tcc, tokstyle]
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
Expand Down
1 change: 1 addition & 0 deletions other/docker/sparse/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!/Makefile
70 changes: 70 additions & 0 deletions other/docker/sparse/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
SOURCES := $(wildcard tox*/*.c tox*/*/*.c) \
third_party/cmp/cmp.c
OBJECTS := $(SOURCES:.c=.o)

CFLAGS := $(shell pkg-config --cflags libsodium opus vpx)
CPPFLAGS := -DSPARSE -DTCP_SERVER_USE_EPOLL=1 -DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE

SPARSE_FLAGS := \
-Wsparse-error \
-Wpedantic \
-Waddress \
-Waddress-space \
-Wbitwise \
-Wbitwise-pointer \
-Wcast-from-as \
-Wcast-to-as \
-Wcast-truncate \
-Wconstant-suffix \
-Wconstexpr-not-const \
-Wcontext \
-Wdecl \
-Wdefault-bitfield-sign \
-Wdesignated-init \
-Wdo-while \
-Wenum-mismatch \
-Wexternal-function-has-definition \
-Wflexible-array-array \
-Wflexible-array-nested \
-Wflexible-array-union \
-Wimplicit-int \
-Winit-cstring \
-Wint-to-pointer-cast \
-Wmemcpy-max-count \
-Wnon-pointer-null \
-Wnewline-eof \
-Wold-initializer \
-Wold-style-definition \
-Wone-bit-signed-bitfield \
-Woverride-init \
-Woverride-init-all \
-Wparen-string \
-Wpast-deep-designator \
-Wpedantic \
-Wpointer-to-int-cast \
-Wptr-subtraction-blows \
-Wreturn-void \
-Wshadow \
-Wshift-count-negative \
-Wshift-count-overflow \
-Wsizeof-bool \
-Wstrict-prototypes \
-Wpointer-arith \
-Wsparse-error \
-Wtautological-compare \
-Wtransparent-union \
-Wtypesign \
-Wundef \
-Wuninitialized \
-Wunion-cast \
-Wvla

SMATCH_FLAGS := $(foreach i,$(shell smatch --show-checks | grep -o 'check_.*'),--enable=$i)

analyse: $(OBJECTS)

%.o: %.c
@echo "Processing $<"
@sparse $(CFLAGS) $(CPPFLAGS) $(SPARSE_FLAGS) $<
# @smatch $(CFLAGS) $(CPPFLAGS) $(SMATCH_FLAGS) $<
# @sparse-llvm $(CFLAGS) $(CPPFLAGS) $< > /dev/null
1 change: 1 addition & 0 deletions other/docker/sparse/local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CFLAGS=-O3 -g -Wno-discarded-qualifiers -Wno-format-truncation -Wno-stringop-truncation -Wno-uninitialized -Wno-unused -Wno-unused-result
6 changes: 6 additions & 0 deletions other/docker/sparse/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -eux
BUILD=sparse
other/docker/sources/build
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .
35 changes: 35 additions & 0 deletions other/docker/sparse/sparse.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM toxchat/c-toxcore:sources AS sources
FROM ubuntu:22.04

RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
ca-certificates \
creduce \
g++ \
gcc \
git \
libc-dev \
libopus-dev \
libsodium-dev \
libsqlite3-dev \
libssl-dev \
libvpx-dev \
llvm-dev \
make \
pkg-config \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /work/smatch
RUN git clone --depth=1 https://repo.or.cz/smatch.git /work/smatch
COPY other/docker/sparse/local.mk /work/smatch/local.mk
RUN make install -j4 PREFIX=/usr/local

WORKDIR /work/c-toxcore
COPY --from=sources /src/ /work/c-toxcore
#COPY other/make_single_file /work/c-toxcore/other/
#RUN other/make_single_file auto_tests/tox_new_test.c > crash.c
#RUN sparsec $(pkg-config --cflags --libs libsodium opus vpx) crash.c

COPY other/docker/sparse/Makefile /work/c-toxcore/
RUN make -j4
97 changes: 49 additions & 48 deletions testing/fuzzing/fuzz_support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,25 @@
};

static constexpr Network_Funcs fuzz_network_funcs = {
/* .close = */ ![](Fuzz_System *self, int sock) { return 0; },
/* .accept = */ ![](Fuzz_System *self, int sock) { return 1337; },
/* .bind = */ ![](Fuzz_System *self, int sock, const Network_Addr *addr) { return 0; },
/* .listen = */ ![](Fuzz_System *self, int sock, int backlog) { return 0; },
/* .close = */ ![](Fuzz_System *self, Socket sock) { return 0; },
/* .accept = */ ![](Fuzz_System *self, Socket sock) { return Socket{1337}; },

Check warning on line 111 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L111

Added line #L111 was not covered by tests
/* .bind = */ ![](Fuzz_System *self, Socket sock, const Network_Addr *addr) { return 0; },
/* .listen = */ ![](Fuzz_System *self, Socket sock, int backlog) { return 0; },
/* .recvbuf = */
![](Fuzz_System *self, int sock) {
assert(sock == 42 || sock == 1337);
![](Fuzz_System *self, Socket sock) {
assert(sock.value == 42 || sock.value == 1337);
const size_t count = random_u16(self->rng.get());
return static_cast<int>(std::min(count, self->data.size()));
},
/* .recv = */
![](Fuzz_System *self, int sock, uint8_t *buf, size_t len) {
assert(sock == 42 || sock == 1337);
![](Fuzz_System *self, Socket sock, uint8_t *buf, size_t len) {
assert(sock.value == 42 || sock.value == 1337);
// Receive data from the fuzzer.
return recv_common(self->data, buf, len);
},
/* .recvfrom = */
![](Fuzz_System *self, int sock, uint8_t *buf, size_t len, Network_Addr *addr) {
assert(sock == 42 || sock == 1337);
![](Fuzz_System *self, Socket sock, uint8_t *buf, size_t len, Network_Addr *addr) {
assert(sock.value == 42 || sock.value == 1337);

addr->addr = sockaddr_storage{};
// Dummy Addr
Expand All @@ -140,26 +140,26 @@
return recv_common(self->data, buf, len);
},
/* .send = */
![](Fuzz_System *self, int sock, const uint8_t *buf, size_t len) {
assert(sock == 42 || sock == 1337);
![](Fuzz_System *self, Socket sock, const uint8_t *buf, size_t len) {
assert(sock.value == 42 || sock.value == 1337);
// Always succeed.
return static_cast<int>(len);
},
/* .sendto = */
![](Fuzz_System *self, int sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {
assert(sock == 42 || sock == 1337);
![](Fuzz_System *self, Socket sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {
assert(sock.value == 42 || sock.value == 1337);
// Always succeed.
return static_cast<int>(len);
},
/* .socket = */ ![](Fuzz_System *self, int domain, int type, int proto) { return 42; },
/* .socket_nonblock = */ ![](Fuzz_System *self, int sock, bool nonblock) { return 0; },
/* .socket = */ ![](Fuzz_System *self, int domain, int type, int proto) { return Socket{42}; },
/* .socket_nonblock = */ ![](Fuzz_System *self, Socket sock, bool nonblock) { return 0; },
/* .getsockopt = */
![](Fuzz_System *self, int sock, int level, int optname, void *optval, size_t *optlen) {
![](Fuzz_System *self, Socket sock, int level, int optname, void *optval, size_t *optlen) {
std::memset(optval, 0, *optlen);
return 0;
},
/* .setsockopt = */
![](Fuzz_System *self, int sock, int level, int optname, const void *optval, size_t optlen) {
![](Fuzz_System *self, Socket sock, int level, int optname, const void *optval, size_t optlen) {
return 0;
},
};
Expand Down Expand Up @@ -221,42 +221,42 @@
};

static constexpr Network_Funcs null_network_funcs = {
/* .close = */ ![](Null_System *self, int sock) { return 0; },
/* .accept = */ ![](Null_System *self, int sock) { return 1337; },
/* .bind = */ ![](Null_System *self, int sock, const Network_Addr *addr) { return 0; },
/* .listen = */ ![](Null_System *self, int sock, int backlog) { return 0; },
/* .recvbuf = */ ![](Null_System *self, int sock) { return 0; },
/* .close = */ ![](Null_System *self, Socket sock) { return 0; },
/* .accept = */ ![](Null_System *self, Socket sock) { return Socket{1337}; },

Check warning on line 225 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L225

Added line #L225 was not covered by tests
/* .bind = */ ![](Null_System *self, Socket sock, const Network_Addr *addr) { return 0; },
/* .listen = */ ![](Null_System *self, Socket sock, int backlog) { return 0; },
/* .recvbuf = */ ![](Null_System *self, Socket sock) { return 0; },

Check warning on line 228 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L227-L228

Added lines #L227 - L228 were not covered by tests
/* .recv = */
![](Null_System *self, int sock, uint8_t *buf, size_t len) {
![](Null_System *self, Socket sock, uint8_t *buf, size_t len) {

Check warning on line 230 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L230

Added line #L230 was not covered by tests
// Always fail.
errno = ENOMEM;
return -1;
},
/* .recvfrom = */
![](Null_System *self, int sock, uint8_t *buf, size_t len, Network_Addr *addr) {
![](Null_System *self, Socket sock, uint8_t *buf, size_t len, Network_Addr *addr) {

Check warning on line 236 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L236

Added line #L236 was not covered by tests
// Always fail.
errno = ENOMEM;
return -1;
},
/* .send = */
![](Null_System *self, int sock, const uint8_t *buf, size_t len) {
![](Null_System *self, Socket sock, const uint8_t *buf, size_t len) {

Check warning on line 242 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L242

Added line #L242 was not covered by tests
// Always succeed.
return static_cast<int>(len);
},
/* .sendto = */
![](Null_System *self, int sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {
![](Null_System *self, Socket sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {

Check warning on line 247 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L247

Added line #L247 was not covered by tests
// Always succeed.
return static_cast<int>(len);
},
/* .socket = */ ![](Null_System *self, int domain, int type, int proto) { return 42; },
/* .socket_nonblock = */ ![](Null_System *self, int sock, bool nonblock) { return 0; },
/* .socket = */ ![](Null_System *self, int domain, int type, int proto) { return Socket{42}; },
/* .socket_nonblock = */ ![](Null_System *self, Socket sock, bool nonblock) { return 0; },
/* .getsockopt = */
![](Null_System *self, int sock, int level, int optname, void *optval, size_t *optlen) {
![](Null_System *self, Socket sock, int level, int optname, void *optval, size_t *optlen) {
std::memset(optval, 0, *optlen);
return 0;
},
/* .setsockopt = */
![](Null_System *self, int sock, int level, int optname, const void *optval, size_t optlen) {
![](Null_System *self, Socket sock, int level, int optname, const void *optval, size_t optlen) {
return 0;
},
};
Expand Down Expand Up @@ -327,10 +327,10 @@
};

static constexpr Network_Funcs record_network_funcs = {
/* .close = */ ![](Record_System *self, int sock) { return 0; },
/* .accept = */ ![](Record_System *self, int sock) { return 2; },
/* .close = */ ![](Record_System *self, Socket sock) { return 0; },
/* .accept = */ ![](Record_System *self, Socket sock) { return Socket{2}; },

Check warning on line 331 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L330-L331

Added lines #L330 - L331 were not covered by tests
/* .bind = */
![](Record_System *self, int sock, const Network_Addr *addr) {
![](Record_System *self, Socket sock, const Network_Addr *addr) {

Check warning on line 333 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L333

Added line #L333 was not covered by tests
const uint16_t port = get_port(addr);
if (self->global_.bound.find(port) != self->global_.bound.end()) {
errno = EADDRINUSE;
Expand All @@ -340,17 +340,17 @@
self->port = port;
return 0;
},
/* .listen = */ ![](Record_System *self, int sock, int backlog) { return 0; },
/* .recvbuf = */ ![](Record_System *self, int sock) { return 0; },
/* .listen = */ ![](Record_System *self, Socket sock, int backlog) { return 0; },
/* .recvbuf = */ ![](Record_System *self, Socket sock) { return 0; },

Check warning on line 344 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L343-L344

Added lines #L343 - L344 were not covered by tests
/* .recv = */
![](Record_System *self, int sock, uint8_t *buf, size_t len) {
![](Record_System *self, Socket sock, uint8_t *buf, size_t len) {

Check warning on line 346 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L346

Added line #L346 was not covered by tests
// Always fail.
errno = ENOMEM;
return -1;
},
/* .recvfrom = */
![](Record_System *self, int sock, uint8_t *buf, size_t len, Network_Addr *addr) {
assert(sock == 42);
![](Record_System *self, Socket sock, uint8_t *buf, size_t len, Network_Addr *addr) {
assert(sock.value == 42);

Check warning on line 353 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L352-L353

Added lines #L352 - L353 were not covered by tests
if (self->recvq.empty()) {
self->push("\xff\xff");
errno = EWOULDBLOCK;
Expand Down Expand Up @@ -385,29 +385,30 @@
return static_cast<int>(recvlen);
},
/* .send = */
![](Record_System *self, int sock, const uint8_t *buf, size_t len) {
![](Record_System *self, Socket sock, const uint8_t *buf, size_t len) {

Check warning on line 388 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L388

Added line #L388 was not covered by tests
// Always succeed.
return static_cast<int>(len);
},
/* .sendto = */
![](Record_System *self, int sock, const uint8_t *buf, size_t len, const Network_Addr *addr) {
assert(sock == 42);
![](Record_System *self, Socket sock, const uint8_t *buf, size_t len,
const Network_Addr *addr) {
assert(sock.value == 42);

Check warning on line 395 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L394-L395

Added lines #L394 - L395 were not covered by tests
auto backend = self->global_.bound.find(get_port(addr));
assert(backend != self->global_.bound.end());
backend->second->receive(self->port, buf, len);
return static_cast<int>(len);
},
/* .socket = */ ![](Record_System *self, int domain, int type, int proto) { return 42; },
/* .socket_nonblock = */ ![](Record_System *self, int sock, bool nonblock) { return 0; },
/* .socket = */
![](Record_System *self, int domain, int type, int proto) { return Socket{42}; },
/* .socket_nonblock = */ ![](Record_System *self, Socket sock, bool nonblock) { return 0; },

Check warning on line 403 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L402-L403

Added lines #L402 - L403 were not covered by tests
/* .getsockopt = */
![](Record_System *self, int sock, int level, int optname, void *optval, size_t *optlen) {
![](Record_System *self, Socket sock, int level, int optname, void *optval, size_t *optlen) {

Check warning on line 405 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L405

Added line #L405 was not covered by tests
std::memset(optval, 0, *optlen);
return 0;
},
/* .setsockopt = */
![](Record_System *self, int sock, int level, int optname, const void *optval, size_t optlen) {
return 0;
},
![](Record_System *self, Socket sock, int level, int optname, const void *optval,
size_t optlen) { return 0; },

Check warning on line 411 in testing/fuzzing/fuzz_support.cc

View check run for this annotation

Codecov / codecov/patch

testing/fuzzing/fuzz_support.cc#L411

Added line #L411 was not covered by tests
};

static constexpr Random_Funcs record_random_funcs = {
Expand Down
4 changes: 2 additions & 2 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
ifc.ifc_buf = (char *)i_faces;
ifc.ifc_len = sizeof(i_faces);

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

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

Expand Down
Loading
Loading