Skip to content
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

[libc][test] remove C++ stdlib includes #122369

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

nickdesaulniers
Copy link
Member

These make cross compiling the test suite more difficult, as you need the
sysroot to contain these headers and libraries cross compiled for your target.
It's straightforward to stick with the corresponding C headers.

These make cross compiling the test suite more difficult, as you need the
sysroot to contain these headers and libraries cross compiled for your target.
It's straightforward to stick with the corresponding C headers.
@llvmbot llvmbot added the libc label Jan 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 9, 2025

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

These make cross compiling the test suite more difficult, as you need the
sysroot to contain these headers and libraries cross compiled for your target.
It's straightforward to stick with the corresponding C headers.


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

3 Files Affected:

  • (modified) libc/test/UnitTest/ExecuteFunctionUnix.cpp (+8-9)
  • (modified) libc/test/UnitTest/FPExceptMatcher.cpp (+3-4)
  • (modified) libc/test/UnitTest/LibcDeathTestExecutors.cpp (+1-1)
diff --git a/libc/test/UnitTest/ExecuteFunctionUnix.cpp b/libc/test/UnitTest/ExecuteFunctionUnix.cpp
index 3a657c00851c7b..df71738668592a 100644
--- a/libc/test/UnitTest/ExecuteFunctionUnix.cpp
+++ b/libc/test/UnitTest/ExecuteFunctionUnix.cpp
@@ -8,13 +8,13 @@
 
 #include "ExecuteFunction.h"
 #include "src/__support/macros/config.h"
-#include <cassert>
-#include <cstdlib>
-#include <cstring>
-#include <iostream>
-#include <memory>
+#include "test/UnitTest/ExecuteFunction.h" // FunctionCaller
+#include <assert.h>
 #include <poll.h>
 #include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/wait.h>
 #include <unistd.h>
 
@@ -35,21 +35,20 @@ int ProcessStatus::get_fatal_signal() {
 }
 
 ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
-  std::unique_ptr<FunctionCaller> X(func);
   int pipe_fds[2];
   if (::pipe(pipe_fds) == -1)
     return ProcessStatus::error("pipe(2) failed");
 
   // Don't copy the buffers into the child process and print twice.
-  std::cout.flush();
-  std::cerr.flush();
+  ::fflush(stderr);
+  ::fflush(stdout);
   pid_t pid = ::fork();
   if (pid == -1)
     return ProcessStatus::error("fork(2) failed");
 
   if (!pid) {
     (*func)();
-    std::exit(0);
+    ::exit(0);
   }
   ::close(pipe_fds[1]);
 
diff --git a/libc/test/UnitTest/FPExceptMatcher.cpp b/libc/test/UnitTest/FPExceptMatcher.cpp
index 37ba0a0a7abde4..889f9f6f44a913 100644
--- a/libc/test/UnitTest/FPExceptMatcher.cpp
+++ b/libc/test/UnitTest/FPExceptMatcher.cpp
@@ -9,11 +9,11 @@
 #include "FPExceptMatcher.h"
 
 #include "src/__support/macros/config.h"
+#include "test/UnitTest/ExecuteFunction.h" // FunctionCaller
 #include "test/UnitTest/Test.h"
 
 #include "hdr/types/fenv_t.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
-#include <memory>
 #include <setjmp.h>
 #include <signal.h>
 
