[libc-commits] [libc] [libc] Fix unit test compile flags propagation. (PR #106128)

via libc-commits libc-commits at lists.llvm.org
Thu Aug 29 11:21:19 PDT 2024


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/106128

>From fb39d4611b91c2e7d970132756ce97905ece9b8a Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Mon, 26 Aug 2024 16:00:24 -0400
Subject: [PATCH 1/5] [libc] Fix unit test compile flags propagation.

---
 libc/CMakeLists.txt                           | 27 +++++++++++++++----
 .../cmake/modules/LLVMLibCArchitectures.cmake |  3 ++-
 libc/cmake/modules/LLVMLibCCheckMPFR.cmake    |  1 +
 .../modules/LLVMLibCCompileOptionRules.cmake  |  2 +-
 libc/cmake/modules/LLVMLibCTestRules.cmake    |  3 ++-
 libc/test/UnitTest/CMakeLists.txt             |  7 ++---
 libc/test/src/math/smoke/nan_test.cpp         |  6 ++---
 libc/test/src/math/smoke/nanf128_test.cpp     |  9 ++++---
 libc/test/src/math/smoke/nanf16_test.cpp      |  7 ++---
 libc/test/src/math/smoke/nanf_test.cpp        |  6 ++---
 libc/test/src/math/smoke/nanl_test.cpp        |  6 ++---
 11 files changed, 48 insertions(+), 29 deletions(-)

diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index dd45d6cc8cb6ab..b19e53d8241bee 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -131,12 +131,29 @@ if(COMMAND_RETURN_CODE EQUAL 0)
   message(STATUS "Set COMPILER_RESOURCE_DIR to "
                  "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
 else()
-  if (LIBC_TARGET_OS_IS_GPU)
-    message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
+  # Try with GCC option: -print-search-dirs, which will output in the form:
+  #   install: <path>
+  #   programs: ........
+  # So we try to capture the <path> after "install: " in the first line of the
+  # output.
+  execute_process(
+    OUTPUT_STRIP_TRAILING_WHITESPACE
+    COMMAND ${CMAKE_CXX_COMPILER} -print-search-dirs
+    RESULT_VARIABLE COMMAND_RETURN_CODE
+    OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
+  )
+  if(COMMAND_RETURN_CODE EQUAL 0)
+    string(REPLACE " " ";" COMPILER_RESOURCE_DIR ${COMPILER_RESOURCE_DIR})
+    string(REPLACE "\n" ";" COMPILER_RESOURCE_DIR "${COMPILER_RESOURCE_DIR}")
+    list(GET COMPILER_RESOURCE_DIR 1 COMPILER_RESOURCE_DIR)
   else()
-    set(COMPILER_RESOURCE_DIR OFF)
-    message(STATUS "COMPILER_RESOURCE_DIR not set
-                    --print-resource-dir not supported by host compiler")
+    if (LIBC_TARGET_OS_IS_GPU)
+      message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
+    else()
+      set(COMPILER_RESOURCE_DIR OFF)
+      message(STATUS "COMPILER_RESOURCE_DIR not set
+                      --print-resource-dir not supported by host compiler")
+    endif()
   endif()
 endif()
 
diff --git a/libc/cmake/modules/LLVMLibCArchitectures.cmake b/libc/cmake/modules/LLVMLibCArchitectures.cmake
index dacb4db75d3374..d922b4f21a8ac6 100644
--- a/libc/cmake/modules/LLVMLibCArchitectures.cmake
+++ b/libc/cmake/modules/LLVMLibCArchitectures.cmake
@@ -207,4 +207,5 @@ if(explicit_target_triple AND
 endif()
 
 message(STATUS
-        "Building libc for ${LIBC_TARGET_ARCHITECTURE} on ${LIBC_TARGET_OS}")
+        "Building libc for ${LIBC_TARGET_ARCHITECTURE} on ${LIBC_TARGET_OS} with
+        LIBC_COMPILE_OPTIONS_DEFAULT: ${LIBC_COMPILE_OPTIONS_DEFAULT}")
diff --git a/libc/cmake/modules/LLVMLibCCheckMPFR.cmake b/libc/cmake/modules/LLVMLibCCheckMPFR.cmake
index a27c2dc0c030bc..c0f0d0e34b2816 100644
--- a/libc/cmake/modules/LLVMLibCCheckMPFR.cmake
+++ b/libc/cmake/modules/LLVMLibCCheckMPFR.cmake
@@ -12,6 +12,7 @@ else()
     ${CMAKE_CURRENT_BINARY_DIR}
     SOURCES
     ${LIBC_SOURCE_DIR}/utils/MPFRWrapper/check_mpfr.cpp
+    COMPILE_DEFINITIONS ${LIBC_COMPILE_OPTIONS_DEFAULT}
     LINK_LIBRARIES
       -lmpfr -lgmp -latomic
   )
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index e3dfe1a1529691..abbf4ed385b642 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -233,7 +233,7 @@ endfunction()
 
 function(_get_hermetic_test_compile_options output_var flags)
   _get_compile_options_from_flags(compile_flags ${flags})
-  list(APPEND compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags}
+  set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags}
        ${flags} -fpie -ffreestanding -fno-exceptions -fno-rtti)
 
   # The GPU build requires overriding the default CMake triple and architecture.
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index d8097855d16377..b0842805bfbaa1 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -138,7 +138,7 @@ function(create_libc_unittest fq_target_name)
 
   _get_common_test_compile_options(compile_options "${LIBC_UNITTEST_C_TEST}"
                                    "${LIBC_UNITTEST_FLAGS}")
-  list(APPEND compile_options ${LIBC_UNITTEST_COMPILE_OPTIONS})
+  list(APPEND compile_options ${LIBC_UNITTEST_COMPILE_OPTIONS} -static)
 
   if(SHOW_INTERMEDIATE_OBJECTS)
     message(STATUS "Adding unit test ${fq_target_name}")
@@ -192,6 +192,7 @@ function(create_libc_unittest fq_target_name)
   target_include_directories(${fq_build_target_name} SYSTEM PRIVATE ${LIBC_INCLUDE_DIR})
   target_include_directories(${fq_build_target_name} PRIVATE ${LIBC_SOURCE_DIR})
   target_compile_options(${fq_build_target_name} PRIVATE ${compile_options})
+  target_link_options(${fq_build_target_name} PRIVATE ${compile_options})
 
   if(NOT LIBC_UNITTEST_CXX_STANDARD)
     set(LIBC_UNITTEST_CXX_STANDARD ${CMAKE_CXX_STANDARD})
diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index 6427b861580777..ef08e13132d474 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -20,14 +20,11 @@ function(add_unittest_framework_library name)
       ${TEST_LIB_HDRS}
     )
     target_include_directories(${lib} PUBLIC ${LIBC_SOURCE_DIR})
-    list(APPEND compile_options -fno-exceptions -fno-rtti)
     if(TARGET libc.src.time.clock)
       target_compile_definitions(${lib} PRIVATE TARGET_SUPPORTS_CLOCK)
     endif()
-    if(LIBC_COMPILER_HAS_FIXED_POINT)
-      list(APPEND compile_options -ffixed-point)
-    endif()
-    target_compile_options(${lib} PUBLIC ${compile_options})
+    _get_common_test_compile_options(compile_options "" "")
+    target_compile_options(${lib} PRIVATE ${compile_options})
   endforeach()
   _get_hermetic_test_compile_options(compile_options -nostdinc++)
   target_include_directories(${name}.hermetic PRIVATE ${LIBC_BUILD_DIR}/include)
diff --git a/libc/test/src/math/smoke/nan_test.cpp b/libc/test/src/math/smoke/nan_test.cpp
index 68c844181a1946..797479d5e5deba 100644
--- a/libc/test/src/math/smoke/nan_test.cpp
+++ b/libc/test/src/math/smoke/nan_test.cpp
@@ -44,7 +44,7 @@ TEST_F(LlvmLibcNanTest, RandomString) {
 }
 
 #if !defined(LIBC_HAVE_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
-TEST_F(LlvmLibcNanTest, InvalidInput) {
-  EXPECT_DEATH([] { LIBC_NAMESPACE::nan(nullptr); }, WITH_SIGNAL(SIGSEGV));
-}
+// TEST_F(LlvmLibcNanTest, InvalidInput) {
+//   EXPECT_DEATH([] { LIBC_NAMESPACE::nan(nullptr); }, WITH_SIGNAL(SIGSEGV));
+// }
 #endif // LIBC_HAVE_ADDRESS_SANITIZER
diff --git a/libc/test/src/math/smoke/nanf128_test.cpp b/libc/test/src/math/smoke/nanf128_test.cpp
index 015cc31e4be237..63203652532bb4 100644
--- a/libc/test/src/math/smoke/nanf128_test.cpp
+++ b/libc/test/src/math/smoke/nanf128_test.cpp
@@ -54,8 +54,9 @@ TEST_F(LlvmLibcNanf128Test, RandomString) {
 }
 
 #if !defined(LIBC_HAVE_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
-#include <signal.h>
-TEST_F(LlvmLibcNanf128Test, InvalidInput) {
-  EXPECT_DEATH([] { LIBC_NAMESPACE::nanf128(nullptr); }, WITH_SIGNAL(SIGSEGV));
-}
+// #include <signal.h>
+// TEST_F(LlvmLibcNanf128Test, InvalidInput) {
+//   EXPECT_DEATH([] { LIBC_NAMESPACE::nanf128(nullptr); },
+//   WITH_SIGNAL(SIGSEGV));
+// }
 #endif // LIBC_HAVE_ADDRESS_SANITIZER
diff --git a/libc/test/src/math/smoke/nanf16_test.cpp b/libc/test/src/math/smoke/nanf16_test.cpp
index 81b844bf6bb59c..1873d0a15d2cc2 100644
--- a/libc/test/src/math/smoke/nanf16_test.cpp
+++ b/libc/test/src/math/smoke/nanf16_test.cpp
@@ -45,7 +45,8 @@ TEST_F(LlvmLibcNanf16Test, RandomString) {
 }
 
 #if !defined(LIBC_HAVE_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
-TEST_F(LlvmLibcNanf16Test, InvalidInput) {
-  EXPECT_DEATH([] { LIBC_NAMESPACE::nanf16(nullptr); }, WITH_SIGNAL(SIGSEGV));
-}
+// TEST_F(LlvmLibcNanf16Test, InvalidInput) {
+//   EXPECT_DEATH([] { LIBC_NAMESPACE::nanf16(nullptr); },
+//   WITH_SIGNAL(SIGSEGV));
+// }
 #endif // LIBC_HAVE_ADDRESS_SANITIZER
diff --git a/libc/test/src/math/smoke/nanf_test.cpp b/libc/test/src/math/smoke/nanf_test.cpp
index ff5823685225ce..cf58ec2052defa 100644
--- a/libc/test/src/math/smoke/nanf_test.cpp
+++ b/libc/test/src/math/smoke/nanf_test.cpp
@@ -43,7 +43,7 @@ TEST_F(LlvmLibcNanfTest, RandomString) {
 }
 
 #if !defined(LIBC_HAVE_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
-TEST_F(LlvmLibcNanfTest, InvalidInput) {
-  EXPECT_DEATH([] { LIBC_NAMESPACE::nanf(nullptr); }, WITH_SIGNAL(SIGSEGV));
-}
+// TEST_F(LlvmLibcNanfTest, InvalidInput) {
+//   EXPECT_DEATH([] { LIBC_NAMESPACE::nanf(nullptr); }, WITH_SIGNAL(SIGSEGV));
+// }
 #endif // LIBC_HAVE_ADDRESS_SANITIZER
diff --git a/libc/test/src/math/smoke/nanl_test.cpp b/libc/test/src/math/smoke/nanl_test.cpp
index de9af05100c10a..49744f0b645f37 100644
--- a/libc/test/src/math/smoke/nanl_test.cpp
+++ b/libc/test/src/math/smoke/nanl_test.cpp
@@ -71,7 +71,7 @@ TEST_F(LlvmLibcNanlTest, RandomString) {
 }
 
 #if !defined(LIBC_HAVE_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
-TEST_F(LlvmLibcNanlTest, InvalidInput) {
-  EXPECT_DEATH([] { LIBC_NAMESPACE::nanl(nullptr); }, WITH_SIGNAL(SIGSEGV));
-}
+// TEST_F(LlvmLibcNanlTest, InvalidInput) {
+//   EXPECT_DEATH([] { LIBC_NAMESPACE::nanl(nullptr); }, WITH_SIGNAL(SIGSEGV));
+// }
 #endif // LIBC_HAVE_ADDRESS_SANITIZER

>From 1ee30a9eda16476e64a3d81f2cf06cfa21232ff1 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Wed, 28 Aug 2024 06:56:44 -0400
Subject: [PATCH 2/5] save current progress.

---
 libc/CMakeLists.txt                           |  4 +-
 .../modules/LLVMLibCCheckCpuFeatures.cmake    |  4 +
 libc/test/src/math/smoke/nanf_test.cpp        |  6 +-
 libc/test/src/sys/mman/linux/madvise_test.cpp |  4 +-
 libc/test/src/sys/mman/linux/mincore_test.cpp | 10 +--
 libc/test/src/sys/mman/linux/mlock_test.cpp   | 90 +++++++++----------
 libc/test/src/sys/mman/linux/msync_test.cpp   |  4 +-
 .../src/sys/mman/linux/posix_madvise_test.cpp |  4 +-
 libc/test/src/termios/termios_test.cpp        |  2 +-
 libc/test/src/unistd/pread_pwrite_test.cpp    |  2 +-
 libc/test/src/unistd/read_write_test.cpp      |  2 +-
 libc/utils/MPFRWrapper/CMakeLists.txt         |  3 +-
 12 files changed, 71 insertions(+), 64 deletions(-)

diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index b19e53d8241bee..73e6cc0d74988d 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -146,7 +146,9 @@ else()
     string(REPLACE " " ";" COMPILER_RESOURCE_DIR ${COMPILER_RESOURCE_DIR})
     string(REPLACE "\n" ";" COMPILER_RESOURCE_DIR "${COMPILER_RESOURCE_DIR}")
     list(GET COMPILER_RESOURCE_DIR 1 COMPILER_RESOURCE_DIR)
-  else()
+    message(STATUS "Set COMPILER_RESOURCE_DIR to "
+    "${COMPILER_RESOURCE_DIR} using --print-search-dirs")
+else()
     if (LIBC_TARGET_OS_IS_GPU)
       message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
     else()
diff --git a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
index be569f6f9cabfb..3b6b6f56fc80cf 100644
--- a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
+++ b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
@@ -13,6 +13,10 @@ elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
   set(LIBC_COMPILE_OPTIONS_NATIVE -mcpu=native)
 endif()
 
+if(LIBC_CROSSBUILD)
+  set(LIBC_COMPILE_OPTIONS_NATIVE ${LIBC_COMPILE_OPTIONS_DEFAULT})
+endif()
+
 # Making sure ALL_CPU_FEATURES is sorted.
 list(SORT ALL_CPU_FEATURES)
 
diff --git a/libc/test/src/math/smoke/nanf_test.cpp b/libc/test/src/math/smoke/nanf_test.cpp
index cf58ec2052defa..ff5823685225ce 100644
--- a/libc/test/src/math/smoke/nanf_test.cpp
+++ b/libc/test/src/math/smoke/nanf_test.cpp
@@ -43,7 +43,7 @@ TEST_F(LlvmLibcNanfTest, RandomString) {
 }
 
 #if !defined(LIBC_HAVE_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
-// TEST_F(LlvmLibcNanfTest, InvalidInput) {
-//   EXPECT_DEATH([] { LIBC_NAMESPACE::nanf(nullptr); }, WITH_SIGNAL(SIGSEGV));
-// }
+TEST_F(LlvmLibcNanfTest, InvalidInput) {
+  EXPECT_DEATH([] { LIBC_NAMESPACE::nanf(nullptr); }, WITH_SIGNAL(SIGSEGV));
+}
 #endif // LIBC_HAVE_ADDRESS_SANITIZER
diff --git a/libc/test/src/sys/mman/linux/madvise_test.cpp b/libc/test/src/sys/mman/linux/madvise_test.cpp
index 6768d111c0d913..4af91a258a3c3a 100644
--- a/libc/test/src/sys/mman/linux/madvise_test.cpp
+++ b/libc/test/src/sys/mman/linux/madvise_test.cpp
@@ -39,6 +39,6 @@ TEST(LlvmLibcMadviseTest, NoError) {
 
 TEST(LlvmLibcMadviseTest, Error_BadPtr) {
   LIBC_NAMESPACE::libc_errno = 0;
-  EXPECT_THAT(LIBC_NAMESPACE::madvise(nullptr, 8, MADV_SEQUENTIAL),
-              Fails(ENOMEM));
+  // 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 8e0c29c2ac1777..895d748c59610b 100644
--- a/libc/test/src/sys/mman/linux/mincore_test.cpp
+++ b/libc/test/src/sys/mman/linux/mincore_test.cpp
@@ -40,8 +40,8 @@ TEST(LlvmLibcMincoreTest, UnalignedAddr) {
   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));
+  // 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());
 }
 
@@ -107,9 +107,9 @@ TEST(LlvmLibcMincoreTest, PageOut) {
                 Succeeds());
 
     LIBC_NAMESPACE::libc_errno = 0;
-    int res = LIBC_NAMESPACE::mincore(addr, page_size, &vec);
-    EXPECT_EQ(vec & 1u, 0u);
-    EXPECT_THAT(res, Succeeds());
+    // int res = LIBC_NAMESPACE::mincore(addr, page_size, &vec);
+    // EXPECT_EQ(vec & 1u, 0u);
+    // EXPECT_THAT(res, Succeeds());
   }
 
   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 804038a68a7e09..d1e77cb6c8875e 100644
