[libc-commits] [libc] ee17fd7 - [libc] add socket function

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed May 3 11:01:16 PDT 2023


Author: Michael Jones
Date: 2023-05-03T11:01:11-07:00
New Revision: ee17fd7d46a914bed4619f656cc9062c40951e5c

URL: https://github.com/llvm/llvm-project/commit/ee17fd7d46a914bed4619f656cc9062c40951e5c
DIFF: https://github.com/llvm/llvm-project/commit/ee17fd7d46a914bed4619f656cc9062c40951e5c.diff

LOG: [libc] add socket function

This patch adds the function "socket" from the header "sys/socket". It's
a simple syscall wrapper, and I plan on adding the related functions in
a followup patch.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149622

Added: 
    libc/include/llvm-libc-types/sa_family_t.h
    libc/include/llvm-libc-types/struct_sockaddr.h
    libc/src/sys/socket/CMakeLists.txt
    libc/src/sys/socket/linux/CMakeLists.txt
    libc/src/sys/socket/linux/socket.cpp
    libc/src/sys/socket/socket.h
    libc/test/src/sys/socket/CMakeLists.txt
    libc/test/src/sys/socket/linux/CMakeLists.txt
    libc/test/src/sys/socket/linux/socket_test.cpp

Modified: 
    libc/config/linux/api.td
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-macros/linux/sys-socket-macros.h
    libc/include/llvm-libc-types/CMakeLists.txt
    libc/spec/linux.td
    libc/spec/posix.td
    libc/src/sys/CMakeLists.txt
    libc/test/src/sys/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 8fac4e041f1bc..4aa06c7845e1f 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -203,6 +203,10 @@ def SysSelectAPI : PublicAPI<"sys/select.h"> {
                "struct timeval"];
 }
 
+def SysSocketAPI : PublicAPI<"sys/socket.h"> {
+  let Types = ["struct sockaddr", "sa_family_t"];
+}
+
 def SysResourceAPI : PublicAPI<"sys/resource.h"> {
   let Types = ["rlim_t", "struct rlimit"];
 }

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5ac24c6daade0..532a7dc72d84f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -145,6 +145,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/sendfile entrypoints
     libc.src.sys.sendfile.sendfile
 
