[libc-commits] [libc] 7b59fcb - [libc] Make string entrypoints mutualy exclusive.

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Fri Nov 26 08:33:28 PST 2021


Author: Siva Chandra Reddy
Date: 2021-11-26T16:29:22Z
New Revision: 7b59fcb7de2223d7d3956b750d32357e30f9acbf

URL: https://github.com/llvm/llvm-project/commit/7b59fcb7de2223d7d3956b750d32357e30f9acbf
DIFF: https://github.com/llvm/llvm-project/commit/7b59fcb7de2223d7d3956b750d32357e30f9acbf.diff

LOG: [libc] Make string entrypoints mutualy exclusive.

For example, strcpy does not pull memcpy now.

Reviewed By: gchatelet

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

Added: 
    

Modified: 
    libc/src/string/CMakeLists.txt
    libc/src/string/memmove.cpp
    libc/src/string/memory_utils/CMakeLists.txt
    libc/src/string/mempcpy.cpp
    libc/src/string/stpncpy.cpp
    libc/src/string/strcpy.cpp
    libc/src/string/strdup.cpp
    libc/src/string/strndup.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index d5cfb1742018f..84af6b144107b 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -24,7 +24,7 @@ add_entrypoint_object(
   HDRS
     mempcpy.h
   DEPENDS
-    libc.src.string.memcpy
+    .memory_utils.memcpy_implementation
 )
 
 add_entrypoint_object(
@@ -45,7 +45,7 @@ add_entrypoint_object(
     memmove.h
   DEPENDS
     libc.src.__support.integer_operations
-    libc.src.string.memcpy
+    .memory_utils.memcpy_implementation
 )
 
 add_entrypoint_object(
@@ -74,7 +74,7 @@ add_entrypoint_object(
   HDRS
     stpncpy.h
   DEPENDS
-    .bzero
+    .memory_utils.memset_implementation
 )
 
 add_entrypoint_object(
@@ -111,7 +111,7 @@ add_entrypoint_object(
   HDRS
     strcpy.h
   DEPENDS
-    .memcpy
+    .memory_utils.memcpy_implementation
     .string_utils
 )
 
@@ -132,7 +132,7 @@ add_entrypoint_object(
   HDRS
     strdup.h
   DEPENDS
-    .memcpy
+    .memory_utils.memcpy_implementation
     .string_utils
     libc.include.stdlib
 )
@@ -181,7 +181,7 @@ add_entrypoint_object(
   HDRS
     strndup.h
   DEPENDS
-    .memcpy
+    .memory_utils.memcpy_implementation
     .string_utils
     libc.include.stdlib
 )
@@ -317,7 +317,7 @@ function(add_bzero bzero_name)
     SRCS ${LIBC_SOURCE_DIR}/src/string/bzero.cpp
     HDRS ${LIBC_SOURCE_DIR}/src/string/bzero.h
     DEPENDS
-      .memory_utils.memory_utils
+      .memory_utils.memset_implementation
       libc.include.string
     COMPILE_OPTIONS
       -fno-builtin-bzero
@@ -346,7 +346,7 @@ function(add_memcmp memcmp_name)
     SRCS ${LIBC_SOURCE_DIR}/src/string/memcmp.cpp
     HDRS ${LIBC_SOURCE_DIR}/src/string/memcmp.h
     DEPENDS
-      .memory_utils.memory_utils
+      .memory_utils.memcmp_implementation
       libc.include.string
     COMPILE_OPTIONS
       -fno-builtin-memcmp
@@ -378,7 +378,7 @@ function(add_memcpy memcpy_name)
     SRCS ${LIBC_SOURCE_DIR}/src/string/memcpy.cpp
     HDRS ${LIBC_SOURCE_DIR}/src/string/memcpy.h
     DEPENDS
-      .memory_utils.memory_utils
+      .memory_utils.memcpy_implementation
       libc.include.string
     COMPILE_OPTIONS
       -fno-builtin-memcpy
@@ -413,7 +413,7 @@ function(add_memset memset_name)
     SRCS ${LIBC_SOURCE_DIR}/src/string/memset.cpp
     HDRS ${LIBC_SOURCE_DIR}/src/string/memset.h
     DEPENDS
-      .memory_utils.memory_utils
+      .memory_utils.memset_implementation
       libc.include.string
     COMPILE_OPTIONS
       -fno-builtin-memset

diff  --git a/libc/src/string/memmove.cpp b/libc/src/string/memmove.cpp
index 01353dace116d..d4c8e0bccd8a9 100644
--- a/libc/src/string/memmove.cpp
+++ b/libc/src/string/memmove.cpp
@@ -10,7 +10,7 @@
 
 #include "src/__support/common.h"
 #include "src/__support/integer_operations.h"
-#include "src/string/memcpy.h"
+#include "src/string/memory_utils/memcpy_implementations.h"
 #include <stddef.h> // size_t, ptr
diff _t
 
 namespace __llvm_libc {
@@ -40,8 +40,11 @@ LLVM_LIBC_FUNCTION(void *, memmove,
   // dest_c:[_____yz_]  [___yz___]  [__yz____]
 
   // Call `memcpy` if `src_c` and `dest_c` do not overlap.
-  if (__llvm_libc::integerAbs(src_c - dest_c) >= static_cast<ptr
diff _t>(count))
-    return __llvm_libc::memcpy(dest_c, src_c, count);
+  if (__llvm_libc::integerAbs(src_c - dest_c) >=
+      static_cast<ptr
diff _t>(count)) {
+    inline_memcpy(dest_c, src_c, count);
+    return dest_c;
+  }
 
   // Overlapping cases.
   // If `dest_c` starts before `src_c` (dest_c < src_c), copy

diff  --git a/libc/src/string/memory_utils/CMakeLists.txt b/libc/src/string/memory_utils/CMakeLists.txt
index 4a550e544dac8..f2bc09ec0cf59 100644
--- a/libc/src/string/memory_utils/CMakeLists.txt
+++ b/libc/src/string/memory_utils/CMakeLists.txt
@@ -4,3 +4,27 @@ add_header_library(
     utils.h
     elements.h
 )
+
+add_header_library(
+  memcpy_implementation
+  HDRS
+    memcpy_implementations.h
+  DEPS
+    .memory_utils
+)
+
+add_header_library(
+  memcmp_implementation
+  HDRS
+    memcmp_implementations.h
+  DEPS
+    .memory_utils
+)
+
+add_header_library(
+  memset_implementation
+  HDRS
+    memset_implementations.h
+  DEPS
+    .memory_utils
+)

diff  --git a/libc/src/string/mempcpy.cpp b/libc/src/string/mempcpy.cpp
index 08b2b80efe12e..f26bd64bee42a 100644
--- a/libc/src/string/mempcpy.cpp
+++ b/libc/src/string/mempcpy.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/string/mempcpy.h"
-#include "src/string/memcpy.h"
+#include "src/string/memory_utils/memcpy_implementations.h"
 
 #include "src/__support/common.h"
 #include <stddef.h> // For size_t.
@@ -17,10 +17,9 @@ namespace __llvm_libc {
 LLVM_LIBC_FUNCTION(void *, mempcpy,
                    (void *__restrict dest, const void *__restrict src,
                     size_t count)) {
-  void *result = __llvm_libc::memcpy(dest, src, count);
-  return result == nullptr
-             ? result
-             : static_cast<void *>(static_cast<char *>(result) + count);
+  char *result = reinterpret_cast<char *>(dest);
+  inline_memcpy(result, reinterpret_cast<const char *>(src), count);
+  return result + count;
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/string/stpncpy.cpp b/libc/src/string/stpncpy.cpp
index 374330ede2ca8..25e916251bad8 100644
--- a/libc/src/string/stpncpy.cpp
+++ b/libc/src/string/stpncpy.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/string/stpncpy.h"
-#include "src/string/bzero.h"
+#include "src/string/memory_utils/memset_implementations.h"
 
 #include "src/__support/common.h"
 
@@ -22,7 +22,7 @@ LLVM_LIBC_FUNCTION(char *, stpncpy,
     dest[i] = src[i];
   // When n>strlen(src), n-strlen(src) \0 are appended.
   if (n > i)
-    __llvm_libc::bzero(dest + i, n - i);
+    inline_memset(dest + i, 0, n - i);
   return dest + i;
 }
 

diff  --git a/libc/src/string/strcpy.cpp b/libc/src/string/strcpy.cpp
index d5b72d9d92f6f..e9832e52e5de6 100644
--- a/libc/src/string/strcpy.cpp
+++ b/libc/src/string/strcpy.cpp
@@ -7,29 +7,18 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/string/strcpy.h"
-#include "src/string/memcpy.h"
+#include "src/string/memory_utils/memcpy_implementations.h"
 #include "src/string/string_utils.h"
 
 #include "src/__support/common.h"
-#include "src/__support/sanitizer.h"
 
 namespace __llvm_libc {
 
 LLVM_LIBC_FUNCTION(char *, strcpy,
                    (char *__restrict dest, const char *__restrict src)) {
   size_t size = internal::string_length(src) + 1;
-  char *result = reinterpret_cast<char *>(__llvm_libc::memcpy(dest, src, size));
-
-  // In many libc uses, we do not want memcpy to be instrumented. Hence,
-  // we mark the destination as initialized.
-  //
-  // We do not want memcpy to be instrumented because compilers can potentially
-  // generate calls to memcpy. If the sanitizer business logic ends up with a
-  // compiler generated call to memcpy which is instrumented, then it will
-  // break the sanitizers.
-  SANITIZER_MEMORY_INITIALIZED(result, size);
-
-  return result;
+  inline_memcpy(dest, src, size);
+  return dest;
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/string/strdup.cpp b/libc/src/string/strdup.cpp
index cb28868188e0c..ddda3a63b1cb6 100644
--- a/libc/src/string/strdup.cpp
+++ b/libc/src/string/strdup.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/string/strdup.h"
-#include "src/string/memcpy.h"
+#include "src/string/memory_utils/memcpy_implementations.h"
 #include "src/string/string_utils.h"
 
 #include "src/__support/common.h"
@@ -25,8 +25,8 @@ LLVM_LIBC_FUNCTION(char *, strdup, (const char *src)) {
   if (dest == nullptr) {
     return nullptr;
   }
-  char *result = reinterpret_cast<char *>(__llvm_libc::memcpy(dest, src, len));
-  return result;
+  inline_memcpy(dest, src, len);
+  return dest;
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/string/strndup.cpp b/libc/src/string/strndup.cpp
index 6c904f4a646ca..d8e0818284722 100644
--- a/libc/src/string/strndup.cpp
+++ b/libc/src/string/strndup.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/string/strndup.h"
-#include "src/string/memcpy.h"
+#include "src/string/memory_utils/memcpy_implementations.h"
 #include "src/string/string_utils.h"
 
 #include "src/__support/common.h"
@@ -26,10 +26,9 @@ LLVM_LIBC_FUNCTION(char *, strndup, (const char *src, size_t size)) {
   char *dest = reinterpret_cast<char *>(::malloc(len + 1)); // NOLINT
   if (dest == nullptr)
     return nullptr;
-  char *result =
-      reinterpret_cast<char *>(__llvm_libc::memcpy(dest, src, len + 1));
-  result[len] = '\0';
-  return result;
+  inline_memcpy(dest, src, len + 1);
+  dest[len] = '\0';
+  return dest;
 }
 
 } // namespace __llvm_libc


        


More information about the libc-commits mailing list