--- a/libc/test/src/sys/mman/linux/mlock_test.cpp
+++ b/libc/test/src/sys/mman/linux/mlock_test.cpp
@@ -76,8 +76,8 @@ static bool is_permitted_size(size_t size) {
 }
 
 TEST(LlvmLibcMlockTest, UnMappedMemory) {
-  EXPECT_THAT(LIBC_NAMESPACE::mlock(nullptr, 1024), Fails(ENOMEM));
-  EXPECT_THAT(LIBC_NAMESPACE::munlock(nullptr, 1024), Fails(ENOMEM));
+  // EXPECT_THAT(LIBC_NAMESPACE::mlock(nullptr, 1024), Fails(ENOMEM));
+  // EXPECT_THAT(LIBC_NAMESPACE::munlock(nullptr, 1024), Fails(ENOMEM));
 }
 
 TEST(LlvmLibcMlockTest, Overflow) {
@@ -93,53 +93,53 @@ TEST(LlvmLibcMlockTest, Overflow) {
 
 #ifdef SYS_mlock2
 TEST(LlvmLibcMlockTest, MLock2) {
-  PageHolder holder;
-  EXPECT_TRUE(holder.is_valid());
-  EXPECT_THAT(LIBC_NAMESPACE::madvise(holder.addr, holder.size, MADV_DONTNEED),
-              Succeeds());
-  EXPECT_THAT(LIBC_NAMESPACE::mlock2(holder.addr, holder.size, 0), Succeeds());
-  unsigned char vec;
-  EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
-              Succeeds());
-  EXPECT_EQ(vec & 1, 1);
-  EXPECT_THAT(LIBC_NAMESPACE::munlock(holder.addr, holder.size), Succeeds());
-  EXPECT_THAT(LIBC_NAMESPACE::madvise(holder.addr, holder.size, MADV_DONTNEED),
-              Succeeds());
-  EXPECT_THAT(LIBC_NAMESPACE::mlock2(holder.addr, holder.size, MLOCK_ONFAULT),
-              Succeeds());
-  EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
-              Succeeds());
-  EXPECT_EQ(vec & 1, 0);
-  holder[0] = 1;
-  EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
-              Succeeds());
-  EXPECT_EQ(vec & 1, 1);
-  EXPECT_THAT(LIBC_NAMESPACE::munlock(holder.addr, holder.size), Succeeds());
+  // PageHolder holder;
+  // EXPECT_TRUE(holder.is_valid());
+  // EXPECT_THAT(LIBC_NAMESPACE::madvise(holder.addr, holder.size, MADV_DONTNEED),
+  //             Succeeds());
+  // EXPECT_THAT(LIBC_NAMESPACE::mlock2(holder.addr, holder.size, 0), Succeeds());
+  // unsigned char vec;
+  // EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
+  //             Succeeds());
+  // EXPECT_EQ(vec & 1, 1);
+  // EXPECT_THAT(LIBC_NAMESPACE::munlock(holder.addr, holder.size), Succeeds());
+  // EXPECT_THAT(LIBC_NAMESPACE::madvise(holder.addr, holder.size, MADV_DONTNEED),
+  //             Succeeds());
+  // EXPECT_THAT(LIBC_NAMESPACE::mlock2(holder.addr, holder.size, MLOCK_ONFAULT),
+  //             Succeeds());
+  // EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
+  //             Succeeds());
+  // EXPECT_EQ(vec & 1, 0);
+  // holder[0] = 1;
+  // EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
+  //             Succeeds());
+  // EXPECT_EQ(vec & 1, 1);
+  // EXPECT_THAT(LIBC_NAMESPACE::munlock(holder.addr, holder.size), Succeeds());
 }
 #endif
 
 TEST(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();
-  EXPECT_NE(addr, MAP_FAILED);
-
-  // Invalid mlock2 flags.
-  EXPECT_THAT(LIBC_NAMESPACE::mlock2(addr, alloc_size, 1234), Fails(EINVAL));
-
-  // Invalid mlockall flags.
-  EXPECT_THAT(LIBC_NAMESPACE::mlockall(1234), Fails(EINVAL));
-
-  // 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
-  // Linux 4.19.y (EOL).
-  // TODO(ndesaulniers) re-enable after
-  // https://github.com/llvm/llvm-project/issues/80073 is fixed.
-  // EXPECT_THAT(LIBC_NAMESPACE::mlockall(MCL_ONFAULT), Fails(EINVAL));
-
-  LIBC_NAMESPACE::munmap(addr, alloc_size);
+  // 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();
+  // EXPECT_NE(addr, MAP_FAILED);
+
+  // // Invalid mlock2 flags.
+  // EXPECT_THAT(LIBC_NAMESPACE::mlock2(addr, alloc_size, 1234), Fails(EINVAL));
+
+  // // Invalid mlockall flags.
+  // EXPECT_THAT(LIBC_NAMESPACE::mlockall(1234), Fails(EINVAL));
+
+  // // 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
+  // // Linux 4.19.y (EOL).
+  // // TODO(ndesaulniers) re-enable after
+  // // https://github.com/llvm/llvm-project/issues/80073 is fixed.
+  // // EXPECT_THAT(LIBC_NAMESPACE::mlockall(MCL_ONFAULT), Fails(EINVAL));
+
+  // LIBC_NAMESPACE::munmap(addr, alloc_size);
 }
 
 TEST(LlvmLibcMlockTest, MLockAll) {
diff --git a/libc/test/src/sys/mman/linux/msync_test.cpp b/libc/test/src/sys/mman/linux/msync_test.cpp
index 0d60415b1243ab..448aa7cc69c78f 100644
--- a/libc/test/src/sys/mman/linux/msync_test.cpp
+++ b/libc/test/src/sys/mman/linux/msync_test.cpp
@@ -38,8 +38,8 @@ struct PageHolder {
 };
 
 TEST(LlvmLibcMsyncTest, UnMappedMemory) {
-  EXPECT_THAT(LIBC_NAMESPACE::msync(nullptr, 1024, MS_SYNC), Fails(ENOMEM));
-  EXPECT_THAT(LIBC_NAMESPACE::msync(nullptr, 1024, MS_ASYNC), Fails(ENOMEM));
+  // 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) {
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 ee6489c5ed2f53..e2f296c44a07a4 100644
--- a/libc/test/src/sys/mman/linux/posix_madvise_test.cpp
+++ b/libc/test/src/sys/mman/linux/posix_madvise_test.cpp
@@ -45,6 +45,6 @@ TEST(LlvmLibcPosixMadviseTest, Error_BadPtr) {
 
   // 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);
+  // EXPECT_EQ(LIBC_NAMESPACE::posix_madvise(nullptr, 8, POSIX_MADV_SEQUENTIAL),
+            // ENOMEM);
 }
diff --git a/libc/test/src/termios/termios_test.cpp b/libc/test/src/termios/termios_test.cpp
index f8fc09a8bbf0e2..8941b43be6ccd9 100644
--- a/libc/test/src/termios/termios_test.cpp
+++ b/libc/test/src/termios/termios_test.cpp
@@ -59,6 +59,6 @@ TEST(LlvmLibcTermiosTest, TcGetSidSmokeTest) {
   if (fd < 0)
     return; // When /dev/tty is not available, no point continuing.
   ASSERT_ERRNO_SUCCESS();
-  ASSERT_GT(LIBC_NAMESPACE::tcgetsid(fd), pid_t(0));
+  // ASSERT_GT(LIBC_NAMESPACE::tcgetsid(fd), pid_t(0));
   ASSERT_EQ(LIBC_NAMESPACE::close(fd), 0);
 }
diff --git a/libc/test/src/unistd/pread_pwrite_test.cpp b/libc/test/src/unistd/pread_pwrite_test.cpp
index 3c42fcc777a683..d18e504103f742 100644
--- a/libc/test/src/unistd/pread_pwrite_test.cpp
+++ b/libc/test/src/unistd/pread_pwrite_test.cpp
@@ -72,5 +72,5 @@ TEST(LlvmLibcUniStd, PWriteFails) {
 
 TEST(LlvmLibcUniStd, PReadFails) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
-  EXPECT_THAT(LIBC_NAMESPACE::pread(-1, nullptr, 1, 0), Fails(EBADF));
+  // EXPECT_THAT(LIBC_NAMESPACE::pread(-1, nullptr, 1, 0), Fails(EBADF));
 }
diff --git a/libc/test/src/unistd/read_write_test.cpp b/libc/test/src/unistd/read_write_test.cpp
index 8b6ba427a343a2..7f05df5c03719e 100644
--- a/libc/test/src/unistd/read_write_test.cpp
+++ b/libc/test/src/unistd/read_write_test.cpp
@@ -56,7 +56,7 @@ TEST(LlvmLibcUniStd, WriteFails) {
 TEST(LlvmLibcUniStd, ReadFails) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 
-  EXPECT_THAT(LIBC_NAMESPACE::read(-1, nullptr, 1), Fails(EBADF));
+  // EXPECT_THAT(LIBC_NAMESPACE::read(-1, nullptr, 1), Fails(EBADF));
   EXPECT_THAT(LIBC_NAMESPACE::read(0, reinterpret_cast<void *>(-1), 1),
               Fails(EFAULT));
 }
diff --git a/libc/utils/MPFRWrapper/CMakeLists.txt b/libc/utils/MPFRWrapper/CMakeLists.txt
index be0415d0956fa4..6feaf131da39e3 100644
--- a/libc/utils/MPFRWrapper/CMakeLists.txt
+++ b/libc/utils/MPFRWrapper/CMakeLists.txt
@@ -4,7 +4,8 @@ if(LIBC_TESTS_CAN_USE_MPFR)
     MPFRUtils.h
     mpfr_inc.h
   )
-  target_compile_options(libcMPFRWrapper PRIVATE -O3)
+  _get_common_test_compile_options(compile_options "" "")
+  target_compile_options(libcMPFRWrapper PRIVATE -O3 ${compile_options})
   add_dependencies(
     libcMPFRWrapper
     libc.src.__support.CPP.array

>From 07a6709bd64d2e44c25cfe12d1208b8a7cc3d9c4 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 29 Aug 2024 15:23:10 +0000
Subject: [PATCH 3/5] Revert test changes.

---
 libc/test/src/math/smoke/nan_test.cpp         |  6 +-
 libc/test/src/math/smoke/nanf128_test.cpp     |  9 +-
 libc/test/src/math/smoke/nanf16_test.cpp      |  7 +-
 libc/test/src/math/smoke/nanl_test.cpp        |  6 +-
 libc/test/src/sys/mman/linux/madvise_test.cpp |  4 +-
 libc/test/src/sys/mman/linux/mincore_test.cpp | 10 +--
 libc/test/src/sys/mman/linux/mlock_test.cpp   | 90 +++++++++----------
 libc/test/src/sys/mman/linux/msync_test.cpp   |  4 +-
 .../src/sys/mman/linux/posix_madvise_test.cpp |  4 +-
 libc/test/src/termios/termios_test.cpp        |  2 +-
 libc/test/src/unistd/pread_pwrite_test.cpp    |  2 +-
 libc/test/src/unistd/read_write_test.cpp      |  2 +-
 12 files changed, 72 insertions(+), 74 deletions(-)

diff --git a/libc/test/src/math/smoke/nan_test.cpp b/libc/test/src/math/smoke/nan_test.cpp
index 797479d5e5deba..68c844181a1946 100644
--- a/libc/test/src/math/smoke/nan_test.cpp
+++ b/libc/test/src/math/smoke/nan_test.cpp
@@ -44,7 +44,7 @@ TEST_F(LlvmLibcNanTest, RandomString) {
 }
 
 #if !defined(LIBC_HAVE_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
-// TEST_F(LlvmLibcNanTest, InvalidInput) {
-//   EXPECT_DEATH([] { LIBC_NAMESPACE::nan(nullptr); }, WITH_SIGNAL(SIGSEGV));
-// }
+TEST_F(LlvmLibcNanTest, InvalidInput) {
+  EXPECT_DEATH([] { LIBC_NAMESPACE::nan(nullptr); }, WITH_SIGNAL(SIGSEGV));
+}
 #endif // LIBC_HAVE_ADDRESS_SANITIZER
diff --git a/libc/test/src/math/smoke/nanf128_test.cpp b/libc/test/src/math/smoke/nanf128_test.cpp
index 63203652532bb4..015cc31e4be237 100644
--- a/libc/test/src/math/smoke/nanf128_test.cpp
+++ b/libc/test/src/math/smoke/nanf128_test.cpp
@@ -54,9 +54,8 @@ TEST_F(LlvmLibcNanf128Test, RandomString) {
 }
 
 #if !defined(LIBC_HAVE_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
-// #include <signal.h>
-// TEST_F(LlvmLibcNanf128Test, InvalidInput) {
-//   EXPECT_DEATH([] { LIBC_NAMESPACE::nanf128(nullptr); },
-//   WITH_SIGNAL(SIGSEGV));
-// }
+#include <signal.h>
+TEST_F(LlvmLibcNanf128Test, InvalidInput) {
+  EXPECT_DEATH([] { LIBC_NAMESPACE::nanf128(nullptr); }, WITH_SIGNAL(SIGSEGV));
+}
 #endif // LIBC_HAVE_ADDRESS_SANITIZER
diff --git a/libc/test/src/math/smoke/nanf16_test.cpp b/libc/test/src/math/smoke/nanf16_test.cpp
index 1873d0a15d2cc2..81b844bf6bb59c 100644
--- a/libc/test/src/math/smoke/nanf16_test.cpp
+++ b/libc/test/src/math/smoke/nanf16_test.cpp
@@ -45,8 +45,7 @@ TEST_F(LlvmLibcNanf16Test, RandomString) {
 }
 
 #if !defined(LIBC_HAVE_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
-// TEST_F(LlvmLibcNanf16Test, InvalidInput) {
-//   EXPECT_DEATH([] { LIBC_NAMESPACE::nanf16(nullptr); },
-//   WITH_SIGNAL(SIGSEGV));
-// }
+TEST_F(LlvmLibcNanf16Test, InvalidInput) {
+  EXPECT_DEATH([] { LIBC_NAMESPACE::nanf16(nullptr); }, WITH_SIGNAL(SIGSEGV));
+}
 #endif // LIBC_HAVE_ADDRESS_SANITIZER
diff --git a/libc/test/src/math/smoke/nanl_test.cpp b/libc/test/src/math/smoke/nanl_test.cpp
index 49744f0b645f37..de9af05100c10a 100644
--- a/libc/test/src/math/smoke/nanl_test.cpp
+++ b/libc/test/src/math/smoke/nanl_test.cpp
@@ -71,7 +71,7 @@ TEST_F(LlvmLibcNanlTest, RandomString) {
 }
 
 #if !defined(LIBC_HAVE_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
-// TEST_F(LlvmLibcNanlTest, InvalidInput) {
-//   EXPECT_DEATH([] { LIBC_NAMESPACE::nanl(nullptr); }, WITH_SIGNAL(SIGSEGV));
-// }
+TEST_F(LlvmLibcNanlTest, InvalidInput) {
+  EXPECT_DEATH([] { LIBC_NAMESPACE::nanl(nullptr); }, WITH_SIGNAL(SIGSEGV));
+}
 #endif // LIBC_HAVE_ADDRESS_SANITIZER
diff --git a/libc/test/src/sys/mman/linux/madvise_test.cpp b/libc/test/src/sys/mman/linux/madvise_test.cpp
index 4af91a258a3c3a..6768d111c0d913 100644
--- a/libc/test/src/sys/mman/linux/madvise_test.cpp
+++ b/libc/test/src/sys/mman/linux/madvise_test.cpp
@@ -39,6 +39,6 @@ TEST(LlvmLibcMadviseTest, NoError) {
 
 TEST(LlvmLibcMadviseTest, Error_BadPtr) {
   LIBC_NAMESPACE::libc_errno = 0;
-  // EXPECT_THAT(LIBC_NAMESPACE::madvise(nullptr, 8, MADV_SEQUENTIAL),
-  //             Fails(ENOMEM));
+  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 895d748c59610b..8e0c29c2ac1777 100644
--- a/libc/test/src/sys/mman/linux/mincore_test.cpp
+++ b/libc/test/src/sys/mman/linux/mincore_test.cpp
@@ -40,8 +40,8 @@ TEST(LlvmLibcMincoreTest, UnalignedAddr) {
   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));
+  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());
 }
 
@@ -107,9 +107,9 @@ TEST(LlvmLibcMincoreTest, PageOut) {
                 Succeeds());
 
     LIBC_NAMESPACE::libc_errno = 0;
-    // int res = LIBC_NAMESPACE::mincore(addr, page_size, &vec);
-    // EXPECT_EQ(vec & 1u, 0u);
-    // EXPECT_THAT(res, Succeeds());
+    int res = LIBC_NAMESPACE::mincore(addr, page_size, &vec);
+    EXPECT_EQ(vec & 1u, 0u);
+    EXPECT_THAT(res, Succeeds());
   }
 
   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 d1e77cb6c8875e..804038a68a7e09 100644
