Skip to content

Commit

Permalink
[libc] Fix send and recv functions
Browse files Browse the repository at this point in the history
There were some errors in the implementation. Oops. This patch fixes
those.
  • Loading branch information
michaelrj-google committed Oct 2, 2024
1 parent 076392b commit 0a13ec5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 45 deletions.
16 changes: 8 additions & 8 deletions libc/src/sys/socket/linux/recv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@

#include "src/sys/socket/recv.h"

#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

#include "hdr/types/socklen_t.h"
#include "hdr/types/ssize_t.h"
#include "hdr/types/struct_sockaddr.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/sanitizer.h"
#include "src/errno/libc_errno.h"
#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(ssize_t, recv,
(int sockfd, const void *buf, size_t len, int flags)) {
#ifdef SYS_recv
ssize_t ret =
LIBC_NAMESPACE::syscall_impl<int>(SYS_recv, sockfd, buf, len, flags);
LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_recv, sockfd, buf, len, flags);
#elif defined(SYS_recvfrom)
ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_recvfrom, sockfd,
reinterpret_cast<long>(buf),
len, flags, nullptr, 0);
ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
SYS_recvfrom, sockfd, buf, len, flags, nullptr, nullptr);
#elif defined(SYS_socketcall)
unsigned long sockcall_args[4] = {
static_cast<unsigned long>(sockfd), reinterpret_cast<unsigned long>(buf),
static_cast<unsigned long>(len), static_cast<unsigned long>(flags)};
ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_RECV,
sockcall_args);
ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_socketcall, SYS_RECV,
sockcall_args);
#else
#error "socket and socketcall syscalls unavailable for this platform."
#endif
Expand Down
17 changes: 8 additions & 9 deletions libc/src/sys/socket/linux/recvfrom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,34 @@

#include "src/sys/socket/recvfrom.h"

#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

#include "hdr/types/socklen_t.h"
#include "hdr/types/ssize_t.h"
#include "hdr/types/struct_sockaddr.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/sanitizer.h"
#include "src/errno/libc_errno.h"
#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(ssize_t, recvfrom,
(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen)) {
const struct sockaddr *dest_addr, socklen_t *addrlen)) {
#ifdef SYS_recvfrom

ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_recvfrom, sockfd, reinterpret_cast<long>(buf), len, flags,
reinterpret_cast<long>(dest_addr), addrlen);
ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
SYS_recvfrom, sockfd, buf, len, flags, dest_addr, addrlen);
#elif defined(SYS_socketcall)
unsigned long sockcall_args[6] = {static_cast<unsigned long>(sockfd),
reinterpret_cast<unsigned long>(buf),
static_cast<unsigned long>(len),
static_cast<unsigned long>(flags),
reinterpret_cast<unsigned long>(dest_addr),
static_cast<unsigned long>(addrlen)};
ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_RECVFROM,
sockcall_args);
ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
SYS_socketcall, SYS_RECVFROM, sockcall_args);
#else
#error "socket and socketcall syscalls unavailable for this platform."
#endif
Expand Down
13 changes: 7 additions & 6 deletions libc/src/sys/socket/linux/recvmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@

#include "src/sys/socket/recvmsg.h"

#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

#include "hdr/types/ssize_t.h"
#include "hdr/types/struct_msghdr.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/sanitizer.h"
#include "src/errno/libc_errno.h"
#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(ssize_t, recvmsg,
(int sockfd, const struct msghdr *msg, int flags)) {
#ifdef SYS_recvmsg
ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_recvmsg, sockfd, reinterpret_cast<long>(msg), flags);
ssize_t ret =
LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_recvmsg, sockfd, msg, flags);
#elif defined(SYS_socketcall)
unsigned long sockcall_args[3] = {static_cast<unsigned long>(sockfd),
reinterpret_cast<unsigned long>(msg),
static_cast<unsigned long>(flags)};
ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_RECVMSG,
sockcall_args);
ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
SYS_socketcall, SYS_RECVMSG, sockcall_args);
#else
#error "socket and socketcall syscalls unavailable for this platform."
#endif
Expand Down
15 changes: 8 additions & 7 deletions libc/src/sys/socket/linux/send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,32 @@

