Skip to content

Commit

Permalink
refactor: Use strong typedef instead of struct for Socket.
Browse files Browse the repository at this point in the history
Sparse checks it. This is neater than using a struct, which has some
slightly weird syntax at times and reduces the risk of someone adding
another struct member.
  • Loading branch information
iphydf committed Feb 3, 2024
1 parent d875693 commit 7b920e1
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 36 deletions.
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
2 changes: 1 addition & 1 deletion third_party/cmp
Submodule cmp updated 1 files
+10 −10 cmp.c
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_system(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_system(sock), SIOCGIFBRDADDR, &i_faces[i]) < 0) {
continue;
}

Expand Down
18 changes: 9 additions & 9 deletions toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1009,9 +1009,9 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
struct epoll_event ev;

ev.events = EPOLLIN | EPOLLET;
ev.data.u64 = sock.sock | ((uint64_t)TCP_SOCKET_LISTENING << 32);
ev.data.u64 = net_socket_to_system(sock) | ((uint64_t)TCP_SOCKET_LISTENING << 32);

if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, sock.sock, &ev) == -1) {
if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, net_socket_to_system(sock), &ev) == -1) {
continue;
}

Expand Down Expand Up @@ -1248,7 +1248,7 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time
#undef MAX_EVENTS

for (int n = 0; n < nfds; ++n) {
const Socket sock = {(int)(events[n].data.u64 & 0xFFFFFFFF)};
const Socket sock = net_socket_from_system((int)(events[n].data.u64 & 0xFFFFFFFF));
const int status = (events[n].data.u64 >> 32) & 0xFF;
const int index = events[n].data.u64 >> 40;

Expand Down Expand Up @@ -1306,9 +1306,9 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time

ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;

ev.data.u64 = sock_new.sock | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40);
ev.data.u64 = net_socket_to_system(sock_new) | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40);

if (epoll_ctl(tcp_server->efd, EPOLL_CTL_ADD, sock_new.sock, &ev) == -1) {
if (epoll_ctl(tcp_server->efd, EPOLL_CTL_ADD, net_socket_to_system(sock_new), &ev) == -1) {
LOGGER_DEBUG(tcp_server->logger, "new connection %d was dropped due to epoll error %d", index, net_error());
kill_tcp_secure_connection(&tcp_server->incoming_connection_queue[index_new]);
continue;
Expand All @@ -1324,9 +1324,9 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time
if (index_new != -1) {
LOGGER_TRACE(tcp_server->logger, "incoming connection %d was accepted as %d", index, index_new);
events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP;
events[n].data.u64 = sock.sock | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40);
events[n].data.u64 = net_socket_to_system(sock) | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40);

if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, sock.sock, &events[n]) == -1) {
if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, net_socket_to_system(sock), &events[n]) == -1) {
LOGGER_DEBUG(tcp_server->logger, "incoming connection %d was dropped due to epoll error %d", index, net_error());
kill_tcp_secure_connection(&tcp_server->unconfirmed_connection_queue[index_new]);
break;
Expand All @@ -1342,9 +1342,9 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time
if (index_new != -1) {
LOGGER_TRACE(tcp_server->logger, "unconfirmed connection %d was confirmed as %d", index, index_new);
events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP;
events[n].data.u64 = sock.sock | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40);
events[n].data.u64 = net_socket_to_system(sock) | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40);