--- a/libc/test/src/sys/mman/linux/mlock_test.cpp
+++ b/libc/test/src/sys/mman/linux/mlock_test.cpp
@@ -76,8 +76,8 @@ static bool is_permitted_size(size_t size) {
 }
 
 TEST(LlvmLibcMlockTest, UnMappedMemory) {
-  // EXPECT_THAT(LIBC_NAMESPACE::mlock(nullptr, 1024), Fails(ENOMEM));
-  // EXPECT_THAT(LIBC_NAMESPACE::munlock(nullptr, 1024), Fails(ENOMEM));
+  EXPECT_THAT(LIBC_NAMESPACE::mlock(nullptr, 1024), Fails(ENOMEM));
+  EXPECT_THAT(LIBC_NAMESPACE::munlock(nullptr, 1024), Fails(ENOMEM));
 }
 
 TEST(LlvmLibcMlockTest, Overflow) {
@@ -93,53 +93,53 @@ TEST(LlvmLibcMlockTest, Overflow) {
 
 #ifdef SYS_mlock2
 TEST(LlvmLibcMlockTest, MLock2) {
-  // PageHolder holder;
-  // EXPECT_TRUE(holder.is_valid());
-  // EXPECT_THAT(LIBC_NAMESPACE::madvise(holder.addr, holder.size, MADV_DONTNEED),
-  //             Succeeds());
-  // EXPECT_THAT(LIBC_NAMESPACE::mlock2(holder.addr, holder.size, 0), Succeeds());
-  // unsigned char vec;
-  // EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
-  //             Succeeds());
-  // EXPECT_EQ(vec & 1, 1);
-  // EXPECT_THAT(LIBC_NAMESPACE::munlock(holder.addr, holder.size), Succeeds());
-  // EXPECT_THAT(LIBC_NAMESPACE::madvise(holder.addr, holder.size, MADV_DONTNEED),
-  //             Succeeds());
-  // EXPECT_THAT(LIBC_NAMESPACE::mlock2(holder.addr, holder.size, MLOCK_ONFAULT),
-  //             Succeeds());
-  // EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
-  //             Succeeds());
-  // EXPECT_EQ(vec & 1, 0);
-  // holder[0] = 1;
-  // EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
-  //             Succeeds());
-  // EXPECT_EQ(vec & 1, 1);
-  // EXPECT_THAT(LIBC_NAMESPACE::munlock(holder.addr, holder.size), Succeeds());
+  PageHolder holder;
+  EXPECT_TRUE(holder.is_valid());
+  EXPECT_THAT(LIBC_NAMESPACE::madvise(holder.addr, holder.size, MADV_DONTNEED),
+              Succeeds());
+  EXPECT_THAT(LIBC_NAMESPACE::mlock2(holder.addr, holder.size, 0), Succeeds());
+  unsigned char vec;
+  EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
+              Succeeds());
+  EXPECT_EQ(vec & 1, 1);
+  EXPECT_THAT(LIBC_NAMESPACE::munlock(holder.addr, holder.size), Succeeds());
+  EXPECT_THAT(LIBC_NAMESPACE::madvise(holder.addr, holder.size, MADV_DONTNEED),
+              Succeeds());
+  EXPECT_THAT(LIBC_NAMESPACE::mlock2(holder.addr, holder.size, MLOCK_ONFAULT),
+              Succeeds());
+  EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
+              Succeeds());
+  EXPECT_EQ(vec & 1, 0);
+  holder[0] = 1;
+  EXPECT_THAT(LIBC_NAMESPACE::mincore(holder.addr, holder.size, &vec),
+              Succeeds());
+  EXPECT_EQ(vec & 1, 1);
+  EXPECT_THAT(LIBC_NAMESPACE::munlock(holder.addr, holder.size), Succeeds());
 }
 #endif
 
 TEST(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();
-  // EXPECT_NE(addr, MAP_FAILED);
-
-  // // Invalid mlock2 flags.
-  // EXPECT_THAT(LIBC_NAMESPACE::mlock2(addr, alloc_size, 1234), Fails(EINVAL));
-
-  // // Invalid mlockall flags.
-  // EXPECT_THAT(LIBC_NAMESPACE::mlockall(1234), Fails(EINVAL));
-
-  // // 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
-  // // Linux 4.19.y (EOL).
-  // // TODO(ndesaulniers) re-enable after
-  // // https://github.com/llvm/llvm-project/issues/80073 is fixed.
-  // // EXPECT_THAT(LIBC_NAMESPACE::mlockall(MCL_ONFAULT), Fails(EINVAL));
-
-  // LIBC_NAMESPACE::munmap(addr, alloc_size);
+  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();
+  EXPECT_NE(addr, MAP_FAILED);
+
+  // Invalid mlock2 flags.
+  EXPECT_THAT(LIBC_NAMESPACE::mlock2(addr, alloc_size, 1234), Fails(EINVAL));
+
+  // Invalid mlockall flags.
+  EXPECT_THAT(LIBC_NAMESPACE::mlockall(1234), Fails(EINVAL));
+
+  // 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
+  // Linux 4.19.y (EOL).
+  // TODO(ndesaulniers) re-enable after
+  // https://github.com/llvm/llvm-project/issues/80073 is fixed.
+  // EXPECT_THAT(LIBC_NAMESPACE::mlockall(MCL_ONFAULT), Fails(EINVAL));
+
+  LIBC_NAMESPACE::munmap(addr, alloc_size);
 }
 
 TEST(LlvmLibcMlockTest, MLockAll) {
diff --git a/libc/test/src/sys/mman/linux/msync_test.cpp b/libc/test/src/sys/mman/linux/msync_test.cpp
index 448aa7cc69c78f..0d60415b1243ab 100644
--- a/libc/test/src/sys/mman/linux/msync_test.cpp
+++ b/libc/test/src/sys/mman/linux/msync_test.cpp
@@ -38,8 +38,8 @@ struct PageHolder {
 };
 
 TEST(LlvmLibcMsyncTest, UnMappedMemory) {
-  // EXPECT_THAT(LIBC_NAMESPACE::msync(nullptr, 1024, MS_SYNC), Fails(ENOMEM));
-  // EXPECT_THAT(LIBC_NAMESPACE::msync(nullptr, 1024, MS_ASYNC), Fails(ENOMEM));
+  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) {
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 e2f296c44a07a4..ee6489c5ed2f53 100644
--- a/libc/test/src/sys/mman/linux/posix_madvise_test.cpp
+++ b/libc/test/src/sys/mman/linux/posix_madvise_test.cpp
@@ -45,6 +45,6 @@ TEST(LlvmLibcPosixMadviseTest, Error_BadPtr) {
 
   // 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);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_madvise(nullptr, 8, POSIX_MADV_SEQUENTIAL),
+            ENOMEM);
 }
diff --git a/libc/test/src/termios/termios_test.cpp b/libc/test/src/termios/termios_test.cpp
index 8941b43be6ccd9..f8fc09a8bbf0e2 100644
--- a/libc/test/src/termios/termios_test.cpp
+++ b/libc/test/src/termios/termios_test.cpp
@@ -59,6 +59,6 @@ TEST(LlvmLibcTermiosTest, TcGetSidSmokeTest) {
   if (fd < 0)
     return; // When /dev/tty is not available, no point continuing.
   ASSERT_ERRNO_SUCCESS();
-  // ASSERT_GT(LIBC_NAMESPACE::tcgetsid(fd), pid_t(0));
+  ASSERT_GT(LIBC_NAMESPACE::tcgetsid(fd), pid_t(0));
   ASSERT_EQ(LIBC_NAMESPACE::close(fd), 0);
 }
diff --git a/libc/test/src/unistd/pread_pwrite_test.cpp b/libc/test/src/unistd/pread_pwrite_test.cpp
index d18e504103f742..3c42fcc777a683 100644
--- a/libc/test/src/unistd/pread_pwrite_test.cpp
+++ b/libc/test/src/unistd/pread_pwrite_test.cpp
@@ -72,5 +72,5 @@ TEST(LlvmLibcUniStd, PWriteFails) {
 
 TEST(LlvmLibcUniStd, PReadFails) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
-  // EXPECT_THAT(LIBC_NAMESPACE::pread(-1, nullptr, 1, 0), Fails(EBADF));
+  EXPECT_THAT(LIBC_NAMESPACE::pread(-1, nullptr, 1, 0), Fails(EBADF));
 }
diff --git a/libc/test/src/unistd/read_write_test.cpp b/libc/test/src/unistd/read_write_test.cpp
index 7f05df5c03719e..8b6ba427a343a2 100644
--- a/libc/test/src/unistd/read_write_test.cpp
+++ b/libc/test/src/unistd/read_write_test.cpp
@@ -56,7 +56,7 @@ TEST(LlvmLibcUniStd, WriteFails) {
 TEST(LlvmLibcUniStd, ReadFails) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 
-  // EXPECT_THAT(LIBC_NAMESPACE::read(-1, nullptr, 1), Fails(EBADF));
+  EXPECT_THAT(LIBC_NAMESPACE::read(-1, nullptr, 1), Fails(EBADF));
   EXPECT_THAT(LIBC_NAMESPACE::read(0, reinterpret_cast<void *>(-1), 1),
               Fails(EFAULT));
 }

>From d6230b18f5f788403656383307bb99ad8bdee9bc Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 29 Aug 2024 15:55:36 +0000
Subject: [PATCH 4/5] Address comments.

---
 libc/CMakeLists.txt                            |  1 +
 .../modules/LLVMLibCCompileOptionRules.cmake   | 18 ++++++++++--------
 libc/cmake/modules/LLVMLibCTestRules.cmake     | 12 +++++++-----
 libc/test/UnitTest/CMakeLists.txt              | 10 ++++++----
 4 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index 73e6cc0d74988d..c393d23a3b33d4 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -112,6 +112,7 @@ add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE})
 
 # Flags to pass down to the compiler while building the libc functions.
 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
