[libc] Make string entrypoints mutualy exclusive.

For example, strcpy does not pull memcpy now.

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D114300
This commit is contained in:
Siva Chandra Reddy 2021-11-26 16:29:22 +00:00 committed by Guillaume Chatelet
parent 562356d6e3
commit 7b59fcb7de
8 changed files with 56 additions and 42 deletions

View File

@ -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

View File

@ -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, ptrdiff_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<ptrdiff_t>(count))
return __llvm_libc::memcpy(dest_c, src_c, count);
if (__llvm_libc::integerAbs(src_c - dest_c) >=
static_cast<ptrdiff_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

View File

@ -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
)

View File

@ -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

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

@ -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