Skip to content

Commit 7ba4dae

Browse files
add bazel rules, socketpair test, fix headergen files (old and new)
1 parent e52a13d commit 7ba4dae

File tree

6 files changed

+272
-0
lines changed

6 files changed

+272
-0
lines changed

libc/newhdrgen/yaml/sys/socket.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,12 @@ functions:
107107
- type: int
108108
- type: int
109109
- type: int
110+
- name: socketpair
111+
standards:
112+
- posix
113+
return_type: int
114+
arguments:
115+
- type: int
116+
- type: int
117+
- type: int
118+
- type: int*

libc/spec/posix.td

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def GetoptArgvT : NamedType<"__getoptargv_t">;
8080

8181
def SAFamilyType : NamedType<"sa_family_t">;
8282
def SocklenType : NamedType<"socklen_t">;
83+
def SocklenPtr : PtrType<SocklenType>;
8384

8485
def StructSockAddr : NamedType<"struct sockaddr">;
8586
def StructSockAddrPtr : PtrType<StructSockAddr>;
@@ -1735,6 +1736,11 @@ def POSIX : StandardSpec<"POSIX"> {
17351736
RetValSpec<IntType>,
17361737
[ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<IntType>]
17371738
>,
1739+
FunctionSpec<
1740+
"socketpair",
1741+
RetValSpec<IntType>,
1742+
[ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<IntPtr>]
1743+
>,
17381744
FunctionSpec<
17391745
"bind",
17401746
RetValSpec<IntType>,
@@ -1759,6 +1765,25 @@ def POSIX : StandardSpec<"POSIX"> {
17591765
RetValSpec<SSizeTType>,
17601766
[ArgSpec<IntType>, ArgSpec<ConstStructMsghdrPtr>, ArgSpec<IntType>]
17611767
>,
1768+
FunctionSpec<
1769+
"recv",
1770+
RetValSpec<SSizeTType>,
1771+
[ArgSpec<IntType>, ArgSpec<VoidPtr>, ArgSpec<SizeTType>, ArgSpec<IntType>]
1772+
>,
1773+
FunctionSpec<
1774+
"recvfrom",
1775+
RetValSpec<SSizeTType>,
1776+
[
1777+
ArgSpec<IntType>, ArgSpec<VoidPtr>, ArgSpec<SizeTType>,
1778+
ArgSpec<IntType>, ArgSpec<StructSockAddrPtr>,
1779+
ArgSpec<SocklenPtr>
1780+
]
1781+
>,
1782+
FunctionSpec<
1783+
"recvmsg",
1784+
RetValSpec<SSizeTType>,
1785+
[ArgSpec<IntType>, ArgSpec<StructMsghdrPtr>, ArgSpec<IntType>]
1786+
>,
17621787
] // Functions
17631788
>;
17641789

libc/test/src/sys/socket/linux/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ add_libc_unittest(
1313
libc.src.unistd.close
1414
)
1515

16+
add_libc_unittest(
17+
socketpair_test
18+
SUITE
19+
libc_sys_socket_unittests
20+
SRCS
21+
socketpair_test.cpp
22+
DEPENDS
23+
libc.include.sys_socket
24+
libc.src.errno.errno
25+
libc.src.sys.socket.socketpair
26+
libc.src.unistd.close
27+
)
28+
1629
add_libc_unittest(
1730
bind_test
1831
SUITE
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Unittests for socketpair ------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sys/socket/socketpair.h"
10+
11+
#include "src/unistd/close.h"
12+
13+
#include "src/errno/libc_errno.h"
14+
#include "test/UnitTest/Test.h"
15+
16+
#include <sys/socket.h> // For AF_UNIX and SOCK_DGRAM
17+
18+
TEST(LlvmLibcSocketPairTest, LocalSocket) {
19+
int sockpair[2] = {-1, -1};
20+
int result = LIBC_NAMESPACE::socketpair(AF_UNIX, SOCK_DGRAM, 0, sockpair);
21+
ASSERT_EQ(result, 0);
22+
ASSERT_ERRNO_SUCCESS();
23+
24+
ASSERT_GE(sockpair[0], 0);
25+
ASSERT_GE(sockpair[1], 0);
26+
27+
LIBC_NAMESPACE::close(sockpair[0]);
28+
LIBC_NAMESPACE::close(sockpair[1]);
29+
ASSERT_ERRNO_SUCCESS();
30+
}
31+
32+
TEST(LlvmLibcSocketPairTest, SocketFails) {
33+
int sockpair[2] = {-1, -1};
34+
int result = LIBC_NAMESPACE::socketpair(-1, -1, -1, sockpair);
35+
ASSERT_EQ(result, -1);
36+
ASSERT_ERRNO_FAILURE();
37+
}

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ libc_support_library(
217217
hdrs = ["hdr/types/FILE.h"],
218218
)
219219

220+
libc_support_library(
221+
name = "types_ssize_t",
222+
hdrs = ["hdr/types/ssize_t.h"],
223+
)
224+
225+
libc_support_library(
226+
name = "types_socklen_t",
227+
hdrs = ["hdr/types/socklen_t.h"],
228+
)
229+
230+
libc_support_library(
231+
name = "types_struct_sockaddr",
232+
hdrs = ["hdr/types/struct_sockaddr.h"],
233+
)
234+
235+
libc_support_library(
236+
name = "types_struct_msghdr",
237+
hdrs = ["hdr/types/struct_msghdr.h"],
238+
)
239+
220240
############################### Support libraries ##############################
221241

222242
libc_support_library(
@@ -4150,6 +4170,112 @@ libc_function(
41504170
],
41514171
)
41524172

4173+
############################## sys/socket targets ##############################
4174+
4175+
libc_function(
4176+
name = "socket",
4177+
srcs = ["src/sys/socket/linux/socket.cpp"],
4178+
hdrs = ["src/sys/socket/socket.h"],
4179+
deps = [
4180+
":__support_common",
4181+
":__support_osutil_syscall",
4182+
":errno",
4183+
],
4184+
)
4185+
4186+
libc_function(
4187+
name = "socketpair",
4188+
srcs = ["src/sys/socket/linux/socketpair.cpp"],
4189+
hdrs = ["src/sys/socket/socketpair.h"],
4190+
deps = [
4191+
":__support_common",
4192+
":__support_osutil_syscall",
4193+
":errno",
4194+
],
4195+
)
4196+
4197+
libc_function(
4198+
name = "send",
4199+
srcs = ["src/sys/socket/linux/send.cpp"],
4200+
hdrs = ["src/sys/socket/send.h"],
4201+
deps = [
4202+
":__support_common",
4203+
":__support_osutil_syscall",
4204+
":errno",
4205+
":types_socklen_t",
4206+
":types_ssize_t",
4207+
":types_struct_sockaddr",
4208+
],
4209+
)
4210+
4211+
libc_function(
4212+
name = "sendto",
4213+
srcs = ["src/sys/socket/linux/sendto.cpp"],
4214+
hdrs = ["src/sys/socket/sendto.h"],
4215+
deps = [
4216+
":__support_common",
4217+
":__support_osutil_syscall",
4218+
":errno",
4219+
":types_socklen_t",
4220+
":types_ssize_t",
4221+
":types_struct_sockaddr",
4222+
],
4223+
)
4224+
4225+
libc_function(
4226+
name = "sendmsg",
4227+
srcs = ["src/sys/socket/linux/sendmsg.cpp"],
4228+
hdrs = ["src/sys/socket/sendmsg.h"],
4229+
deps = [
4230+
":__support_common",
4231+
":__support_osutil_syscall",
4232+
":errno",
4233+
":types_ssize_t",
4234+
":types_struct_msghdr",
4235+
],
4236+
)
4237+
4238+
libc_function(
4239+
name = "recv",
4240+
srcs = ["src/sys/socket/linux/recv.cpp"],
4241+
hdrs = ["src/sys/socket/recv.h"],
4242+
deps = [
4243+
":__support_common",
4244+
":__support_osutil_syscall",
4245+
":errno",
4246+
":types_socklen_t",
4247+
":types_ssize_t",
4248+
":types_struct_sockaddr",
4249+
],
4250+
)
4251+
4252+
libc_function(
4253+
name = "recvfrom",
4254+
srcs = ["src/sys/socket/linux/recvfrom.cpp"],
4255+
hdrs = ["src/sys/socket/recvfrom.h"],
4256+
deps = [
4257+
":__support_common",
4258+
":__support_osutil_syscall",
4259+
":errno",
4260+
":types_socklen_t",
4261+
":types_ssize_t",
4262+
":types_struct_sockaddr",
4263+
],
4264+
)
4265+
4266+
libc_function(
4267+
name = "recvmsg",
4268+
srcs = ["src/sys/socket/linux/recvmsg.cpp"],
4269+
hdrs = ["src/sys/socket/recvmsg.h"],
4270+
deps = [
4271+
":__support_common",
4272+
":__support_osutil_syscall",
4273+
":errno",
4274+
":types_ssize_t",
4275+
":types_struct_msghdr",
4276+
],
4277+
)
4278+
41534279
############################## sys/epoll targets ###############################
41544280

41554281
libc_function(
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
# Tests for LLVM libc string.h functions.
6+
7+
load("//libc/test:libc_test_rules.bzl", "libc_test")
8+
9+
package(default_visibility = ["//visibility:public"])
10+
11+
licenses(["notice"])
12+
13+
libc_test(
14+
name = "socket_test",
15+
srcs = ["linux/socket_test.cpp"],
16+
libc_function_deps = [
17+
"//libc:socket",
18+
"//libc:close",
19+
],
20+
)
21+
22+
libc_test(
23+
name = "socketpair_test",
24+
srcs = ["linux/socketpair_test.cpp"],
25+
libc_function_deps = [
26+
"//libc:socketpair",
27+
"//libc:close",
28+
],
29+
)
30+
31+
libc_test(
32+
name = "send_recv_test",
33+
srcs = ["linux/send_recv_test.cpp"],
34+
libc_function_deps = [
35+
"//libc:socketpair",
36+
"//libc:send",
37+
"//libc:recv",
38+
"//libc:close",
39+
],
40+
)
41+
42+
libc_test(
43+
name = "sendto_recvfrom_test",
44+
srcs = ["linux/sendto_recvfrom_test.cpp"],
45+
libc_function_deps = [
46+
"//libc:socketpair",
47+
"//libc:sendto",
48+
"//libc:recvfrom",
49+
"//libc:close",
50+
],
51+
)
52+
53+
libc_test(
54+
name = "sendmsg_recvmsg_test",
55+
srcs = ["linux/sendmsg_recvmsg_test.cpp"],
56+
libc_function_deps = [
57+
"//libc:socketpair",
58+
"//libc:sendmsg",
59+
"//libc:recvmsg",
60+
"//libc:close",
61+
],
62+
)

0 commit comments

Comments
 (0)