[libc-commits] [libc] [libc] Update some syscall tests to be more tolerance of QEMU behaviors. (PR #208920)

via libc-commits libc-commits at lists.llvm.org
Sat Jul 11 08:19:24 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: lntue

<details>
<summary>Changes</summary>

This will allow us to enable riscv64 qemu precommit CI.

Assisted-by: Gemini 3.5 Flash

---
Full diff: https://github.com/llvm/llvm-project/pull/208920.diff


11 Files Affected:

- (modified) libc/test/src/sys/mman/linux/madvise_test.cpp (+9-2) 
- (modified) libc/test/src/sys/mman/linux/mincore_test.cpp (+2-1) 
- (modified) libc/test/src/sys/mman/linux/mlock_test.cpp (+24-2) 
- (modified) libc/test/src/sys/mman/linux/mremap_test.cpp (+3-1) 
- (modified) libc/test/src/sys/mman/linux/posix_madvise_test.cpp (+6-3) 
- (modified) libc/test/src/sys/mman/linux/remap_file_pages_test.cpp (+16) 
- (modified) libc/test/src/sys/socket/linux/sendrecvmmsg_test.cpp (+21-8) 
- (modified) libc/test/src/sys/socket/linux/socketopt_test.cpp (+5-2) 
- (modified) libc/test/src/sys/socket/linux/socketpair_test.cpp (+4-1) 
- (modified) libc/test/src/unistd/pread_pwrite_test.cpp (+2-1) 
- (modified) libc/test/src/unistd/read_write_test.cpp (+2-1) 


``````````diff
diff --git a/libc/test/src/sys/mman/linux/madvise_test.cpp b/libc/test/src/sys/mman/linux/madvise_test.cpp
index 5818696ce3840..68846d9b3d665 100644
--- a/libc/test/src/sys/mman/linux/madvise_test.cpp
+++ b/libc/test/src/sys/mman/linux/madvise_test.cpp
@@ -37,6 +37,13 @@ TEST_F(LlvmLibcMadviseTest, NoError) {
 }
 
 TEST_F(LlvmLibcMadviseTest, Error_BadPtr) {
-  EXPECT_THAT(LIBC_NAMESPACE::madvise(nullptr, 8, MADV_SEQUENTIAL),
-              Fails(ENOMEM));
+  int err = LIBC_NAMESPACE::madvise(nullptr, 8, MADV_SEQUENTIAL);
+  if (err == 0) {
+    // Under emulators like QEMU, madvise with hint flags is a no-op returning
+    // 0.
+    return;
+  }
+  EXPECT_EQ(err, -1);
+  EXPECT_EQ(static_cast<int>(libc_errno), ENOMEM);
+  libc_errno = 0;
 }
diff --git a/libc/test/src/sys/mman/linux/mincore_test.cpp b/libc/test/src/sys/mman/linux/mincore_test.cpp
index 65a184dc383ae..60817955ca200 100644
--- a/libc/test/src/sys/mman/linux/mincore_test.cpp
+++ b/libc/test/src/sys/mman/linux/mincore_test.cpp
@@ -36,7 +36,8 @@ TEST_F(LlvmLibcMincoreTest, UnalignedAddr) {
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_NE(addr, MAP_FAILED);
   EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % page_size, 0ul);
-  int res = LIBC_NAMESPACE::mincore(static_cast<char *>(addr) + 1, 1, nullptr);
+  unsigned char vec;
+  int res = LIBC_NAMESPACE::mincore(static_cast<char *>(addr) + 1, 1, &vec);
   EXPECT_THAT(res, Fails(EINVAL, -1));
   EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, page_size), Succeeds());
 }
diff --git a/libc/test/src/sys/mman/linux/mlock_test.cpp b/libc/test/src/sys/mman/linux/mlock_test.cpp
index f8742e1b6749f..01fc19e2b9178 100644
--- a/libc/test/src/sys/mman/linux/mlock_test.cpp
+++ b/libc/test/src/sys/mman/linux/mlock_test.cpp
@@ -75,8 +75,20 @@ TEST_F(LlvmLibcMlockTest, Overflow) {
               Fails(EINVAL));
 }
 
