[libc-commits] [libc] [libc] Create socketcall helper for Linux syscall wrappers (PR #196903)

via libc-commits libc-commits at lists.llvm.org
Mon May 11 01:42:30 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Pavel Labath (labath)

<details>
<summary>Changes</summary>

This patch implements a generic socketcall variadic wrapper template under linux/syscall_wrappers/socketcall.h and refactors socket functions to use it.

Assisted by Gemini.

---

Patch is 28.67 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/196903.diff


18 Files Affected:

- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+17) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/accept.h (+3-7) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/accept4.h (+2-6) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/connect.h (+3-7) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/getsockopt.h (+3-6) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/listen.h (+3-5) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/setsockopt.h (+3-6) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/shutdown.h (+3-5) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/socketcall.h (+47) 
- (modified) libc/src/sys/socket/linux/bind.cpp (+4-9) 
- (modified) libc/src/sys/socket/linux/recv.cpp (+6-10) 
- (modified) libc/src/sys/socket/linux/recvfrom.cpp (+6-11) 
- (modified) libc/src/sys/socket/linux/recvmsg.cpp (+4-8) 
- (modified) libc/src/sys/socket/linux/send.cpp (+6-10) 
- (modified) libc/src/sys/socket/linux/sendmsg.cpp (+4-8) 
- (modified) libc/src/sys/socket/linux/sendto.cpp (+6-11) 
- (modified) libc/src/sys/socket/linux/socket.cpp (+4-9) 
- (modified) libc/src/sys/socket/linux/socketpair.cpp (+4-8) 