+set(LIBC_TEST_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Common compile options for all the tests.")
 
 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
 
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index abbf4ed385b642..45dfe3e63302bf 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -188,7 +188,10 @@ endfunction()
 function(_get_common_test_compile_options output_var c_test flags)
   _get_compile_options_from_flags(compile_flags ${flags})
 
-  set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags})
+  set(compile_options
+      ${LIBC_COMPILE_OPTIONS_DEFAULT}
+      ${LIBC_TEST_COMPILE_OPTIONS_DEFAULT}
+      ${compile_flags})
 
   if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
     list(APPEND compile_options "-fpie")
@@ -232,9 +235,12 @@ function(_get_common_test_compile_options output_var c_test flags)
 endfunction()
 
 function(_get_hermetic_test_compile_options output_var flags)
-  _get_compile_options_from_flags(compile_flags ${flags})
-  set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags}
-       ${flags} -fpie -ffreestanding -fno-exceptions -fno-rtti)
+  _get_common_test_compile_options(compile_options "" "${flags}")
+
+  list(APPEND compile_options "-fpie")
+  list(APPEND compile_options "-ffreestanding")
+  list(APPEND compile_options "-fno-exceptions")
+  list(APPEND compile_options "-fno-rtti")
 
   # The GPU build requires overriding the default CMake triple and architecture.
   if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