if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, sock.sock, &events[n]) == -1) {
if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, net_socket_to_system(sock), &events[n]) == -1) {
// remove from confirmed connections
LOGGER_DEBUG(tcp_server->logger, "unconfirmed connection %d was dropped due to epoll error %d", index, net_error());
kill_accepted(tcp_server, index_new);
Expand Down
8 changes: 8 additions & 0 deletions toxcore/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@

#define nullable(...)

#ifdef SPARSE
#define bitwise __attribute__((bitwise))
#define force __attribute__((force))
#else
#define bitwise
#define force
#endif

//!TOKSTYLE+

#endif /* C_TOXCORE_TOXCORE_ATTRIBUTES_H */
48 changes: 27 additions & 21 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,19 @@ IP6 get_ip6_loopback(void)
#define INVALID_SOCKET (-1)
#endif /* OS_WIN32 */

int net_socket_to_system(Socket sock)
{
return (force int)sock;
}

Socket net_socket_from_system(int sock)
{
return (force Socket)sock;
}

Socket net_invalid_socket(void)
{
const Socket invalid_socket = { (int)INVALID_SOCKET };
return invalid_socket;
return net_socket_from_system(INVALID_SOCKET);
}

Family net_family_unspec(void)
Expand Down Expand Up @@ -466,8 +475,7 @@ bool net_family_is_tox_tcp_ipv6(Family family)

bool sock_valid(Socket sock)
{
const Socket invalid_socket = net_invalid_socket();
return sock.sock != invalid_socket.sock;
return sock != net_invalid_socket();
}

struct Network_Addr {
Expand Down Expand Up @@ -623,13 +631,13 @@ void os_network_deinit(const Network *ns)
non_null()
static int net_setsockopt(const Network *ns, Socket sock, int level, int optname, const void *optval, size_t optlen)
{
return ns->funcs->setsockopt(ns->obj, sock.sock, level, optname, optval, optlen);
return ns->funcs->setsockopt(ns->obj, net_socket_to_system(sock), level, optname, optval, optlen);
}

non_null()
static int net_getsockopt(const Network *ns, Socket sock, int level, int optname, void *optval, size_t *optlen)
{
return ns->funcs->getsockopt(ns->obj, sock.sock, level, optname, optval, optlen);
return ns->funcs->getsockopt(ns->obj, net_socket_to_system(sock), level, optname, optval, optlen);
}

non_null()
Expand Down Expand Up @@ -804,7 +812,7 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu
int net_send(const Network *ns, const Logger *log,
Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port)
{
const int res = ns->funcs->send(ns->obj, sock.sock, buf, len);
const int res = ns->funcs->send(ns->obj, net_socket_to_system(sock), buf, len);
loglogdata(log, "T=>", buf, len, ip_port, res);
return res;
}
Expand All @@ -814,13 +822,13 @@ static int net_sendto(
const Network *ns,
Socket sock, const uint8_t *buf, size_t len, const Network_Addr *addr, const IP_Port *ip_port)
{
return ns->funcs->sendto(ns->obj, sock.sock, buf, len, addr);
return ns->funcs->sendto(ns->obj, net_socket_to_system(sock), buf, len, addr);
}

int net_recv(const Network *ns, const Logger *log,
Socket sock, uint8_t *buf, size_t len, const IP_Port *ip_port)
{
const int res = ns->funcs->recv(ns->obj, sock.sock, buf, len);
const int res = ns->funcs->recv(ns->obj, net_socket_to_system(sock), buf, len);
loglogdata(log, "=>T", buf, len, ip_port, res);
return res;
}
Expand All @@ -829,35 +837,34 @@ non_null()
static int net_recvfrom(const Network *ns,
Socket sock, uint8_t *buf, size_t len, Network_Addr *addr)
{
return ns->funcs->recvfrom(ns->obj, sock.sock, buf, len, addr);
return ns->funcs->recvfrom(ns->obj, net_socket_to_system(sock), buf, len, addr);
}

int net_listen(const Network *ns, Socket sock, int backlog)
{
return ns->funcs->listen(ns->obj, sock.sock, backlog);
return ns->funcs->listen(ns->obj, net_socket_to_system(sock), backlog);
}

non_null()
static int net_bind(const Network *ns, Socket sock, const Network_Addr *addr)
{
return ns->funcs->bind(ns->obj, sock.sock, addr);
return ns->funcs->bind(ns->obj, net_socket_to_system(sock), addr);
}

Socket net_accept(const Network *ns, Socket sock)
{
const Socket newsock = {ns->funcs->accept(ns->obj, sock.sock)};
return newsock;
return net_socket_from_system(ns->funcs->accept(ns->obj, net_socket_to_system(sock)));
}

/** Close the socket. */
void kill_sock(const Network *ns, Socket sock)
{
ns->funcs->close(ns->obj, sock.sock);
ns->funcs->close(ns->obj, net_socket_to_system(sock));
}

bool set_socket_nonblock(const Network *ns, Socket sock)
{
return ns->funcs->socket_nonblock(ns->obj, sock.sock, true) == 0;
return ns->funcs->socket_nonblock(ns->obj, net_socket_to_system(sock), true) == 0;
}

bool set_socket_nosigpipe(const Network *ns, Socket sock)
Expand Down Expand Up @@ -1946,10 +1953,10 @@ bool net_connect(const Memory *mem, const Logger *log, Socket sock, const IP_Por

Ip_Ntoa ip_str;
LOGGER_DEBUG(log, "connecting socket %d to %s:%d",
(int)sock.sock, net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port));
net_socket_to_system(sock), net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port));
errno = 0;

if (connect(sock.sock, (struct sockaddr *)&addr, addrsize) == -1) {
if (connect(net_socket_to_system(sock), (struct sockaddr *)&addr, addrsize) == -1) {
const int error = net_error();

// Non-blocking socket: "Operation in progress" means it's connecting.
Expand Down Expand Up @@ -2109,13 +2116,12 @@ Socket net_socket(const Network *ns, Family domain, int type, int protocol)
const int platform_domain = make_family(domain);
const int platform_type = make_socktype(type);
const int platform_prot = make_proto(protocol);
const Socket sock = {ns->funcs->socket(ns->obj, platform_domain, platform_type, platform_prot)};
return sock;
return net_socket_from_system(ns->funcs->socket(ns->obj, platform_domain, platform_type, platform_prot));
}

uint16_t net_socket_data_recv_buffer(const Network *ns, Socket sock)
{
const int count = ns->funcs->recvbuf(ns->obj, sock.sock);
const int count = ns->funcs->recvbuf(ns->obj, net_socket_to_system(sock));
return (uint16_t)max_s32(0, min_s32(count, UINT16_MAX));
}

Expand Down
7 changes: 4 additions & 3 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@ typedef struct IP_Port {
uint16_t port;
} IP_Port;

typedef struct Socket {
int sock;
} Socket;
typedef bitwise int Socket;

int net_socket_to_system(Socket sock);
Socket net_socket_from_system(int sock);

non_null()
Socket net_socket(const Network *ns, Family domain, int type, int protocol);
Expand Down

0 comments on commit 7b920e1

Please sign in to comment.