``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 6418d6f83dbfa..4e29e310f5799 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -37,12 +37,24 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  socketcall
+  HDRS
+    socketcall.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.include.sys_syscall
+)
+
 add_header_library(
   accept
   HDRS
     accept.h
   DEPENDS
     libc.src.__support.OSUtil.linux.syscall_wrappers.accept
+    libc.src.__support.OSUtil.linux.syscall_wrappers.socketcall
     libc.src.__support.common
     libc.src.__support.error_or
     libc.src.__support.libc_errno
@@ -57,6 +69,7 @@ add_header_library(
   HDRS
     accept4.h
   DEPENDS
+    libc.src.__support.OSUtil.linux.syscall_wrappers.socketcall
     libc.src.__support.common
     libc.src.__support.error_or
     libc.src.__support.libc_errno
@@ -72,6 +85,7 @@ add_header_library(
     connect.h
   DEPENDS
     libc.src.__support.OSUtil.linux.syscall_wrappers.connect
+    libc.src.__support.OSUtil.linux.syscall_wrappers.socketcall
     libc.src.__support.common
     libc.src.__support.error_or
     libc.src.__support.libc_errno
@@ -86,6 +100,7 @@ add_header_library(
   HDRS
     getsockopt.h
   DEPENDS
+    libc.src.__support.OSUtil.linux.syscall_wrappers.socketcall
     libc.src.__support.common
     libc.src.__support.error_or
     libc.src.__support.libc_errno
@@ -99,6 +114,7 @@ add_header_library(
   HDRS
     listen.h
   DEPENDS
+    libc.src.__support.OSUtil.linux.syscall_wrappers.socketcall
     libc.src.__support.common
     libc.src.__support.error_or
     libc.src.__support.libc_errno
@@ -124,6 +140,7 @@ add_header_library(
   HDRS
     setsockopt.h
   DEPENDS
+    libc.src.__support.OSUtil.linux.syscall_wrappers.socketcall
     libc.src.__support.common
     libc.src.__support.error_or
     libc.src.__support.libc_errno
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/accept.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/accept.h
index e661f4df1daab..368eaf3164179 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/accept.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/accept.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_ACCEPT_H
 
 #include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -25,14 +26,9 @@ namespace linux_syscalls {
 LIBC_INLINE ErrorOr<int> accept(int sockfd, struct sockaddr *addr,
                                 socklen_t *addrlen) {
 #ifdef SYS_accept
-  int ret =
-      LIBC_NAMESPACE::syscall_impl<int>(SYS_accept, sockfd, addr, addrlen);
+  int ret = syscall_impl<int>(SYS_accept, sockfd, addr, addrlen);
 #elif defined(SYS_socketcall)
-  unsigned long sockcall_args[3] = {static_cast<unsigned long>(sockfd),
-                                    reinterpret_cast<unsigned long>(addr),
-                                    reinterpret_cast<unsigned long>(addrlen)};
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_ACCEPT,
-                                              sockcall_args);
+  int ret = socketcall<int>(SYS_ACCEPT, sockfd, addr, addrlen);
 #else
 #error "accept and socketcall syscalls unavailable for this platform."
 #endif
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/accept4.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/accept4.h
index ebe82f189af50..b43de526dab0e 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/accept4.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/accept4.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_ACCEPT4_H
 
 #include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -28,12 +29,7 @@ LIBC_INLINE ErrorOr<int> accept4(int sockfd, struct sockaddr *addr,
   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_accept4, sockfd, addr,
                                               addrlen, flags);
 #elif defined(SYS_socketcall)
-  unsigned long sockcall_args[4] = {static_cast<unsigned long>(sockfd),
-                                    reinterpret_cast<unsigned long>(addr),
-                                    reinterpret_cast<unsigned long>(addrlen),
-                                    static_cast<unsigned long>(flags)};
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_ACCEPT4,
-                                              sockcall_args);
+  int ret = socketcall<int>(SYS_ACCEPT4, sockfd, addr, addrlen, flags);
 #else
 #error "accept4 and socketcall syscalls unavailable for this platform."
 #endif
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/connect.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/connect.h
index 6974546630b00..094cdc79743b1 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/connect.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/connect.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CONNECT_H
 
 #include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -25,14 +26,9 @@ namespace linux_syscalls {
 LIBC_INLINE ErrorOr<int> connect(int sockfd, const struct sockaddr *addr,
                                  socklen_t addrlen) {
 #ifdef SYS_connect
-  int ret =
-      LIBC_NAMESPACE::syscall_impl<int>(SYS_connect, sockfd, addr, addrlen);
+  int ret = syscall_impl<int>(SYS_connect, sockfd, addr, addrlen);
 #elif defined(SYS_socketcall)
-  unsigned long sockcall_args[3] = {static_cast<unsigned long>(sockfd),
-                                    reinterpret_cast<unsigned long>(addr),
-                                    static_cast<unsigned long>(addrlen)};
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_CONNECT,
-                                              sockcall_args);
+  int ret = socketcall<int>(SYS_CONNECT, sockfd, addr, addrlen);
 #else
 #error "socket and socketcall syscalls unavailable for this platform."
 #endif
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockopt.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockopt.h
index 623ba58ebb2ed..7498330a15ea2 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockopt.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockopt.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETSOCKOPT_H
 
 #include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -27,12 +28,8 @@ LIBC_INLINE ErrorOr<int> getsockopt(int sockfd, int level, int optname,
   int ret =
       syscall_impl<int>(SYS_getsockopt, sockfd, level, optname, optval, optlen);
 #elif defined(SYS_socketcall)
-  unsigned long sockcall_args[5] = {static_cast<unsigned long>(sockfd),
-                                    static_cast<unsigned long>(level),
-                                    static_cast<unsigned long>(optname),
-                                    reinterpret_cast<unsigned long>(optval),
-                                    reinterpret_cast<unsigned long>(optlen)};
-  int ret = syscall_impl<int>(SYS_socketcall, SYS_GETSOCKOPT, sockcall_args);
+  int ret =
+      socketcall<int>(SYS_GETSOCKOPT, sockfd, level, optname, optval, optlen);
 #else
 #error "getsockopt and socketcall syscalls unavailable for this platform."
 #endif
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/listen.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/listen.h
index 9de54ce0a9a9f..cbc0c34cab33f 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/listen.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/listen.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_LISTEN_H
 
 #include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -22,12 +23,9 @@ namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> listen(int sockfd, int backlog) {
 #ifdef SYS_listen
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_listen, sockfd, backlog);
+  int ret = syscall_impl<int>(SYS_listen, sockfd, backlog);
 #elif defined(SYS_socketcall)
-  unsigned long sockcall_args[2] = {static_cast<unsigned long>(sockfd),
-                                    static_cast<unsigned long>(backlog)};
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_LISTEN,
-                                              sockcall_args);
+  int ret = socketcall<int>(SYS_LISTEN, sockfd, backlog);
 #else
 #error "listen and socketcall syscalls unavailable for this platform."
 #endif
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/setsockopt.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/setsockopt.h
index d16c397bba6ac..d5f8e28f058dc 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/setsockopt.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/setsockopt.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SETSOCKOPT_H
 
 #include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -27,12 +28,8 @@ LIBC_INLINE ErrorOr<int> setsockopt(int sockfd, int level, int optname,
   int ret =
       syscall_impl<int>(SYS_setsockopt, sockfd, level, optname, optval, optlen);
 #elif defined(SYS_socketcall)
-  unsigned long sockcall_args[5] = {static_cast<unsigned long>(sockfd),
-                                    static_cast<unsigned long>(level),
-                                    static_cast<unsigned long>(optname),
-                                    reinterpret_cast<unsigned long>(optval),
-                                    static_cast<unsigned long>(optlen)};
-  int ret = syscall_impl<int>(SYS_socketcall, SYS_SETSOCKOPT, sockcall_args);
+  int ret =
+      socketcall<int>(SYS_SETSOCKOPT, sockfd, level, optname, optval, optlen);
 #else
 #error "setsockopt and socketcall syscalls unavailable for this platform."
 #endif
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/shutdown.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/shutdown.h
index 2a9e92364f637..bdab8a82e6a1f 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/shutdown.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/shutdown.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SHUTDOWN_H
 
 #include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -22,12 +23,9 @@ namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> shutdown(int sockfd, int how) {
 #ifdef SYS_shutdown
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_shutdown, sockfd, how);
+  int ret = syscall_impl<int>(SYS_shutdown, sockfd, how);
 #elif defined(SYS_socketcall)
-  unsigned long sockcall_args[2] = {static_cast<unsigned long>(sockfd),
-                                    static_cast<unsigned long>(how)};
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_SHUTDOWN,
-                                              sockcall_args);
+  int ret = socketcall<int>(SYS_SHUTDOWN, sockfd, how);
 #else
 #error "shutdown and socketcall syscalls unavailable for this platform."
 #endif
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/socketcall.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/socketcall.h
new file mode 100644
index 0000000000000..c1aae0172e2a9
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/socketcall.h
@@ -0,0 +1,47 @@
+//===-- Implementation header for socketcall wrapper ------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SOCKETCALL_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SOCKETCALL_H
+
+#include "src/__support/CPP/type_traits/is_integral.h"
+#include "src/__support/CPP/type_traits/is_pointer.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+#ifdef SYS_socketcall
+
+template <typename T>
+LIBC_INLINE unsigned long socketcall_arg_cast_impl(T val) {
+  if constexpr (cpp::is_pointer_v<T>) {
+    return reinterpret_cast<unsigned long>(val);
+  } else {
+    static_assert(cpp::is_integral_v<T>, "Expected integral or pointer type.");
+    return static_cast<unsigned long>(val);
+  }
+}
+
+template <typename ReturnType, typename... Args>
+LIBC_INLINE ReturnType socketcall(int call, Args... args) {
+  unsigned long sockcall_args[sizeof...(Args)] = {
+      socketcall_arg_cast_impl(args)...};
+  return LIBC_NAMESPACE::syscall_impl<ReturnType>(SYS_socketcall, call,
+                                                  sockcall_args);
+}
+
+#endif // SYS_socketcall
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SOCKETCALL_H
diff --git a/libc/src/sys/socket/linux/bind.cpp b/libc/src/sys/socket/linux/bind.cpp
index 83a3d06f5380b..0a14d764c4d8e 100644
--- a/libc/src/sys/socket/linux/bind.cpp
+++ b/libc/src/sys/socket/linux/bind.cpp
@@ -8,9 +8,9 @@
 
 #include "src/sys/socket/bind.h"
 
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
-
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
 
