Skip to content

[libc] Migrate sys/ tests to ErrnoCheckingTest. #141871

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 1 commit into from
May 29, 2025

Conversation

vonosmas
Copy link
Contributor

See PR #132823 for a previous change to sys/epoll tests, and prior changes before that.

ErrnoCheckingTest ensures that errno is properly reset at the beginning of the test case, and is validated at the end of it. This change removes many instances of manual errno manipulation.

See PR llvm#132823 for a previous change to sys/epoll tests, and prior
changes before that.

ErrnoCheckingTest ensures that errno is properly reset at the beginning
of the test case, and is validated at the end of it. This change removes
many instances of manual errno manipulation.
@vonosmas vonosmas requested a review from lntue May 28, 2025 22:39
@vonosmas vonosmas added the libc label May 28, 2025
@llvmbot
Copy link
Member

llvmbot commented May 28, 2025

@llvm/pr-subscribers-libc

Author: Alexey Samsonov (vonosmas)

Changes

See PR #132823 for a previous change to sys/epoll tests, and prior changes before that.

ErrnoCheckingTest ensures that errno is properly reset at the beginning of the test case, and is validated at the end of it. This change removes many instances of manual errno manipulation.


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

39 Files Affected:

  • (modified) libc/test/src/sys/auxv/linux/CMakeLists.txt (+1)
  • (modified) libc/test/src/sys/auxv/linux/getauxval_test.cpp (+4-2)
  • (modified) libc/test/src/sys/mman/linux/CMakeLists.txt (+10)
  • (modified) libc/test/src/sys/mman/linux/madvise_test.cpp (+4-5)
  • (modified) libc/test/src/sys/mman/linux/mincore_test.cpp (+8-14)
  • (modified) libc/test/src/sys/mman/linux/mlock_test.cpp (+7-6)
  • (modified) libc/test/src/sys/mman/linux/mmap_test.cpp (+4-5)
  • (modified) libc/test/src/sys/mman/linux/mprotect_test.cpp (+4-4)
  • (modified) libc/test/src/sys/mman/linux/mremap_test.cpp (+4-5)
  • (modified) libc/test/src/sys/mman/linux/msync_test.cpp (+6-5)
  • (modified) libc/test/src/sys/mman/linux/posix_madvise_test.cpp (+4-5)
  • (modified) libc/test/src/sys/mman/linux/remap_file_pages_test.cpp (+5-9)
  • (modified) libc/test/src/sys/mman/linux/shm_test.cpp (+4-2)
  • (modified) libc/test/src/sys/prctl/linux/CMakeLists.txt (+2)
  • (modified) libc/test/src/sys/prctl/linux/prctl_test.cpp (+4-4)
  • (modified) libc/test/src/sys/random/linux/CMakeLists.txt (+1)
  • (modified) libc/test/src/sys/random/linux/getrandom_test.cpp (+11-13)
  • (modified) libc/test/src/sys/resource/CMakeLists.txt (+2)
  • (modified) libc/test/src/sys/resource/getrlimit_setrlimit_test.cpp (+4-7)
  • (modified) libc/test/src/sys/select/CMakeLists.txt (+3)
  • (modified) libc/test/src/sys/select/select_failure_test.cpp (+3-1)
  • (modified) libc/test/src/sys/select/select_ui_test.cpp (+12-8)
  • (modified) libc/test/src/sys/sendfile/CMakeLists.txt (+2)
  • (modified) libc/test/src/sys/sendfile/sendfile_test.cpp (+4-6)
  • (modified) libc/test/src/sys/stat/CMakeLists.txt (+14)
  • (modified) libc/test/src/sys/stat/chmod_test.cpp (+5-10)
  • (modified) libc/test/src/sys/stat/fchmod_test.cpp (+5-9)
  • (modified) libc/test/src/sys/stat/fchmodat_test.cpp (+5-10)
  • (modified) libc/test/src/sys/stat/fstat_test.cpp (+5-9)
  • (modified) libc/test/src/sys/stat/lstat_test.cpp (+5-9)
  • (modified) libc/test/src/sys/stat/mkdirat_test.cpp (+6-4)
  • (modified) libc/test/src/sys/stat/stat_test.cpp (+5-9)
  • (modified) libc/test/src/sys/statvfs/linux/CMakeLists.txt (+2)
  • (modified) libc/test/src/sys/statvfs/linux/fstatvfs_test.cpp (+5-2)
  • (modified) libc/test/src/sys/statvfs/linux/statvfs_test.cpp (+4-2)
  • (modified) libc/test/src/sys/time/utimes_test.cpp (-1)
  • (modified) libc/test/src/sys/wait/CMakeLists.txt (+6-2)
  • (modified) libc/test/src/sys/wait/wait4_test.cpp (+5-2)
  • (modified) libc/test/src/sys/wait/waitpid_test.cpp (+5-2)