+    # sys/socket.h entrypoints
+    libc.src.sys.socket.socket
+
     # sys/stat.h entrypoints
     libc.src.sys.stat.chmod
     libc.src.sys.stat.fchmod

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index f3b23a5e3e26a..9482e16073aab 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -388,6 +388,8 @@ add_gen_header(
   DEPENDS
     .llvm_libc_common_h
     .llvm-libc-macros.sys_socket_macros
+    .llvm-libc-types.struct_sockaddr
+    .llvm-libc-types.sa_family_t
 )
 
 add_gen_header(

diff  --git a/libc/include/llvm-libc-macros/linux/sys-socket-macros.h b/libc/include/llvm-libc-macros/linux/sys-socket-macros.h
index 74a886408bd4e..7de410225b712 100644
--- a/libc/include/llvm-libc-macros/linux/sys-socket-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-socket-macros.h
@@ -18,4 +18,11 @@
 #define AF_INET 2   // Internet IPv4 Protocol
 #define AF_INET6 10 // IP version 6
 
+#define SOCK_STREAM 1
+#define SOCK_DGRAM 2
+#define SOCK_RAW 3
+#define SOCK_RDM 4
+#define SOCK_SEQPACKET 5
+#define SOCK_PACKET 10
+
 #endif // __LLVM_LIBC_MACROS_LINUX_SYS_SOCKET_MACROS_H

diff  --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 37230208e458a..0ff124ac9e31a 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -87,3 +87,5 @@ add_header(struct_termios HDR struct_termios.h DEPENDS .cc_t .speed_t .tcflag_t)
 add_header(__getoptargv_t HDR __getoptargv_t.h)
 add_header(wchar_t HDR wchar_t.h)
 add_header(wint_t HDR wint_t.h)
+add_header(sa_family_t HDR sa_family_t.h)
+add_header(struct_sockaddr HDR struct_sockaddr.h)

diff  --git a/libc/include/llvm-libc-types/sa_family_t.h b/libc/include/llvm-libc-types/sa_family_t.h
new file mode 100644
index 0000000000000..52b69957b0d3f
--- /dev/null
+++ b/libc/include/llvm-libc-types/sa_family_t.h
@@ -0,0 +1,19 @@
+//===-- Definition of sa_family_t type ------------------------------------===//
+//
+// 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_TYPES_SA_FAMILY_T_H__
+#define __LLVM_LIBC_TYPES_SA_FAMILY_T_H__
+
+// The posix standard only says of sa_family_t that it must be unsigned. The
+// linux man page for "address_families" lists approximately 32 
diff erent
+// address families, meaning that a short 16 bit number will have plenty of
+// space for all of them.
+
+typedef unsigned short sa_family_t;
+
+#endif // __LLVM_LIBC_TYPES_SA_FAMILY_T_H__

diff  --git a/libc/include/llvm-libc-types/struct_sockaddr.h b/libc/include/llvm-libc-types/struct_sockaddr.h
new file mode 100644
index 0000000000000..1ef907904ca3e
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_sockaddr.h
@@ -0,0 +1,21 @@
+//===-- Definition of struct stat -----------------------------------------===//
+//
+// 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_TYPES_STRUCT_STAT_H__
+#define __LLVM_LIBC_TYPES_STRUCT_STAT_H__
+
+#include <llvm-libc-types/sa_family_t.h>
+
+struct sockaddr {
+  sa_family_t sa_family;
+  // sa_data is a variable length array. It is provided with a length of one
+  // here as a placeholder.
+  char sa_data[1];
+};
+
+#endif // __LLVM_LIBC_TYPES_STRUCT_STAT_H__

diff  --git a/libc/spec/linux.td b/libc/spec/linux.td
index d5b876cadebe5..ba5f99c12ecd1 100644
--- a/libc/spec/linux.td
+++ b/libc/spec/linux.td
@@ -110,19 +110,6 @@ def Linux : StandardSpec<"Linux"> {
       ]
   >;
 
-  HeaderSpec SysSocket = HeaderSpec<
-      "sys/socket.h",
-      [
-        Macro<"AF_UNSPEC">,
-        Macro<"AF_LOCAL">,
-        Macro<"AF_INET">,
-        Macro<"AF_INET6">,
-      ],
-      [], // Types
-      [], // Enumerations
-      []  // Functions
-  >;
-
   HeaderSpec SysTime = HeaderSpec<
       "sys/time.h",
       [
@@ -184,7 +171,6 @@ def Linux : StandardSpec<"Linux"> {
     SysMMan,
     SysPrctl,
     SysRandom,
-    SysSocket,
     SysTime,
     Signal,
   ];

diff  --git a/libc/spec/posix.td b/libc/spec/posix.td
index 2246e49eeaaa6..b1aa2fb472124 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -80,6 +80,10 @@ def RestrictedFdSetPtr : RestrictedPtrType<FdSet>;
 
 def GetoptArgvT : NamedType<"__getoptargv_t">;
 
+def StructSockAddr : NamedType<"struct sockaddr">;
+def StructSockAddrPtr : PtrType<StructSockAddr>;
+def SAFamilyType : NamedType<"sa_family_t">;
+
 def POSIX : StandardSpec<"POSIX"> {
   PtrType CharPtr = PtrType<CharType>;
   RestrictedPtrType RestrictedCharPtr = RestrictedPtrType<CharType>;
@@ -1347,6 +1351,34 @@ def POSIX : StandardSpec<"POSIX"> {
     ]
   >;
 
+  HeaderSpec SysSocket = HeaderSpec<
+      "sys/socket.h",
+      [
+        Macro<"AF_UNSPEC">,
+        Macro<"AF_UNIX">,
+        Macro<"AF_LOCAL">,
+        Macro<"AF_INET">,
+        Macro<"AF_INET6">,
+        Macro<"SOCK_STREAM">,
+        Macro<"SOCK_DGRAM">,
+        Macro<"SOCK_RAW">,
+        Macro<"SOCK_RDM">,
+        Macro<"SOCK_SEQPACKET">,
+        Macro<"SOCK_PACKET">,
+      ], // Macros
+      [
+        StructSockAddr, SAFamilyType,
+      ], // Types
+      [], // Enumerations
+      [
+        FunctionSpec<
+          "socket",
+          RetValSpec<IntType>,
+          [ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<IntType>]
+        >,
+      ]  // Functions
+  >;
+
   HeaderSpec SysTypes = HeaderSpec<
     "sys/types.h",
     [], // Macros
@@ -1373,6 +1405,7 @@ def POSIX : StandardSpec<"POSIX"> {
     SysMMan,
     SysResource,
     SysSelect,
+    SysSocket,
     SysStat,
     SysTypes,
     SysUtsName,

diff  --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index 94f62123d2c40..bf869ddc6a23c 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -2,6 +2,7 @@ add_subdirectory(mman)
 add_subdirectory(random)
 add_subdirectory(resource)
 add_subdirectory(select)
+add_subdirectory(socket)
 add_subdirectory(sendfile)
 add_subdirectory(stat)
 add_subdirectory(utsname)

diff  --git a/libc/src/sys/socket/CMakeLists.txt b/libc/src/sys/socket/CMakeLists.txt
new file mode 100644
index 0000000000000..7079d6e4466c6
--- /dev/null
+++ b/libc/src/sys/socket/CMakeLists.txt
@@ -0,0 +1,11 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  socket
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.socket
+)
+

diff  --git a/libc/src/sys/socket/linux/CMakeLists.txt b/libc/src/sys/socket/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..41bcc9c9055f4
--- /dev/null
+++ b/libc/src/sys/socket/linux/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_entrypoint_object(
+  socket
+  SRCS
+    socket.cpp
+  HDRS
+    ../socket.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.include.sys_socket
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)

diff  --git a/libc/src/sys/socket/linux/socket.cpp b/libc/src/sys/socket/linux/socket.cpp
new file mode 100644
index 0000000000000..9ce20fd1685b4
--- /dev/null
+++ b/libc/src/sys/socket/linux/socket.cpp
@@ -0,0 +1,38 @@
+//===-- Linux implementation of socket ------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/socket/socket.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 __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, socket, (int domain, int type, int protocol)) {
+#ifdef SYS_socket
+  long ret = __llvm_libc::syscall_impl(SYS_socket, domain, type, protocol);
+#elif defined(SYS_socketcall)
+  unsigned long sockcall_args[3] = {domain, type, protocol};
+  long ret =
+      __llvm_libc::syscall_impl(SYS_socketcall, SYS_SOCKET, sockcall_args);
+#else
+#error "socket and socketcall syscalls unavailable for this platform."
+#endif
+  if (ret < 0) {
+    libc_errno = -ret;
+    return -1;
+  }
+  return ret;
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/sys/socket/socket.h b/libc/src/sys/socket/socket.h
new file mode 100644
index 0000000000000..6a5a4c9e623f4
--- /dev/null
+++ b/libc/src/sys/socket/socket.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for socket ------------------------*- 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_SYS_SOCKET_SENDFILE_H
+#define LLVM_LIBC_SRC_SYS_SOCKET_SENDFILE_H
+
+namespace __llvm_libc {
+
+int socket(int domain, int type, int protocol);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_SYS_SOCKET_SENDFILE_H

diff  --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 94f62123d2c40..5ef97fe817700 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -3,6 +3,7 @@ add_subdirectory(random)
 add_subdirectory(resource)
 add_subdirectory(select)
 add_subdirectory(sendfile)
+add_subdirectory(socket)
 add_subdirectory(stat)
 add_subdirectory(utsname)
 add_subdirectory(wait)

diff  --git a/libc/test/src/sys/socket/CMakeLists.txt b/libc/test/src/sys/socket/CMakeLists.txt
new file mode 100644
index 0000000000000..b4bbe81c92ff2
--- /dev/null
+++ b/libc/test/src/sys/socket/CMakeLists.txt
@@ -0,0 +1,3 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${LIBC_TARGET_OS})
+endif()

diff  --git a/libc/test/src/sys/socket/linux/CMakeLists.txt b/libc/test/src/sys/socket/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..4380597e55157
--- /dev/null
+++ b/libc/test/src/sys/socket/linux/CMakeLists.txt
@@ -0,0 +1,14 @@
+add_custom_target(libc_sys_socket_unittests)
+
+add_libc_unittest(
+  socket_test
+  SUITE
+    libc_sys_socket_unittests
+  SRCS
+    socket_test.cpp
+  DEPENDS
+    libc.include.sys_socket
+    libc.src.errno.errno
+    libc.src.sys.socket.socket
+    libc.src.unistd.close
+)

diff  --git a/libc/test/src/sys/socket/linux/socket_test.cpp b/libc/test/src/sys/socket/linux/socket_test.cpp
new file mode 100644
index 0000000000000..6826594e52d7b
--- /dev/null
+++ b/libc/test/src/sys/socket/linux/socket_test.cpp
@@ -0,0 +1,24 @@
+//===-- Unittests for socket ----------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/socket/socket.h"
+
+#include "src/unistd/close.h"
+
+#include "src/errno/libc_errno.h"
+#include "test/UnitTest/Test.h"
+
+#include <sys/socket.h> // For AF_LOCAL and SOCK_DGRAM
+
+TEST(LlvmLibcSocketTest, LocalSocket) {
+  int sock = __llvm_libc::socket(AF_LOCAL, SOCK_DGRAM, 0);
+  ASSERT_GE(sock, 0);
+  ASSERT_EQ(libc_errno, 0);
+
+  __llvm_libc::close(sock);
+}


        


More information about the libc-commits mailing list