@@ -23,14 +23,9 @@ LLVM_LIBC_FUNCTION(int, bind,
                    (int socket, const struct sockaddr *address,
                     socklen_t address_len)) {
 #ifdef SYS_bind
-  int ret =
-      LIBC_NAMESPACE::syscall_impl<int>(SYS_bind, socket, address, address_len);
+  int ret = syscall_impl<int>(SYS_bind, socket, address, address_len);
 #elif defined(SYS_socketcall)
-  unsigned long sockcall_args[3] = {static_cast<unsigned long>(socket),
-                                    reinterpret_cast<unsigned long>(address),
-                                    static_cast<unsigned long>(address_len)};
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_BIND,
-                                              sockcall_args);
+  int ret = socketcall(SYS_BIND, socket, address, address_len);
 #else
 #error "socket and socketcall syscalls unavailable for this platform."
 #endif
diff --git a/libc/src/sys/socket/linux/recv.cpp b/libc/src/sys/socket/linux/recv.cpp
index baf4de1b5eb54..015f870dfb8ad 100644
--- a/libc/src/sys/socket/linux/recv.cpp
+++ b/libc/src/sys/socket/linux/recv.cpp
@@ -14,7 +14,8 @@
 #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/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/sanitizer.h"
@@ -24,17 +25,12 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(ssize_t, recv,
                    (int sockfd, void *buf, size_t len, int flags)) {
 #ifdef SYS_recv
-  ssize_t ret =
-      LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_recv, sockfd, buf, len, flags);
+  ssize_t ret = syscall_impl<ssize_t>(SYS_recv, sockfd, buf, len, flags);
 #elif defined(SYS_recvfrom)