+static bool mlock2_supported() {
+  static bool supported = []() {
+    LIBC_NAMESPACE::mlock2(nullptr, 0, 0);
+    int err = libc_errno;
+    libc_errno = 0;
+    return err != ENOSYS;
+  }();
+  return supported;
+}
+
 #ifdef SYS_mlock2
 TEST_F(LlvmLibcMlockTest, MLock2) {
+  if (!mlock2_supported())
+    return;
   PageHolder holder;
   EXPECT_TRUE(holder.is_valid());
   EXPECT_THAT(LIBC_NAMESPACE::madvise(holder.addr, holder.size, MADV_DONTNEED),
@@ -110,10 +122,20 @@ TEST_F(LlvmLibcMlockTest, InvalidFlag) {
   EXPECT_NE(addr, MAP_FAILED);
 
   // Invalid mlock2 flags.
-  EXPECT_THAT(LIBC_NAMESPACE::mlock2(addr, alloc_size, 1234), Fails(EINVAL));
+  if (mlock2_supported()) {
+    EXPECT_THAT(LIBC_NAMESPACE::mlock2(addr, alloc_size, 1234), Fails(EINVAL));
+  }
 
   // Invalid mlockall flags.
-  EXPECT_THAT(LIBC_NAMESPACE::mlockall(1234), Fails(EINVAL));
+  int mlockall_ret = LIBC_NAMESPACE::mlockall(1234);
+  if (mlockall_ret == 0) {
+    // Under QEMU, mlockall can be a stub returning 0. Clean up immediately.
+    LIBC_NAMESPACE::munlockall();
+  } else {
+    EXPECT_EQ(mlockall_ret, -1);
+    EXPECT_EQ(static_cast<int>(libc_errno), EINVAL);
+    libc_errno = 0;
+  }
 
   // man 2 mlockall says EINVAL is a valid return code when MCL_ONFAULT was
   // specified without MCL_FUTURE or MCL_CURRENT, but this seems to fail on
diff --git a/libc/test/src/sys/mman/linux/mremap_test.cpp b/libc/test/src/sys/mman/linux/mremap_test.cpp
index 400719daf3af1..7811011c7d781 100644
--- a/libc/test/src/sys/mman/linux/mremap_test.cpp
+++ b/libc/test/src/sys/mman/linux/mremap_test.cpp
@@ -59,7 +59,9 @@ TEST_F(LlvmLibcMremapTest, Error_InvalidSize) {
   // Attempt to re-map the memory with an invalid new size (0).
   void *new_addr =
       LIBC_NAMESPACE::mremap(addr, initial_size, 0, MREMAP_MAYMOVE);
-  EXPECT_THAT(new_addr, Fails(EINVAL, MAP_FAILED));
+  EXPECT_EQ(new_addr, MAP_FAILED);
+  EXPECT_TRUE(libc_errno == EINVAL || libc_errno == ENOMEM);
+  libc_errno = 0; // Clear errno
 
   // Clean up the original mapping.
   EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, initial_size), Succeeds());
diff --git a/libc/test/src/sys/mman/linux/posix_madvise_test.cpp b/libc/test/src/sys/mman/linux/posix_madvise_test.cpp
index 59c580f61a5f9..a0932fdaf5314 100644
--- a/libc/test/src/sys/mman/linux/posix_madvise_test.cpp
+++ b/libc/test/src/sys/mman/linux/posix_madvise_test.cpp
@@ -44,7 +44,10 @@ TEST_F(LlvmLibcPosixMadviseTest, Error_BadPtr) {
   EXPECT_EQ(LIBC_NAMESPACE::posix_madvise(nullptr, 8, POSIX_MADV_DONTNEED), 0);
 
   // posix_madvise doesn't set errno, but the return value is actually the error
-  // code.
-  EXPECT_EQ(LIBC_NAMESPACE::posix_madvise(nullptr, 8, POSIX_MADV_SEQUENTIAL),
-            ENOMEM);
+  // code. Under emulators like QEMU, madvise with hint flags can be a no-op, so
+  // posix_madvise might return 0.
+  int ret = LIBC_NAMESPACE::posix_madvise(nullptr, 8, POSIX_MADV_SEQUENTIAL);
+  if (ret != 0) {
+    EXPECT_EQ(ret, ENOMEM);
+  }
 }
diff --git a/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp b/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp
index 94e4c63286ad2..31104bab3f012 100644
--- a/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp
+++ b/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp
@@ -26,7 +26,19 @@ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
 using LlvmLibcRemapFilePagesTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
+static bool remap_file_pages_supported() {
+  static bool supported = []() {
+    LIBC_NAMESPACE::remap_file_pages(nullptr, 0, 0, 0, 0);
+    int err = libc_errno;
+    libc_errno = 0;
+    return err != ENOSYS;
+  }();
+  return supported;
+}
+
 TEST_F(LlvmLibcRemapFilePagesTest, NoError) {
+  if (!remap_file_pages_supported())
+    return;
   size_t page_size = PAGE_SIZE;
   ASSERT_GT(page_size, size_t(0));
 
@@ -53,6 +65,8 @@ TEST_F(LlvmLibcRemapFilePagesTest, NoError) {
 }
 
 TEST_F(LlvmLibcRemapFilePagesTest, ErrorInvalidFlags) {
+  if (!remap_file_pages_supported())
+    return;
   size_t page_size = PAGE_SIZE;
   ASSERT_GT(page_size, size_t(0));
 
@@ -80,6 +94,8 @@ TEST_F(LlvmLibcRemapFilePagesTest, ErrorInvalidFlags) {
 }
 
 TEST_F(LlvmLibcRemapFilePagesTest, ErrorInvalidAddress) {
+  if (!remap_file_pages_supported())
+    return;
   size_t page_size = PAGE_SIZE;
   ASSERT_GT(page_size, size_t(0));
 
diff --git a/libc/test/src/sys/socket/linux/sendrecvmmsg_test.cpp b/libc/test/src/sys/socket/linux/sendrecvmmsg_test.cpp
index 75f352802f124..dcb37784cc826 100644
--- a/libc/test/src/sys/socket/linux/sendrecvmmsg_test.cpp
+++ b/libc/test/src/sys/socket/linux/sendrecvmmsg_test.cpp
@@ -72,13 +72,19 @@ TEST_F(LlvmLibcSendRecvMmsgTest, SendRecvMmsgSucceedsWithSocketPair) {
   }
 
   struct timespec invalid_timeout = {-1, 0};
-  ASSERT_THAT(LIBC_NAMESPACE::recvmmsg(sockpair[1], recv_msg_hdr,
-                                       MESSAGES_COUNT, 0, &invalid_timeout),
-              Fails<int>(EINVAL));
+  int recv_res = LIBC_NAMESPACE::recvmmsg(sockpair[1], recv_msg_hdr,
+                                          MESSAGES_COUNT, 0, &invalid_timeout);
+  if (recv_res == -1) {
+    ASSERT_EQ(static_cast<int>(libc_errno), EINVAL);
+    libc_errno = 0;
 
-  ASSERT_THAT(LIBC_NAMESPACE::recvmmsg(sockpair[1], recv_msg_hdr,
-                                       MESSAGES_COUNT, 0, nullptr),
-              Succeeds<int>(MESSAGES_COUNT));
+    ASSERT_THAT(LIBC_NAMESPACE::recvmmsg(sockpair[1], recv_msg_hdr,
+                                         MESSAGES_COUNT, 0, nullptr),
+                Succeeds<int>(MESSAGES_COUNT));
+  } else {
+    // Under QEMU, the call might succeed and return MESSAGES_COUNT.
+    ASSERT_EQ(recv_res, static_cast<int>(MESSAGES_COUNT));
+  }
 
   for (size_t i = 0; i < MESSAGES_COUNT; ++i) {
     ASSERT_EQ(static_cast<size_t>(recv_msg_hdr[i].msg_len),
@@ -94,6 +100,13 @@ TEST_F(LlvmLibcSendRecvMmsgTest, SendMmsgFails) {
 
 TEST_F(LlvmLibcSendRecvMmsgTest, RecvmmsgFails) {
   struct mmsghdr msg_hdrs = {};
-  ASSERT_THAT(LIBC_NAMESPACE::recvmmsg(-1, &msg_hdrs, 1, 0, nullptr),
-              Fails(EBADF, -1));
+  int ret = LIBC_NAMESPACE::recvmmsg(-1, &msg_hdrs, 1, 0, nullptr);
+  if (ret == 1) {
+    // Under QEMU, recvmmsg(-1, ...) is buggy and can return 1 instead of -1
+    // (EBADF).
+    return;
+  }
+  EXPECT_EQ(ret, -1);
+  EXPECT_EQ(static_cast<int>(libc_errno), EBADF);
+  libc_errno = 0;
 }
diff --git a/libc/test/src/sys/socket/linux/socketopt_test.cpp b/libc/test/src/sys/socket/linux/socketopt_test.cpp
index 8fd03c2cf24d4..979eb57fc29c1 100644
--- a/libc/test/src/sys/socket/linux/socketopt_test.cpp
+++ b/libc/test/src/sys/socket/linux/socketopt_test.cpp
@@ -139,8 +139,11 @@ TEST_F(LlvmLibcSocketOptTest, ReceiveTimeout) {
   ASSERT_THAT(LIBC_NAMESPACE::getsockopt(sv[0], SOL_SOCKET, SO_RCVTIMEO,
                                          &retrieved_tv, &retrieved_optlen),
               Succeeds(0));
-  ASSERT_EQ(retrieved_optlen, optlen);
-  ASSERT_EQ(retrieved_tv.tv_sec, tv.tv_sec);
+  // Under QEMU getsockopt(SO_RCVTIMEO) may return a length of 0.
+  ASSERT_TRUE(retrieved_optlen == optlen || retrieved_optlen == 0);
+  if (retrieved_optlen == optlen) {
+    ASSERT_EQ(retrieved_tv.tv_sec, tv.tv_sec);
+  }
 
   char buffer[10];
   struct timespec start, end;
diff --git a/libc/test/src/sys/socket/linux/socketpair_test.cpp b/libc/test/src/sys/socket/linux/socketpair_test.cpp
index 94b7412f8c061..cd450ef61392a 100644
--- a/libc/test/src/sys/socket/linux/socketpair_test.cpp
+++ b/libc/test/src/sys/socket/linux/socketpair_test.cpp
@@ -34,5 +34,8 @@ TEST_F(LlvmLibcSocketPairTest, LocalSocket) {
 
 TEST_F(LlvmLibcSocketPairTest, SocketFails) {
   int sockpair[2] = {-1, -1};
-  ASSERT_THAT(LIBC_NAMESPACE::socketpair(-1, -1, -1, sockpair), Fails(EINVAL));
+  int ret = LIBC_NAMESPACE::socketpair(-1, -1, -1, sockpair);
+  EXPECT_EQ(ret, -1);
+  EXPECT_TRUE(libc_errno == EINVAL || libc_errno == EAFNOSUPPORT);
+  libc_errno = 0;
 }
diff --git a/libc/test/src/unistd/pread_pwrite_test.cpp b/libc/test/src/unistd/pread_pwrite_test.cpp
index 7cbac04b60a95..ed7d2113b502a 100644
--- a/libc/test/src/unistd/pread_pwrite_test.cpp
+++ b/libc/test/src/unistd/pread_pwrite_test.cpp
@@ -74,5 +74,6 @@ TEST_F(LlvmLibcUniStd, PWriteFails) {
 
 TEST_F(LlvmLibcUniStd, PReadFails) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
-  EXPECT_THAT(LIBC_NAMESPACE::pread(-1, nullptr, 1, 0), Fails<ssize_t>(EBADF));
+  char buf[1];
+  EXPECT_THAT(LIBC_NAMESPACE::pread(-1, buf, 1, 0), Fails<ssize_t>(EBADF));
 }
diff --git a/libc/test/src/unistd/read_write_test.cpp b/libc/test/src/unistd/read_write_test.cpp
index 8952e6083f97d..39953a5cd43d6 100644
--- a/libc/test/src/unistd/read_write_test.cpp
+++ b/libc/test/src/unistd/read_write_test.cpp
@@ -58,7 +58,8 @@ TEST_F(LlvmLibcUniStd, WriteFails) {
 TEST_F(LlvmLibcUniStd, ReadFails) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 
-  EXPECT_THAT(LIBC_NAMESPACE::read(-1, nullptr, 1), Fails<ssize_t>(EBADF));
+  char buf[1];
+  EXPECT_THAT(LIBC_NAMESPACE::read(-1, buf, 1), Fails<ssize_t>(EBADF));
   EXPECT_THAT(LIBC_NAMESPACE::read(0, reinterpret_cast<void *>(-1), 1),
               Fails<ssize_t>(EFAULT));
 }

``````````

</details>


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


More information about the libc-commits mailing list