@@ -248,9 +254,5 @@ function(_get_hermetic_test_compile_options output_var flags)
          -nogpulib -march=${LIBC_GPU_TARGET_ARCHITECTURE} -fno-use-cxa-atexit)
   endif()
 
-  if(LLVM_LIBC_FULL_BUILD)
-    list(APPEND compile_options "-DLIBC_FULL_BUILD")
-  endif()
-  
   set(${output_var} ${compile_options} PARENT_SCOPE)
 endfunction()
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index b0842805bfbaa1..f899efc3cc5ec8 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -466,8 +466,9 @@ function(add_integration_test test_name)
   target_include_directories(${fq_build_target_name} SYSTEM PRIVATE ${LIBC_INCLUDE_DIR})
   target_include_directories(${fq_build_target_name} PRIVATE ${LIBC_SOURCE_DIR})
 
-  _get_hermetic_test_compile_options(compile_options "${INTEGRATION_TEST_COMPILE_OPTIONS}")
-  target_compile_options(${fq_build_target_name} PRIVATE ${compile_options})
+  _get_hermetic_test_compile_options(compile_options "")
+  target_compile_options(${fq_build_target_name} PRIVATE
+                         ${compile_options} ${INTEGRATION_TEST_COMPILE_OPTIONS})
 
   if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
     target_link_options(${fq_build_target_name} PRIVATE
@@ -639,11 +640,12 @@ function(add_libc_hermetic test_name)
       #OUTPUT_NAME ${fq_target_name}
   )
 
-  _get_hermetic_test_compile_options(compile_options "${HERMETIC_TEST_COMPILE_OPTIONS}")
   target_include_directories(${fq_build_target_name} SYSTEM PRIVATE ${LIBC_INCLUDE_DIR})
   target_include_directories(${fq_build_target_name} PRIVATE ${LIBC_SOURCE_DIR})
-  _get_hermetic_test_compile_options(compile_options "${HERMETIC_TEST_COMPILE_OPTIONS}")
-  target_compile_options(${fq_build_target_name} PRIVATE ${compile_options})
+  _get_hermetic_test_compile_options(compile_options "")
+  target_compile_options(${fq_build_target_name} PRIVATE
+                         ${compile_options}
+                         ${HERMETIC_TEST_COMPILE_OPTIONS})
 
   set(link_libraries "")
   foreach(lib IN LISTS HERMETIC_TEST_LINK_LIBRARIES)
diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index ef08e13132d474..a10d6581f668d6 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -23,12 +23,14 @@ function(add_unittest_framework_library name)
     if(TARGET libc.src.time.clock)
       target_compile_definitions(${lib} PRIVATE TARGET_SUPPORTS_CLOCK)
     endif()
-    _get_common_test_compile_options(compile_options "" "")
-    target_compile_options(${lib} PRIVATE ${compile_options})
   endforeach()