diff --git a/libc/test/src/sys/auxv/linux/CMakeLists.txt b/libc/test/src/sys/auxv/linux/CMakeLists.txt
index c1e82a1f0a46c..66370118bf6d6 100644
--- a/libc/test/src/sys/auxv/linux/CMakeLists.txt
+++ b/libc/test/src/sys/auxv/linux/CMakeLists.txt
@@ -9,6 +9,7 @@ add_libc_unittest(
     libc.include.sys_auxv
     libc.src.errno.errno
     libc.src.sys.auxv.getauxval
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
     libc.src.string.strstr
 )
diff --git a/libc/test/src/sys/auxv/linux/getauxval_test.cpp b/libc/test/src/sys/auxv/linux/getauxval_test.cpp
index 8811fd8dfbc3a..b8728b7ad775c 100644
--- a/libc/test/src/sys/auxv/linux/getauxval_test.cpp
+++ b/libc/test/src/sys/auxv/linux/getauxval_test.cpp
@@ -5,16 +5,18 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-#include "src/errno/libc_errno.h"
+
 #include "src/sys/auxv/getauxval.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 #include <src/string/strstr.h>
 #include <sys/auxv.h>
 
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcGetauxvalTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
-TEST(LlvmLibcGetauxvalTest, Basic) {
+TEST_F(LlvmLibcGetauxvalTest, Basic) {
   EXPECT_THAT(LIBC_NAMESPACE::getauxval(AT_PAGESZ),
               returns(GT(0ul)).with_errno(EQ(0)));
   const char *filename;
diff --git a/libc/test/src/sys/mman/linux/CMakeLists.txt b/libc/test/src/sys/mman/linux/CMakeLists.txt
index 44ed11aadfe8b..93fdc627d9f85 100644
--- a/libc/test/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/test/src/sys/mman/linux/CMakeLists.txt
@@ -11,6 +11,7 @@ add_libc_unittest(
     libc.src.errno.errno
     libc.src.sys.mman.mmap
     libc.src.sys.mman.munmap
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
@@ -26,6 +27,7 @@ add_libc_unittest(
     libc.src.sys.mman.mmap
     libc.src.sys.mman.mremap
     libc.src.sys.mman.munmap
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
@@ -43,6 +45,7 @@ if (NOT LLVM_USE_SANITIZER)
       libc.src.sys.mman.mmap
       libc.src.sys.mman.munmap
       libc.src.sys.mman.mprotect
+      libc.test.UnitTest.ErrnoCheckingTest
       libc.test.UnitTest.ErrnoSetterMatcher
   )
 endif()
@@ -59,6 +62,7 @@ add_libc_unittest(
     libc.src.sys.mman.mmap
     libc.src.sys.mman.munmap
     libc.src.sys.mman.madvise
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
@@ -75,6 +79,7 @@ add_libc_unittest(
     libc.src.sys.mman.mmap
     libc.src.sys.mman.munmap
     libc.src.sys.mman.posix_madvise
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
@@ -95,6 +100,7 @@ add_libc_unittest(
     libc.src.sys.mman.mlock
     libc.src.sys.mman.munlock
     libc.src.unistd.sysconf
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
@@ -120,6 +126,7 @@ add_libc_unittest(
     libc.src.sys.resource.getrlimit
     libc.src.__support.OSUtil.osutil
     libc.src.unistd.sysconf
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
@@ -140,6 +147,7 @@ add_libc_unittest(
     libc.src.sys.mman.mlock
     libc.src.sys.mman.munlock
     libc.src.unistd.sysconf
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
@@ -153,6 +161,7 @@ add_libc_unittest(
     libc.include.sys_mman
     libc.include.sys_stat
     libc.src.unistd.sysconf
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
     libc.src.sys.mman.remap_file_pages
     libc.src.errno.errno
@@ -179,5 +188,6 @@ add_libc_unittest(
     libc.src.unistd.close
     libc.src.__support.OSUtil.osutil
     libc.hdr.fcntl_macros
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
diff --git a/libc/test/src/sys/mman/linux/madvise_test.cpp b/libc/test/src/sys/mman/linux/madvise_test.cpp
index 6768d111c0d91..6671050a28038 100644
--- a/libc/test/src/sys/mman/linux/madvise_test.cpp
+++ b/libc/test/src/sys/mman/linux/madvise_test.cpp
@@ -6,10 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/errno/libc_errno.h"
 #include "src/sys/mman/madvise.h"
 #include "src/sys/mman/mmap.h"
 #include "src/sys/mman/munmap.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
@@ -17,10 +17,10 @@
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcMadviseTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
-TEST(LlvmLibcMadviseTest, NoError) {
+TEST_F(LlvmLibcMadviseTest, NoError) {
   size_t alloc_size = 128;
-  LIBC_NAMESPACE::libc_errno = 0;
   void *addr = LIBC_NAMESPACE::mmap(nullptr, alloc_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   ASSERT_ERRNO_SUCCESS();
@@ -37,8 +37,7 @@ TEST(LlvmLibcMadviseTest, NoError) {
   EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, alloc_size), Succeeds());
 }
 
-TEST(LlvmLibcMadviseTest, Error_BadPtr) {
-  LIBC_NAMESPACE::libc_errno = 0;
+TEST_F(LlvmLibcMadviseTest, Error_BadPtr) {
   EXPECT_THAT(LIBC_NAMESPACE::madvise(nullptr, 8, MADV_SEQUENTIAL),
               Fails(ENOMEM));
 }
diff --git a/libc/test/src/sys/mman/linux/mincore_test.cpp b/libc/test/src/sys/mman/linux/mincore_test.cpp
index e49e300e43c88..ade620b838a38 100644
--- a/libc/test/src/sys/mman/linux/mincore_test.cpp
+++ b/libc/test/src/sys/mman/linux/mincore_test.cpp
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
-#include "src/errno/libc_errno.h"
 #include "src/sys/mman/madvise.h"
 #include "src/sys/mman/mincore.h"
 #include "src/sys/mman/mlock.h"
@@ -15,6 +14,7 @@
 #include "src/sys/mman/munlock.h"
 #include "src/sys/mman/munmap.h"
 #include "src/unistd/sysconf.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
@@ -24,64 +24,60 @@
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcMincoreTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
-TEST(LlvmLibcMincoreTest, UnMappedMemory) {
-  LIBC_NAMESPACE::libc_errno = 0;
+TEST_F(LlvmLibcMincoreTest, UnMappedMemory) {
   unsigned char vec;
   int res = LIBC_NAMESPACE::mincore(nullptr, 1, &vec);
   EXPECT_THAT(res, Fails(ENOMEM, -1));
 }
 
-TEST(LlvmLibcMincoreTest, UnalignedAddr) {
+TEST_F(LlvmLibcMincoreTest, UnalignedAddr) {
   unsigned long page_size = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
   void *addr = LIBC_NAMESPACE::mmap(nullptr, page_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_NE(addr, MAP_FAILED);
   EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % page_size, 0ul);
-  LIBC_NAMESPACE::libc_errno = 0;
   int res = LIBC_NAMESPACE::mincore(static_cast<char *>(addr) + 1, 1, nullptr);
   EXPECT_THAT(res, Fails(EINVAL, -1));
   EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, page_size), Succeeds());
 }
 
-TEST(LlvmLibcMincoreTest, InvalidVec) {
+TEST_F(LlvmLibcMincoreTest, InvalidVec) {
   unsigned long page_size = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
   void *addr = LIBC_NAMESPACE::mmap(nullptr, 4 * page_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_NE(addr, MAP_FAILED);
   EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % page_size, 0ul);
-  LIBC_NAMESPACE::libc_errno = 0;
   int res = LIBC_NAMESPACE::mincore(addr, 1, nullptr);
   EXPECT_THAT(res, Fails(EFAULT, -1));
 }
 
-TEST(LlvmLibcMincoreTest, NoError) {
+TEST_F(LlvmLibcMincoreTest, NoError) {
   unsigned long page_size = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
   void *addr = LIBC_NAMESPACE::mmap(nullptr, page_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_NE(addr, MAP_FAILED);
   EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % page_size, 0ul);
   unsigned char vec;
-  LIBC_NAMESPACE::libc_errno = 0;
   int res = LIBC_NAMESPACE::mincore(addr, 1, &vec);
   EXPECT_THAT(res, Succeeds());
   EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, page_size), Succeeds());
 }
 
-TEST(LlvmLibcMincoreTest, NegativeLength) {
+TEST_F(LlvmLibcMincoreTest, NegativeLength) {
   unsigned long page_size = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
   void *addr = LIBC_NAMESPACE::mmap(nullptr, page_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_NE(addr, MAP_FAILED);
   EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % page_size, 0ul);
   unsigned char vec;
-  LIBC_NAMESPACE::libc_errno = 0;
   int res = LIBC_NAMESPACE::mincore(addr, -1, &vec);
   EXPECT_THAT(res, Fails(ENOMEM, -1));
   EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, page_size), Succeeds());
 }
 
-TEST(LlvmLibcMincoreTest, PageOut) {
+TEST_F(LlvmLibcMincoreTest, PageOut) {
   unsigned long page_size = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
   unsigned char vec;
   void *addr = LIBC_NAMESPACE::mmap(nullptr, page_size, PROT_READ | PROT_WRITE,
@@ -101,11 +97,9 @@ TEST(LlvmLibcMincoreTest, PageOut) {
 
   // page out the memory
   {
-    LIBC_NAMESPACE::libc_errno = 0;
     EXPECT_THAT(LIBC_NAMESPACE::madvise(addr, page_size, MADV_DONTNEED),
                 Succeeds());
 
-    LIBC_NAMESPACE::libc_errno = 0;
     int res = LIBC_NAMESPACE::mincore(addr, page_size, &vec);
     EXPECT_EQ(vec & 1u, 0u);
     EXPECT_THAT(res, Succeeds());
diff --git a/libc/test/src/sys/mman/linux/mlock_test.cpp b/libc/test/src/sys/mman/linux/mlock_test.cpp
index 48cde1317655a..88abacad554e0 100644
--- a/libc/test/src/sys/mman/linux/mlock_test.cpp
+++ b/libc/test/src/sys/mman/linux/mlock_test.cpp
@@ -19,6 +19,7 @@
 #include "src/sys/mman/munmap.h"
 #include "src/sys/resource/getrlimit.h"
 #include "src/unistd/sysconf.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
@@ -29,6 +30,7 @@
 #include <unistd.h>
 
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcMlockTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
 struct PageHolder {
   size_t size;
@@ -72,12 +74,12 @@ static bool is_permitted_size(size_t size) {
          get_capacity(CAP_IPC_LOCK);
 }
 
-TEST(LlvmLibcMlockTest, UnMappedMemory) {
+TEST_F(LlvmLibcMlockTest, UnMappedMemory) {
   EXPECT_THAT(LIBC_NAMESPACE::mlock(nullptr, 1024), Fails(ENOMEM));
   EXPECT_THAT(LIBC_NAMESPACE::munlock(nullptr, 1024), Fails(ENOMEM));
 }
 
-TEST(LlvmLibcMlockTest, Overflow) {
+TEST_F(LlvmLibcMlockTest, Overflow) {
   PageHolder holder;
   EXPECT_TRUE(holder.is_valid());
   size_t negative_size = -holder.size;
@@ -89,7 +91,7 @@ TEST(LlvmLibcMlockTest, Overflow) {
 }
 
 #ifdef SYS_mlock2
-TEST(LlvmLibcMlockTest, MLock2) {
+TEST_F(LlvmLibcMlockTest, MLock2) {
   PageHolder holder;
   EXPECT_TRUE(holder.is_valid());
   EXPECT_THAT(LIBC_NAMESPACE::madvise(holder.addr, holder.size, MADV_DONTNEED),
@@ -115,9 +117,8 @@ TEST(LlvmLibcMlockTest, MLock2) {
 }
 #endif
 
-TEST(LlvmLibcMlockTest, InvalidFlag) {
+TEST_F(LlvmLibcMlockTest, InvalidFlag) {
   size_t alloc_size = 128; // page size
-  LIBC_NAMESPACE::libc_errno = 0;
   void *addr = LIBC_NAMESPACE::mmap(nullptr, alloc_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   ASSERT_ERRNO_SUCCESS();
@@ -139,7 +140,7 @@ TEST(LlvmLibcMlockTest, InvalidFlag) {
   LIBC_NAMESPACE::munmap(addr, alloc_size);
 }
 
-TEST(LlvmLibcMlockTest, MLockAll) {
+TEST_F(LlvmLibcMlockTest, MLockAll) {
   {
     PageHolder holder;
     EXPECT_TRUE(holder.is_valid());
diff --git a/libc/test/src/sys/mman/linux/mmap_test.cpp b/libc/test/src/sys/mman/linux/mmap_test.cpp
index dcbc75808f13c..1541576e3cbda 100644
--- a/libc/test/src/sys/mman/linux/mmap_test.cpp
+++ b/libc/test/src/sys/mman/linux/mmap_test.cpp
@@ -6,9 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/errno/libc_errno.h"
 #include "src/sys/mman/mmap.h"
 #include "src/sys/mman/munmap.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
@@ -16,10 +16,10 @@
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcMMapTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
-TEST(LlvmLibcMMapTest, NoError) {
+TEST_F(LlvmLibcMMapTest, NoError) {
   size_t alloc_size = 128;
-  LIBC_NAMESPACE::libc_errno = 0;
   void *addr = LIBC_NAMESPACE::mmap(nullptr, alloc_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   ASSERT_ERRNO_SUCCESS();
@@ -33,8 +33,7 @@ TEST(LlvmLibcMMapTest, NoError) {
   EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, alloc_size), Succeeds());
 }
 
-TEST(LlvmLibcMMapTest, Error_InvalidSize) {
-  LIBC_NAMESPACE::libc_errno = 0;
+TEST_F(LlvmLibcMMapTest, Error_InvalidSize) {
   void *addr = LIBC_NAMESPACE::mmap(nullptr, 0, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_THAT(addr, Fails(EINVAL, MAP_FAILED));
diff --git a/libc/test/src/sys/mman/linux/mprotect_test.cpp b/libc/test/src/sys/mman/linux/mprotect_test.cpp
index 46e449e54779f..c1278a14fbfa7 100644
--- a/libc/test/src/sys/mman/linux/mprotect_test.cpp
+++ b/libc/test/src/sys/mman/linux/mprotect_test.cpp
@@ -6,10 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/errno/libc_errno.h"
 #include "src/sys/mman/mmap.h"
 #include "src/sys/mman/mprotect.h"
 #include "src/sys/mman/munmap.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
@@ -18,10 +18,10 @@
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcMProtectTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
-TEST(LlvmLibcMProtectTest, NoError) {
+TEST_F(LlvmLibcMProtectTest, NoError) {
   size_t alloc_size = 128;
-  LIBC_NAMESPACE::libc_errno = 0;
   void *addr = LIBC_NAMESPACE::mmap(nullptr, alloc_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   ASSERT_ERRNO_SUCCESS();
@@ -47,7 +47,7 @@ TEST(LlvmLibcMProtectTest, NoError) {
 // This test is disabled currently due to flakeyness. It will be re-enabled once
 // it is less flakey.
 /*
-TEST(LlvmLibcMProtectTest, Error_InvalidWrite) {
+TEST_F(LlvmLibcMProtectTest, Error_InvalidWrite) {
   // attempting to write to a read-only protected part of memory should cause a
   // segfault.
   EXPECT_DEATH(
diff --git a/libc/test/src/sys/mman/linux/mremap_test.cpp b/libc/test/src/sys/mman/linux/mremap_test.cpp
index 12e4485588421..5ff774d57614a 100644
--- a/libc/test/src/sys/mman/linux/mremap_test.cpp
+++ b/libc/test/src/sys/mman/linux/mremap_test.cpp
@@ -6,10 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/errno/libc_errno.h"
 #include "src/sys/mman/mmap.h"
 #include "src/sys/mman/mremap.h"
 #include "src/sys/mman/munmap.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
@@ -17,11 +17,11 @@
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcMremapTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
-TEST(LlvmLibcMremapTest, NoError) {
+TEST_F(LlvmLibcMremapTest, NoError) {
   size_t initial_size = 128;
   size_t new_size = 256;
-  LIBC_NAMESPACE::libc_errno = 0;
 
   // Allocate memory using mmap.
   void *addr =
@@ -47,9 +47,8 @@ TEST(LlvmLibcMremapTest, NoError) {
   EXPECT_THAT(LIBC_NAMESPACE::munmap(new_addr, new_size), Succeeds());
 }
 
-TEST(LlvmLibcMremapTest, Error_InvalidSize) {
+TEST_F(LlvmLibcMremapTest, Error_InvalidSize) {
   size_t initial_size = 128;
-  LIBC_NAMESPACE::libc_errno = 0;
 
   // Allocate memory using mmap.
   void *addr =
diff --git a/libc/test/src/sys/mman/linux/msync_test.cpp b/libc/test/src/sys/mman/linux/msync_test.cpp
index 65eedb2b8232e..b4eedb3baeac7 100644
--- a/libc/test/src/sys/mman/linux/msync_test.cpp
+++ b/libc/test/src/sys/mman/linux/msync_test.cpp
@@ -6,17 +6,18 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/errno/libc_errno.h"
 #include "src/sys/mman/mlock.h"
 #include "src/sys/mman/mmap.h"
 #include "src/sys/mman/msync.h"
 #include "src/sys/mman/munlock.h"
 #include "src/sys/mman/munmap.h"
 #include "src/unistd/sysconf.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcMsyncTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
 struct PageHolder {
   size_t size;
@@ -36,12 +37,12 @@ struct PageHolder {
   bool is_valid() { return addr != MAP_FAILED; }
 };
 
-TEST(LlvmLibcMsyncTest, UnMappedMemory) {
+TEST_F(LlvmLibcMsyncTest, UnMappedMemory) {
   EXPECT_THAT(LIBC_NAMESPACE::msync(nullptr, 1024, MS_SYNC), Fails(ENOMEM));
   EXPECT_THAT(LIBC_NAMESPACE::msync(nullptr, 1024, MS_ASYNC), Fails(ENOMEM));
 }
 
-TEST(LlvmLibcMsyncTest, LockedPage) {
+TEST_F(LlvmLibcMsyncTest, LockedPage) {
   PageHolder page;
   ASSERT_TRUE(page.is_valid());
   ASSERT_THAT(LIBC_NAMESPACE::mlock(page.addr, page.size), Succeeds());
@@ -52,14 +53,14 @@ TEST(LlvmLibcMsyncTest, LockedPage) {
   EXPECT_THAT(LIBC_NAMESPACE::msync(page.addr, page.size, MS_SYNC), Succeeds());
 }
 
-TEST(LlvmLibcMsyncTest, UnalignedAddress) {
+TEST_F(LlvmLibcMsyncTest, UnalignedAddress) {
   PageHolder page;
   ASSERT_TRUE(page.is_valid());
   EXPECT_THAT(LIBC_NAMESPACE::msync(&page[1], page.size - 1, MS_SYNC),
               Fails(EINVAL));
 }
 
-TEST(LlvmLibcMsyncTest, InvalidFlag) {
+TEST_F(LlvmLibcMsyncTest, InvalidFlag) {
   PageHolder page;
   ASSERT_TRUE(page.is_valid());
   EXPECT_THAT(LIBC_NAMESPACE::msync(page.addr, page.size, MS_SYNC | MS_ASYNC),
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 ee6489c5ed2f5..7fe27182b02ce 100644
--- a/libc/test/src/sys/mman/linux/posix_madvise_test.cpp
+++ b/libc/test/src/sys/mman/linux/posix_madvise_test.cpp
@@ -6,10 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/errno/libc_errno.h"
 #include "src/sys/mman/mmap.h"
 #include "src/sys/mman/munmap.h"
 #include "src/sys/mman/posix_madvise.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
@@ -17,10 +17,10 @@
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcPosixMadviseTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
-TEST(LlvmLibcPosixMadviseTest, NoError) {
+TEST_F(LlvmLibcPosixMadviseTest, NoError) {
   size_t alloc_size = 128;
-  LIBC_NAMESPACE::libc_errno = 0;
   void *addr = LIBC_NAMESPACE::mmap(nullptr, alloc_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   ASSERT_ERRNO_SUCCESS();
@@ -37,8 +37,7 @@ TEST(LlvmLibcPosixMadviseTest, NoError) {
   EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, alloc_size), Succeeds());
 }
 
-TEST(LlvmLibcPosixMadviseTest, Error_BadPtr) {
-  LIBC_NAMESPACE::libc_errno = 0;
+TEST_F(LlvmLibcPosixMadviseTest, Error_BadPtr) {
   // posix_madvise is a no-op on DONTNEED, so it shouldn't fail even with the
   // nullptr.
   EXPECT_EQ(LIBC_NAMESPACE::posix_madvise(nullptr, 8, POSIX_MADV_DONTNEED), 0);
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 ebc5c89a1ff57..851e4f7ddd5b0 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
@@...
[truncated]

@vonosmas vonosmas requested a review from michaelrj-google May 29, 2025 00:16
@vonosmas vonosmas merged commit f22122a into llvm:main May 29, 2025
17 of 18 checks passed
@vonosmas vonosmas deleted the errno-checking-test-1 branch May 29, 2025 05:18
svkeerthy pushed a commit that referenced this pull request May 29, 2025
See PR #132823 for a previous change to sys/epoll tests, and prior
changes before that.

ErrnoCheckingTest ensures that errno is properly reset at the beginning
of the test case, and is validated at the end of it. This change removes
many instances of manual errno manipulation.
google-yfyang pushed a commit to google-yfyang/llvm-project that referenced this pull request May 29, 2025
See PR llvm#132823 for a previous change to sys/epoll tests, and prior
changes before that.

ErrnoCheckingTest ensures that errno is properly reset at the beginning
of the test case, and is validated at the end of it. This change removes
many instances of manual errno manipulation.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
See PR llvm#132823 for a previous change to sys/epoll tests, and prior
changes before that.

ErrnoCheckingTest ensures that errno is properly reset at the beginning
of the test case, and is validated at the end of it. This change removes
many instances of manual errno manipulation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants