Drop prefix from own memcpy/memset/memcmp

C compiler might generate calls to memcpy, memset, memcmp, and memmove
as it seem fit (so far we haven't seen memmove being required). That
means we need to provide our own versions of it for code which is not
linked to a libc.

We already have a solution for that in commit bdf6051
("pie: provide memcpy/memcmp/memset for noglibc case")
but we faced another problem of compiler trying to optimize
our builtin_memset() by inserting calls to memset() which
is just an alias in our case and so it lead to infinite recursion.
This was workarounded in commit 8ea0ba7 ("string.h: fix memset
over-optimization with clang") but it's not clear that was a proper
fix.

This patch is considered to be the real solution. As we don't have
any other implementations of memset/memcpy/memcmp in non-libc case,
we can call ours without any prefixes and avoid using weak aliases.

Implementation notes:

1. mem*() functions code had to be moved from .h to .c for the functions
to be compatible with their prototypes declared in /usr/include/string.h
(i.e. "extern").

2. FORTIFY_SOURCE needed to be disabled for code not linked to libc,
because otherwise memcpy() may be replaced with a macro that expands
to __memcpy_chk() which of course can't be resolved during linking.

https://travis-ci.org/kolyshkin/criu/builds/198415449
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@virtuozzo.com>
This commit is contained in:
Kir Kolyshkin 2017-02-04 14:35:48 -08:00 committed by Pavel Emelyanov
parent 5abbb79124
commit 0367a1f6fe
26 changed files with 110 additions and 171 deletions

View file

@ -2,10 +2,6 @@
#error "The __sys macro is required"
#endif
#ifndef __memcpy
#error "The __memcpy macro is required"
#endif
static void scm_fdset_init_chunk(struct scm_fdset *fdset, int nr_fds,
void *data, unsigned ch_size)
{
@ -62,7 +58,7 @@ int send_fds(int sock, struct sockaddr_un *saddr, int len,
for (i = 0; i < nr_fds; i += min_fd) {
min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
scm_fdset_init_chunk(&fdset, min_fd, data, ch_size);
__memcpy(cmsg_data, &fds[i], sizeof(int) * min_fd);
memcpy(cmsg_data, &fds[i], sizeof(int) * min_fd);
ret = __sys(sendmsg)(sock, &fdset.hdr, 0);
if (ret <= 0)
@ -113,7 +109,7 @@ int __recv_fds(int sock, int *fds, int nr_fds, void *data, unsigned ch_size, int
if (unlikely(min_fd <= 0))
return -EBADFD;
__memcpy(&fds[i], cmsg_data, sizeof(int) * min_fd);
memcpy(&fds[i], cmsg_data, sizeof(int) * min_fd);
if (data)
data += ch_size * min_fd;
}