Skip to content

[libc] implement sys/uio/readv #124718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ set(TARGET_LIBC_ENTRYPOINTS

# sys/uio.h entrypoints
libc.src.sys.uio.writev
libc.src.sys.uio.readv
)

if(LLVM_LIBC_INCLUDE_SCUDO)
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ set(TARGET_LIBC_ENTRYPOINTS

# sys/uio.h entrypoints
libc.src.sys.uio.writev
libc.src.sys.uio.readv
)

if(LLVM_LIBC_INCLUDE_SCUDO)
Expand Down
4 changes: 4 additions & 0 deletions libc/include/llvm-libc-macros/limits-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,8 @@
#define _POSIX_ARG_MAX 4096
#endif

#ifndef IOV_MAX
#define IOV_MAX 1024
#endif // IOV_MAX

#endif // LLVM_LIBC_MACROS_LIMITS_MACROS_H
8 changes: 8 additions & 0 deletions libc/include/sys/uio.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ functions:
- type: int
- type: const struct iovec *
- type: int
- name: readv
standards:
- POSIX
return_type: ssize_t
arguments:
- type: int
- type: const struct iovec *
- type: int
7 changes: 7 additions & 0 deletions libc/src/sys/uio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.writev
)

add_entrypoint_object(
readv
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.readv
)
15 changes: 15 additions & 0 deletions libc/src/sys/uio/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ add_entrypoint_object(
HDRS
../writev.h
DEPENDS
libc.hdr.types.ssize_t
libc.hdr.types.struct_iovec
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.__support.common
libc.src.errno.errno
)

add_entrypoint_object(
readv
SRCS
readv.cpp
HDRS
../readv.h
DEPENDS
libc.hdr.types.ssize_t
libc.hdr.types.struct_iovec
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.__support.common
libc.src.errno.errno
)
29 changes: 29 additions & 0 deletions libc/src/sys/uio/linux/readv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===-- Implementation file for readv -------------------------------------===//
//
// 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/uio/readv.h"
#include "hdr/types/ssize_t.h"
#include "hdr/types/struct_iovec.h"
#include "src/__support/OSUtil/syscall.h"
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
#include <sys/syscall.h>

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(ssize_t, readv, (int fd, const iovec *iov, int iovcnt)) {
long ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_readv, fd, iov, iovcnt);
// On failure, return -1 and set errno.
if (ret < 0) {
libc_errno = static_cast<int>(-ret);
return -1;
}
// On success, return number of bytes read.
return static_cast<ssize_t>(ret);
}

} // namespace LIBC_NAMESPACE_DECL
2 changes: 2 additions & 0 deletions libc/src/sys/uio/linux/writev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#include "src/sys/uio/writev.h"
#include "hdr/types/ssize_t.h"
#include "hdr/types/struct_iovec.h"
#include "src/__support/OSUtil/syscall.h"
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
Expand Down
22 changes: 22 additions & 0 deletions libc/src/sys/uio/readv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for readv -----------------------------------===//
//
// 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_UIO_READV_H
#define LLVM_LIBC_SRC_SYS_UIO_READV_H

#include "hdr/types/ssize_t.h"
#include "hdr/types/struct_iovec.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

ssize_t readv(int fd, const iovec *iov, int iovcnt);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_SYS_UIO_READV_H
25 changes: 23 additions & 2 deletions libc/test/src/sys/uio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
add_custom_target(libc_sys_uio_unittests)

add_libc_unittest(
writev_test
SUITE
libc_sys_uio_unittests
libc_sys_uio_unittests
SRCS
writev_test.cpp
DEPENDS
libc.src.errno.errno
libc.hdr.types.struct_iovec
libc.src.__support.common
libc.src.errno.errno
libc.src.fcntl.open
libc.src.sys.uio.writev
libc.src.unistd.close
libc.src.unistd.unlink
libc.test.UnitTest.ErrnoSetterMatcher
)

add_libc_unittest(
readv_test
SUITE
libc_sys_uio_unittests
SRCS
readv_test.cpp
DEPENDS
libc.hdr.types.struct_iovec
libc.src.__support.common
libc.src.errno.errno
libc.src.fcntl.open
libc.src.sys.uio.readv
libc.src.unistd.close
libc.src.unistd.unlink
libc.src.unistd.write
libc.test.UnitTest.ErrnoSetterMatcher
)
43 changes: 43 additions & 0 deletions libc/test/src/sys/uio/readv_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===-- Unittests for readv -----------------------------------------------===//
//
// 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 "hdr/types/struct_iovec.h"
#include "src/fcntl/open.h"
#include "src/sys/uio/readv.h"
#include "src/unistd/close.h"
#include "src/unistd/unlink.h"
#include "src/unistd/write.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;

TEST(LlvmLibcSysUioReadvTest, SmokeTest) {
const char *filename = "./LlvmLibcSysUioReadvTest";
int fd = LIBC_NAMESPACE::open(filename, O_WRONLY | O_CREAT, 0644);
ASSERT_THAT(fd, returns(GT(0)).with_errno(EQ(0)));
const char data[] = "Hello, World!\n";
ASSERT_THAT(LIBC_NAMESPACE::write(fd, data, sizeof(data)),
returns(EQ(sizeof(data))).with_errno(EQ(0)));
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());

fd = LIBC_NAMESPACE::open(filename, O_RDONLY);
ASSERT_THAT(fd, returns(GT(0)).with_errno(EQ(0)));
char buf0[2];
char buf1[3];
struct iovec iov[2];
iov[0].iov_base = buf0;
iov[0].iov_len = 1;
iov[1].iov_base = buf1;
iov[1].iov_len = 2;
ASSERT_THAT(LIBC_NAMESPACE::readv(fd, iov, 2),
returns(EQ(3)).with_errno(EQ(0)));
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
ASSERT_THAT(LIBC_NAMESPACE::unlink(filename),
returns(EQ(0)).with_errno(EQ(0)));
}
7 changes: 6 additions & 1 deletion libc/test/src/sys/uio/writev_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
//
//===----------------------------------------------------------------------===//

#include "hdr/types/struct_iovec.h"
#include "src/fcntl/open.h"
#include "src/sys/uio/writev.h"
#include "src/unistd/close.h"
#include "src/unistd/unlink.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;

TEST(LlvmLibcSysUioWritevTest, SmokeTest) {
int fd = LIBC_NAMESPACE::open("/dev/null", O_WRONLY);
const char *filename = "./LlvmLibcSysUioWritevTest";
int fd = LIBC_NAMESPACE::open(filename, O_WRONLY | O_CREAT, 0644);
ASSERT_THAT(fd, returns(GT(0)).with_errno(EQ(0)));
const char *data = "Hello, World!\n";
struct iovec iov[2];
Expand All @@ -26,4 +29,6 @@ TEST(LlvmLibcSysUioWritevTest, SmokeTest) {
ASSERT_THAT(LIBC_NAMESPACE::writev(fd, iov, 2),
returns(EQ(15)).with_errno(EQ(0)));
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
ASSERT_THAT(LIBC_NAMESPACE::unlink(filename),
returns(EQ(0)).with_errno(EQ(0)));
}