-  _get_hermetic_test_compile_options(compile_options -nostdinc++)
+
+  _get_common_test_compile_options(compile_options "" "")
+  target_compile_options(${name}.unit PRIVATE ${compile_options})
+
+  _get_hermetic_test_compile_options(compile_options "")
   target_include_directories(${name}.hermetic PRIVATE ${LIBC_BUILD_DIR}/include)
-  target_compile_options(${name}.hermetic PRIVATE ${compile_options})
+  target_compile_options(${name}.hermetic PRIVATE ${compile_options} -nostdinc++)
 
   if(TEST_LIB_DEPENDS)
     foreach(dep IN ITEMS ${TEST_LIB_DEPENDS})

>From e0737b2ad38f10b9d0d4f0a380e5af26184fe9dd Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 29 Aug 2024 18:20:34 +0000
Subject: [PATCH 5/5] Remove -static from unit tests.

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index f899efc3cc5ec8..1f6ccb27f35f02 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -138,7 +138,7 @@ function(create_libc_unittest fq_target_name)
 
   _get_common_test_compile_options(compile_options "${LIBC_UNITTEST_C_TEST}"
                                    "${LIBC_UNITTEST_FLAGS}")
-  list(APPEND compile_options ${LIBC_UNITTEST_COMPILE_OPTIONS} -static)
+  list(APPEND compile_options ${LIBC_UNITTEST_COMPILE_OPTIONS})
 
   if(SHOW_INTERMEDIATE_OBJECTS)
     message(STATUS "Adding unit test ${fq_target_name}")



More information about the libc-commits mailing list