#include "src/sys/socket/send.h"

#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

#include "hdr/types/socklen_t.h"
#include "hdr/types/ssize_t.h"
#include "hdr/types/struct_sockaddr.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(ssize_t, send,
(int sockfd, const void *buf, size_t len, int flags)) {
#ifdef SYS_send
ssize_t ret =
LIBC_NAMESPACE::syscall_impl<int>(SYS_send, sockfd, buf, len, flags);
LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_send, sockfd, buf, len, flags);
#elif defined(SYS_sendto)
ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_sendto, sockfd, reinterpret_cast<long>(buf), len, flags, nullptr, 0);
ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendto, sockfd, buf,
len, flags, nullptr, 0);
#elif defined(SYS_socketcall)
unsigned long sockcall_args[4] = {
static_cast<unsigned long>(sockfd), reinterpret_cast<unsigned long>(buf),
static_cast<unsigned long>(len), static_cast<unsigned long>(flags)};
ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_SEND,
sockcall_args);
ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_socketcall, SYS_SEND,
sockcall_args);
#else
#error "socket and socketcall syscalls unavailable for this platform."
#endif
Expand Down
13 changes: 7 additions & 6 deletions libc/src/sys/socket/linux/sendmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@

#include "src/sys/socket/sendmsg.h"

#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

#include "hdr/types/ssize_t.h"
#include "hdr/types/struct_msghdr.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(ssize_t, sendmsg,
(int sockfd, const struct msghdr *msg, int flags)) {
#ifdef SYS_sendmsg
ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_sendmsg, sockfd, reinterpret_cast<long>(msg), flags);
ssize_t ret =
LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendmsg, sockfd, msg, flags);
#elif defined(SYS_socketcall)
unsigned long sockcall_args[3] = {static_cast<unsigned long>(sockfd),
reinterpret_cast<unsigned long>(msg),
static_cast<unsigned long>(flags)};
ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_SENDMSG,
sockcall_args);
ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
SYS_socketcall, SYS_SENDMSG, sockcall_args);
#else
#error "socket and socketcall syscalls unavailable for this platform."
#endif
Expand Down
15 changes: 7 additions & 8 deletions libc/src/sys/socket/linux/sendto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,33 @@

#include "src/sys/socket/sendto.h"

#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

#include "hdr/types/socklen_t.h"
#include "hdr/types/ssize_t.h"
#include "hdr/types/struct_sockaddr.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(ssize_t, sendto,
(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen)) {
#ifdef SYS_sendto

ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_sendto, sockfd, reinterpret_cast<long>(buf), len, flags,
reinterpret_cast<long>(dest_addr), addrlen);
ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
SYS_sendto, sockfd, buf, len, flags, dest_addr, addrlen);
#elif defined(SYS_socketcall)
unsigned long sockcall_args[6] = {static_cast<unsigned long>(sockfd),
reinterpret_cast<unsigned long>(buf),
static_cast<unsigned long>(len),
static_cast<unsigned long>(flags),
reinterpret_cast<unsigned long>(dest_addr),
static_cast<unsigned long>(addrlen)};
ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_SENDTO,
sockcall_args);
ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
SYS_socketcall, SYS_SENDTO, sockcall_args);
#else
#error "socket and socketcall syscalls unavailable for this platform."
#endif
Expand Down
2 changes: 1 addition & 1 deletion libc/src/sys/socket/recvfrom.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace LIBC_NAMESPACE_DECL {

ssize_t recvfrom(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *address, socklen_t addrlen);
const struct sockaddr *address, socklen_t *addrlen);

} // namespace LIBC_NAMESPACE_DECL

Expand Down

0 comments on commit 0a13ec5

Please sign in to comment.