@@ -37,14 +37,13 @@ static void sigfpeHandler(int sig) {
 }
 
 FPExceptMatcher::FPExceptMatcher(FunctionCaller *func) {
-  auto oldSIGFPEHandler = signal(SIGFPE, &sigfpeHandler);
-  std::unique_ptr<FunctionCaller> funcUP(func);
+  sighandler_t oldSIGFPEHandler = signal(SIGFPE, &sigfpeHandler);
 
   caughtExcept = false;
   fenv_t oldEnv;
   fputil::get_env(&oldEnv);
   if (sigsetjmp(jumpBuffer, 1) == 0)
-    funcUP->call();
+    func->call();
   // We restore the previous floating point environment after
   // the call to the function which can potentially raise SIGFPE.
   fputil::set_env(&oldEnv);
diff --git a/libc/test/UnitTest/LibcDeathTestExecutors.cpp b/libc/test/UnitTest/LibcDeathTestExecutors.cpp
index 77b0559c7acea9..943e2c23c5fde9 100644
--- a/libc/test/UnitTest/LibcDeathTestExecutors.cpp
+++ b/libc/test/UnitTest/LibcDeathTestExecutors.cpp
@@ -12,7 +12,7 @@
 #include "test/UnitTest/ExecuteFunction.h"
 #include "test/UnitTest/TestLogger.h"
 
-#include <cassert>
+#include <assert.h>
 
 namespace {
 constexpr unsigned TIMEOUT_MS = 10000;

@nickdesaulniers nickdesaulniers changed the title y [libc][test] remove C++ stdlib includes Jan 9, 2025
Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, though in future we should look into using our own functions in ExecuteFunctionUnix.cpp. I think we have most of what's needed now.

@nickdesaulniers nickdesaulniers merged commit 0efb376 into llvm:main Jan 9, 2025
11 of 12 checks passed
@nickdesaulniers nickdesaulniers deleted the no_cpp_in_tests branch January 9, 2025 21:41
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jan 9, 2025

LLVM Buildbot has detected a new failure on builder libc-x86_64-debian-fullbuild-dbg-asan running on libc-x86_64-debian-fullbuild while building libc at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/171/builds/13525

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
[ RUN      ] LlvmLibcHeapSortTest.UnsortedTwoElementArray2
[       OK ] LlvmLibcHeapSortTest.UnsortedTwoElementArray2 (3 us)
[ RUN      ] LlvmLibcHeapSortTest.SameElementTwoElementArray
[       OK ] LlvmLibcHeapSortTest.SameElementTwoElementArray (2 us)
[ RUN      ] LlvmLibcHeapSortTest.SingleElementArray
[       OK ] LlvmLibcHeapSortTest.SingleElementArray (2 us)
[ RUN      ] LlvmLibcHeapSortTest.DifferentElemSizeArray
[       OK ] LlvmLibcHeapSortTest.DifferentElemSizeArray (207 ms)
Ran 17 tests.  PASS: 17  FAIL: 0
[1824/2251] Running unit test libc.test.src.stdlib.abort_test.__unit__
FAILED: libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.abort_test.__unit__ /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.abort_test.__unit__ 
cd /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/libc/test/src/stdlib && /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/libc/test/src/stdlib/libc.test.src.stdlib.abort_test.__unit__.__build__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcStdlib.abort
[       OK ] LlvmLibcStdlib.abort (807 us)
Ran 1 tests.  PASS: 1  FAIL: 0

=================================================================
==3116111==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x56155703cfed in operator new(unsigned long) (/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/libc/test/src/stdlib/libc.test.src.stdlib.abort_test.__unit__.__build__+0xe1fed) (BuildId: 4e95bc7b527c35776e340cd98f8ae73514093ad1)
    #1 0x56155703f845 in __llvm_libc_20_0_0_git::testutils::FunctionCaller* __llvm_libc_20_0_0_git::testing::Test::createCallable<LlvmLibcStdlib_abort::Run()::$_0>(LlvmLibcStdlib_abort::Run()::$_0) /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/llvm-project/libc/test/UnitTest/LibcTest.h:221:12
    #2 0x56155703f6c4 in LlvmLibcStdlib_abort::Run() /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/llvm-project/libc/test/src/stdlib/abort_test.cpp:17:3
    #3 0x561557048287 in __llvm_libc_20_0_0_git::testing::Test::runTests(__llvm_libc_20_0_0_git::testing::TestOptions const&) /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/llvm-project/libc/test/UnitTest/LibcTest.cpp:165:8
    #4 0x561557084638 in main /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/llvm-project/libc/test/UnitTest/LibcTestMain.cpp:59:10
    #5 0x7fe865947249 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

SUMMARY: AddressSanitizer: 16 byte(s) leaked in 1 allocation(s).
[1825/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.memrchr_test.__unit__.__build__
[1826/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.stpcpy_test.__unit__.__build__
[1827/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.stpncpy_test.__unit__.__build__
[1828/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcmp_test.__unit__.__build__
[1829/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strerror_r_test.__unit__.__build__
[1830/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strchr_test.__unit__.__build__
[1831/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcat_test.__unit__.__build__
[1832/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strchrnul_test.__unit__.__build__
[1833/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcspn_test.__unit__.__build__
[1834/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcoll_test.__unit__.__build__
[1835/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcasestr_test.__unit__.__build__
[1836/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strerror_test.__unit__.__build__
[1837/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strdup_test.__unit__.__build__
[1838/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcpy_test.__unit__.__build__
[1839/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strlcpy_test.__unit__.__build__
[1840/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strlcat_test.__unit__.__build__
[1841/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strndup_test.__unit__.__build__
[1842/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strncpy_test.__unit__.__build__
[1843/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strlen_test.__unit__.__build__
[1844/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strncat_test.__unit__.__build__
Step 8 (libc-unit-tests) failure: libc-unit-tests (failure)
...
[ RUN      ] LlvmLibcHeapSortTest.UnsortedTwoElementArray2
[       OK ] LlvmLibcHeapSortTest.UnsortedTwoElementArray2 (3 us)
[ RUN      ] LlvmLibcHeapSortTest.SameElementTwoElementArray
[       OK ] LlvmLibcHeapSortTest.SameElementTwoElementArray (2 us)
[ RUN      ] LlvmLibcHeapSortTest.SingleElementArray
[       OK ] LlvmLibcHeapSortTest.SingleElementArray (2 us)
[ RUN      ] LlvmLibcHeapSortTest.DifferentElemSizeArray
[       OK ] LlvmLibcHeapSortTest.DifferentElemSizeArray (207 ms)
Ran 17 tests.  PASS: 17  FAIL: 0
[1824/2251] Running unit test libc.test.src.stdlib.abort_test.__unit__
FAILED: libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.abort_test.__unit__ /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.abort_test.__unit__ 
cd /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/libc/test/src/stdlib && /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/libc/test/src/stdlib/libc.test.src.stdlib.abort_test.__unit__.__build__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcStdlib.abort
[       OK ] LlvmLibcStdlib.abort (807 us)
Ran 1 tests.  PASS: 1  FAIL: 0

=================================================================
==3116111==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x56155703cfed in operator new(unsigned long) (/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/libc/test/src/stdlib/libc.test.src.stdlib.abort_test.__unit__.__build__+0xe1fed) (BuildId: 4e95bc7b527c35776e340cd98f8ae73514093ad1)
    #1 0x56155703f845 in __llvm_libc_20_0_0_git::testutils::FunctionCaller* __llvm_libc_20_0_0_git::testing::Test::createCallable<LlvmLibcStdlib_abort::Run()::$_0>(LlvmLibcStdlib_abort::Run()::$_0) /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/llvm-project/libc/test/UnitTest/LibcTest.h:221:12
    #2 0x56155703f6c4 in LlvmLibcStdlib_abort::Run() /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/llvm-project/libc/test/src/stdlib/abort_test.cpp:17:3
    #3 0x561557048287 in __llvm_libc_20_0_0_git::testing::Test::runTests(__llvm_libc_20_0_0_git::testing::TestOptions const&) /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/llvm-project/libc/test/UnitTest/LibcTest.cpp:165:8
    #4 0x561557084638 in main /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/llvm-project/libc/test/UnitTest/LibcTestMain.cpp:59:10
    #5 0x7fe865947249 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

SUMMARY: AddressSanitizer: 16 byte(s) leaked in 1 allocation(s).
[1825/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.memrchr_test.__unit__.__build__
[1826/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.stpcpy_test.__unit__.__build__
[1827/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.stpncpy_test.__unit__.__build__
[1828/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcmp_test.__unit__.__build__
[1829/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strerror_r_test.__unit__.__build__
[1830/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strchr_test.__unit__.__build__
[1831/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcat_test.__unit__.__build__
[1832/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strchrnul_test.__unit__.__build__
[1833/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcspn_test.__unit__.__build__
[1834/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcoll_test.__unit__.__build__
[1835/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcasestr_test.__unit__.__build__
[1836/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strerror_test.__unit__.__build__
[1837/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strdup_test.__unit__.__build__
[1838/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strcpy_test.__unit__.__build__
[1839/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strlcpy_test.__unit__.__build__
[1840/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strlcat_test.__unit__.__build__
[1841/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strndup_test.__unit__.__build__
[1842/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strncpy_test.__unit__.__build__
[1843/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strlen_test.__unit__.__build__
[1844/2251] Linking CXX executable libc/test/src/string/libc.test.src.string.strncat_test.__unit__.__build__

@nickdesaulniers
Copy link
Member Author

ah, that's why they were using std::unique_ptr. I guess I can manually free these callbacks.

nickdesaulniers added a commit to nickdesaulniers/llvm-project that referenced this pull request Jan 9, 2025
Looks like the smart pointer I removed was being used to free the underlying
object.

Fixes: llvm#122369
@nickdesaulniers
Copy link
Member Author

#122378 should fix this

nickdesaulniers added a commit that referenced this pull request Jan 9, 2025
Looks like the smart pointer I removed was being used to free the underlying
object.

Fixes: #122369
nickdesaulniers added a commit to nickdesaulniers/llvm-project that referenced this pull request Jan 9, 2025
These were created with operator new (see `Test::createCallable`), so operator
delete should be used instead of free().

Fixes: llvm#122369
Fixes: llvm#122378
nickdesaulniers added a commit that referenced this pull request Jan 9, 2025
These were created with operator new (see `Test::createCallable`), so operator
delete should be used instead of free().

Fixes: #122369
Fixes: #122378
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.

5 participants