[libc-commits] [libc] 929ad8b - [libc] Add aliases to C memory functions for integration tests

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Wed Mar 15 18:15:13 PDT 2023


Author: Joseph Huber
Date: 2023-03-15T20:15:05-05:00
New Revision: 929ad8bc7cc212605f933e0e06fe60ead2e84b30

URL: https://github.com/llvm/llvm-project/commit/929ad8bc7cc212605f933e0e06fe60ead2e84b30
DIFF: https://github.com/llvm/llvm-project/commit/929ad8bc7cc212605f933e0e06fe60ead2e84b30.diff

LOG: [libc] Add aliases to C memory functions for integration tests

The integration tests require the C memory functions as the compiler may
emit calls to them directly. The tests normally use the `__internal__`
variant that is built for testing, but these memory functions were
linked directly to preserve the entrypoint. Instead, we forward delcare
the internal versions and map the entrypoints to them manually inside
the integration test. This allows us to use the internal versions of
these files like the rest of the test objects.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146177

Added: 
    

Modified: 
    libc/cmake/modules/LLVMLibCTestRules.cmake
    libc/test/IntegrationTest/test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index e4cc562dd1082..6e253745a6fc1 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -448,8 +448,8 @@ function(add_integration_test test_name)
       libc.src.stdlib.atexit
       libc.src.stdlib.exit
       libc.src.unistd.environ
-  )
-  list(APPEND memory_functions
+      # We always add the memory functions objects. This is because the
+      # compiler's codegen can emit calls to the C memory functions.
       libc.src.string.bcmp
       libc.src.string.bzero
       libc.src.string.memcmp
@@ -457,9 +457,6 @@ function(add_integration_test test_name)
       libc.src.string.memmove
       libc.src.string.memset
   )
-  # We remove the memory function deps because we want to explicitly add the
-  # object files which include the public symbols of the memory functions.
-  list(REMOVE_ITEM fq_deps_list ${memory_functions})
   list(REMOVE_DUPLICATES fq_deps_list)
 
   # TODO: Instead of gathering internal object files from entrypoints,
@@ -474,13 +471,6 @@ function(add_integration_test test_name)
     endif()
     return()
   endif()
-  # We add the memory functions objects explicitly. Note that we
-  # are adding objects of the targets which contain the public
-  # C symbols. This is because compiler codegen can emit calls to
-  # the C memory functions.
-  foreach(func IN LISTS memory_functions)
-    list(APPEND link_object_files $<TARGET_OBJECTS:${func}>)
-  endforeach()
   list(REMOVE_DUPLICATES link_object_files)
 
   # Make a library of all deps

diff  --git a/libc/test/IntegrationTest/test.cpp b/libc/test/IntegrationTest/test.cpp
index c52f9e6c295b7..0662b9a894e3a 100644
--- a/libc/test/IntegrationTest/test.cpp
+++ b/libc/test/IntegrationTest/test.cpp
@@ -9,6 +9,43 @@
 #include <stddef.h>
 #include <stdint.h>
 
+// Integration tests rely on the following memory functions. This is because the
+// compiler code generation can emit calls to them. We want to map the external
+// entrypoint to the internal implementation of the function used for testing.
+// This is done manually as not all targets support aliases.
+
+namespace __llvm_libc {
+
+int bcmp(const void *lhs, const void *rhs, size_t count);
+void bzero(void *ptr, size_t count);
+int memcmp(const void *lhs, const void *rhs, size_t count);
+void *memcpy(void *__restrict, const void *__restrict, size_t);
+void *memmove(void *dst, const void *src, size_t count);
+void *memset(void *ptr, int value, size_t count);
+
+} // namespace __llvm_libc
+
+extern "C" {
+
+int bcmp(const void *lhs, const void *rhs, size_t count) {
+  __llvm_libc::bcmp(lhs, rhs, count);
+}
+void bzero(void *ptr, size_t count) { __llvm_libc::bzero(ptr, count); }
+int memcmp(const void *lhs, const void *rhs, size_t count) {
+  __llvm_libc::memcmp(lhs, rhs, count);
+}
+void *memcpy(void *__restrict dst, const void *__restrict src, size_t count) {
+  __llvm_libc::memcpy(dst, src, count);
+}
+void *memmove(void *dst, const void *src, size_t count) {
+  __llvm_libc::memmove(dst, src, count);
+}
+void *memset(void *ptr, int value, size_t count) {
+  __llvm_libc::memset(ptr, value, count);
+}
+
+} // extern "C"
+
 // Integration tests cannot use the SCUDO standalone allocator as SCUDO pulls
 // various other parts of the libc. Since SCUDO development does not use
 // LLVM libc build rules, it is very hard to keep track or pull all that SCUDO


        


More information about the libc-commits mailing list