-  ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
-      SYS_recvfrom, sockfd, buf, len, flags, nullptr, nullptr);
+  ssize_t ret = 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<ssize_t>(SYS_socketcall, SYS_RECV,
-                                                      sockcall_args);
+  ssize_t ret = socketcall<ssize_t>(SYS_RECV, sockfd, buf, len, flags);
 #else
 #error "socket and socketcall syscalls unavailable for this platform."
 #endif
diff --git a/libc/src/sys/socket/linux/recvfrom.cpp b/libc/src/sys/socket/linux/recvfrom.cpp
index 3d8397b478cc4..18f8425008fc4 100644
--- a/libc/src/sys/socket/linux/recvfrom.cpp
+++ b/libc/src/sys/socket/linux/recvfrom.cpp
@@ -14,7 +14,8 @@
 #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/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/sanitizer.h"
@@ -35,17 +36,11 @@ LLVM_LIBC_FUNCTION(ssize_t, recvfrom,
   (void)srcaddr_sz; // prevent "set but not used" warning
 
 #ifdef SYS_recvfrom
-  ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
-      SYS_recvfrom, sockfd, buf, len, flags, src_addr, addrlen);
+  ssize_t ret = syscall_impl<ssize_t>(SYS_recvfrom, sockfd, buf, len, flags,
+                                      src_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>(src_addr),
-                                    static_cast<unsigned long>(addrlen)};
-  ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
-      SYS_socketcall, SYS_RECVFROM, sockcall_args);
+  ssize_t ret = socketcall<ssize_t>(SYS_RECVFROM, sockfd, buf, len, flags,
+                                    src_addr, addrlen);
 #else
 #error "socket and socketcall syscalls unavailable for this platform."
 #endif
diff --git a/libc/src/sys/socket/linux/recvmsg.cpp b/libc/src/sys/socket/linux/recvmsg.cpp
index bc6d072dbf9a1..7de70752f9294 100644
--- a/libc/src/sys/socket/linux/recvmsg.cpp
+++ b/libc/src/sys/socket/linux/recvmsg.cpp
@@ -13,7 +13,8 @@
 
 #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/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketcall.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/sanitizer.h"
@@ -22,14 +23,9 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(ssize_t, recvmsg, (int sockfd, msghdr *msg, int f...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/196903


More information about the libc-commits mailing list