[CRIU] [PATCH] Unify own memcpy/memset/memcmp

Kir Kolyshkin kir at openvz.org
Mon Jan 30 18:30:18 PST 2017


Currently, we have a few implementations of memcpy/memset/memcmp
functions:

 1. Ones that comes from a standard library (glibc/musl/whatever).
 2. Own implementation used by criu/pie/, named builtin_mem*
 3. (Optional) arch-optimized versions of above
    (memcpy for x86, memcpy and memcmp for power).
 4. Own implementation provided/used by compel plugins, named std_*

There are two reasons why 3-5 exist:
 a) code which is not linked to a libc (i.e. parasite/restorer and
    also compel plugins).
 b) C compiler, which might generate calls to memcpy, memset, memcmp,
    memmove calls as it seem fit (so far we haven't seen memmove
    being used by either gcc or clang on any arch supported)

So, having own mem* functions is required. What's messed up is
how many implementations and different names for those we have.

This commit is an attempt to untie the knot described.

A few implementation considerations:

First, let's stick to just one implementation, and have it in compel
std plugin.

Second, let's use the code from criu as it comes with arch-optimized
versions. Surely this means the patch is bigger.

Third, let's call the functions memcpy/memset/memcmp (i.e. no std_ or
builtin_ prefix, no aliases), as this is required by the compiler
(see "b" above) and we are not using libc in this code anyway, so no
names conflict. In the meantime, keep the std_ prefix for other functions
in compel std plugin as there are no implicit calls to those.

Notes:

 - FORTIFY_SOURCE had to be disabled for compel/plugins as in Ubuntu
   Trusty x86_64 it plays tricks with standard includes, replacing
   calls to memcpy() with __memcpy_chk().

 - builtin_strncmp() had to be moved to compel std plugin as
   criu/include/asm-generic/string.h is gone. Currently the only user
   is util-vdso.c

 - this is a long patch, and I'm not sure it can be split effectively
   without breaking git bisect.

Signed-off-by: Kir Kolyshkin <kir at openvz.org>
---
 compel/arch/aarch64/plugins/include/builtins.h     |  4 ++
 compel/arch/arm/plugins/include/builtins.h         |  4 ++
 compel/arch/ppc64/plugins/include/builtins.h       |  7 ++
 .../arch/ppc64/plugins/std/memcmp.S                |  2 +-
 .../arch/ppc64/plugins/std/memcpy.S                |  1 -
 compel/arch/x86/plugins/include/builtins.h         |  6 ++
 .../x86 => compel/arch/x86/plugins/std}/memcpy.S   |  2 -
 compel/plugins/Makefile                            | 12 +++-
 compel/plugins/include/uapi/std/string.h           |  7 +-
 compel/plugins/std/fds.c                           |  1 -
 compel/plugins/std/log.c                           |  6 +-
 compel/plugins/std/string.c                        | 57 +++++++++++++--
 compel/src/lib/infect.c                            |  1 -
 criu/arch/aarch64/include/asm/string.h             |  7 --
 criu/arch/aarch64/restorer.c                       |  1 -
 criu/arch/aarch64/vdso-pie.c                       |  1 -
 criu/arch/arm/include/asm/string.h                 |  7 --
 criu/arch/arm/restorer.c                           |  1 -
 criu/arch/ppc64/include/asm/string.h               | 28 --------
 criu/arch/ppc64/vdso-pie.c                         |  4 +-
 criu/arch/x86/include/asm/parasite.h               |  6 +-
 criu/arch/x86/include/asm/string.h                 | 21 ------
 criu/arch/x86/restorer.c                           |  4 +-
 criu/arch/x86/sigaction_compat.c                   |  4 +-
 criu/arch/x86/vdso-pie.c                           |  4 +-
 criu/include/asm-generic/string.h                  | 82 ----------------------
 criu/include/string.h                              |  2 -
 criu/pie/Makefile.library                          |  5 +-
 criu/pie/restorer.c                                |  3 +-
 criu/pie/util-vdso.c                               | 14 ++--
 include/common/scm-code.c                          |  8 +--
 31 files changed, 117 insertions(+), 195 deletions(-)
 create mode 100644 compel/arch/aarch64/plugins/include/builtins.h
 create mode 100644 compel/arch/arm/plugins/include/builtins.h
 create mode 100644 compel/arch/ppc64/plugins/include/builtins.h
 rename criu/arch/ppc64/memcmp_64.S => compel/arch/ppc64/plugins/std/memcmp.S (99%)
 rename criu/arch/ppc64/memcpy_power7.S => compel/arch/ppc64/plugins/std/memcpy.S (99%)
 create mode 100644 compel/arch/x86/plugins/include/builtins.h
 rename {criu/arch/x86 => compel/arch/x86/plugins/std}/memcpy.S (93%)
 delete mode 100644 criu/arch/aarch64/include/asm/string.h
 delete mode 100644 criu/arch/arm/include/asm/string.h
 delete mode 100644 criu/arch/ppc64/include/asm/string.h
 delete mode 100644 criu/arch/x86/include/asm/string.h
 delete mode 100644 criu/include/asm-generic/string.h

diff --git a/compel/arch/aarch64/plugins/include/builtins.h b/compel/arch/aarch64/plugins/include/builtins.h
new file mode 100644
index 0000000..cff8c0a
--- /dev/null
+++ b/compel/arch/aarch64/plugins/include/builtins.h
@@ -0,0 +1,4 @@
+#ifndef __COMPEL_BUILTINS_H
+#define __COMPEL_BUILTINS_H
+
+#endif /* __COMPEL_BUILTINS_H */
diff --git a/compel/arch/arm/plugins/include/builtins.h b/compel/arch/arm/plugins/include/builtins.h
new file mode 100644
index 0000000..cff8c0a
--- /dev/null
+++ b/compel/arch/arm/plugins/include/builtins.h
@@ -0,0 +1,4 @@
+#ifndef __COMPEL_BUILTINS_H
+#define __COMPEL_BUILTINS_H
+
+#endif /* __COMPEL_BUILTINS_H */
diff --git a/compel/arch/ppc64/plugins/include/builtins.h b/compel/arch/ppc64/plugins/include/builtins.h
new file mode 100644
index 0000000..2170f98
--- /dev/null
+++ b/compel/arch/ppc64/plugins/include/builtins.h
@@ -0,0 +1,7 @@
+#ifndef __COMPEL_BUILTINS_H
+#define __COMPEL_BUILTINS_H
+
+#define ARCH_HAS_MEMCPY
+#define ARCH_HAS_MEMCMP
+
+#endif /* __COMPEL_BUILTINS_H */
diff --git a/criu/arch/ppc64/memcmp_64.S b/compel/arch/ppc64/plugins/std/memcmp.S
similarity index 99%
rename from criu/arch/ppc64/memcmp_64.S
rename to compel/arch/ppc64/plugins/std/memcmp.S
index fd1bfb9..7f7fe91 100644
--- a/criu/arch/ppc64/memcmp_64.S
+++ b/compel/arch/ppc64/plugins/std/memcmp.S
@@ -31,7 +31,7 @@
 #define LD	ldx
 #endif
 
-ENTRY(builtin_memcmp)
+ENTRY(memcmp)
 	cmpdi	cr1,r5,0
 
 	/* Use the short loop if both strings are not 8B aligned */
diff --git a/criu/arch/ppc64/memcpy_power7.S b/compel/arch/ppc64/plugins/std/memcpy.S
similarity index 99%
rename from criu/arch/ppc64/memcpy_power7.S
rename to compel/arch/ppc64/plugins/std/memcpy.S
index e665723..e1afb7e 100644
--- a/criu/arch/ppc64/memcpy_power7.S
+++ b/compel/arch/ppc64/plugins/std/memcpy.S
@@ -28,7 +28,6 @@
  * service memcpy to initialise big local variable in the stack.
  */
 ENTRY(memcpy)
-ENTRY(memcpy_power7)
 	cmpldi	r5,16
 	std	r3,-STACKFRAMESIZE+STK_REG(R31)(r1)
 	blt	.Lshort_copy
diff --git a/compel/arch/x86/plugins/include/builtins.h b/compel/arch/x86/plugins/include/builtins.h
new file mode 100644
index 0000000..e2b211a
--- /dev/null
+++ b/compel/arch/x86/plugins/include/builtins.h
@@ -0,0 +1,6 @@
+#ifndef __COMPEL_BUILTINS_H
+#define __COMPEL_BUILTINS_H
+
+#define ARCH_HAS_MEMCPY
+
+#endif /* __COMPEL_BUILTINS_H */
diff --git a/criu/arch/x86/memcpy.S b/compel/arch/x86/plugins/std/memcpy.S
similarity index 93%
rename from criu/arch/x86/memcpy.S
rename to compel/arch/x86/plugins/std/memcpy.S
index c8f7555..2496cb9 100644
--- a/criu/arch/x86/memcpy.S
+++ b/compel/arch/x86/plugins/std/memcpy.S
@@ -16,7 +16,6 @@
  * Output:
  * rax original destination
  */
-ENTRY(memcpy_x86)
 ENTRY(memcpy)
 	movq %rdi, %rax
 	movq %rdx, %rcx
@@ -27,4 +26,3 @@ ENTRY(memcpy)
 	rep movsb
 	ret
 END(memcpy)
-END(memcpy_x86)
diff --git a/compel/plugins/Makefile b/compel/plugins/Makefile
index 5873240..d31fcf0 100644
--- a/compel/plugins/Makefile
+++ b/compel/plugins/Makefile
@@ -1,6 +1,7 @@
 .PHONY: .FORCE
 
-CFLAGS			:= $(filter-out -pg $(CFLAGS-GCOV),$(CFLAGS)) -DCR_NOGLIBC
+CFLAGS			:= $(filter-out -pg $(CFLAGS-GCOV),$(CFLAGS))
+CFLAGS			+= -DCR_NOGLIBC -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
 PLUGIN_ARCH_DIR		:= compel/arch/$(ARCH)/plugins
 
 #
@@ -47,6 +48,15 @@ std-obj-y		+= std/string.o
 std-obj-y		+= std/infect.o
 std-obj-y		+= ./$(PLUGIN_ARCH_DIR)/std/parasite-head.o
 
+ifeq ($(SRCARCH),x86)
+	std-obj-y	+= ./$(PLUGIN_ARCH_DIR)/std/memcpy.o
+endif
+
+ifeq ($(SRCARCH),ppc64)
+	std-obj-y	+= ./$(PLUGIN_ARCH_DIR)/std/memcpy.o
+	std-obj-y	+= ./$(PLUGIN_ARCH_DIR)/std/memcmp.o
+endif
+
 include ./$(PLUGIN_ARCH_DIR)/std/syscalls/Makefile.syscalls
 
 define syscall-priority
diff --git a/compel/plugins/include/uapi/std/string.h b/compel/plugins/include/uapi/std/string.h
index 8aec886..ddc1ea3 100644
--- a/compel/plugins/include/uapi/std/string.h
+++ b/compel/plugins/include/uapi/std/string.h
@@ -21,8 +21,11 @@ extern void __std_printf(int fd, const char *format, ...);
 #define std_putchar(c)		__std_putc(STDOUT_FILENO, c)
 
 extern unsigned long std_strtoul(const char *nptr, char **endptr, int base);
-extern void *std_memcpy(void *to, const void *from, unsigned int n);
-extern int std_memcmp(const void *cs, const void *ct, size_t count);
 extern int std_strcmp(const char *cs, const char *ct);
+extern int std_strncmp(const char *cs, const char *ct, size_t n);
+
+extern void *memcpy(void *dest, const void *src, size_t n);
+extern int memcmp(const void *s1, const void *s2, size_t n);
+extern void *memset(void *s, int c, size_t n);
 
 #endif /* COMPEL_PLUGIN_STD_STRING_H__ */
diff --git a/compel/plugins/std/fds.c b/compel/plugins/std/fds.c
index 8cfe6d2..aa83682 100644
--- a/compel/plugins/std/fds.c
+++ b/compel/plugins/std/fds.c
@@ -15,6 +15,5 @@
 #include "common/bug.h"
 
 #define __sys(foo)	sys_##foo
-#define __memcpy	std_memcpy
 
 #include "common/scm-code.c"
diff --git a/compel/plugins/std/log.c b/compel/plugins/std/log.c
index dbdbf58..52a3f1a 100644
--- a/compel/plugins/std/log.c
+++ b/compel/plugins/std/log.c
@@ -61,14 +61,14 @@ static void sbuf_log_init(struct simple_buf *b)
 		n = std_vprint_num(pbuf, sizeof(pbuf), (unsigned)now.tv_sec, &s);
 		pad_num(&s, &n, 2);
 		b->bp[0] = '(';
-		std_memcpy(b->bp + 1, s, n);
+		memcpy(b->bp + 1, s, n);
 		b->bp[n + 1] = '.';
 		b->bp += n + 2;
 
 		/* Mu-seconds */
 		n = std_vprint_num(pbuf, sizeof(pbuf), (unsigned)now.tv_usec, &s);
 		pad_num(&s, &n, 6);
-		std_memcpy(b->bp, s, n);
+		memcpy(b->bp, s, n);
 		b->bp[n] = ')';
 		b->bp += n + 1;
 	}
@@ -79,7 +79,7 @@ static void sbuf_log_init(struct simple_buf *b)
 	b->bp[2] = 'e';
 	b->bp[3] = ':';
 	b->bp[4] = ' ';
-	std_memcpy(b->bp + 5, s, n);
+	memcpy(b->bp + 5, s, n);
 	b->bp[n + 5] = ':';
 	b->bp[n + 6] = ' ';
 	b->bp += n + 7;
diff --git a/compel/plugins/std/string.c b/compel/plugins/std/string.c
index e987e40..87e3fed 100644
--- a/compel/plugins/std/string.c
+++ b/compel/plugins/std/string.c
@@ -5,6 +5,8 @@
 #include "uapi/std/syscall.h"
 #include "uapi/std/string.h"
 
+#include "builtins.h"
+
 static const char conv_tab[] = "0123456789abcdefghijklmnopqrstuvwxyz";
 
 void __std_putc(int fd, char c)
@@ -220,17 +222,33 @@ fin:
 	return neg ? (unsigned long)-num : (unsigned long)num;
 }
 
-void *std_memcpy(void *to, const void *from, unsigned int n)
+
+/* C compiler may generate calls to memcmp, memset, memcpy and memmove,
+ * so it relies on those to be available during linking.
+ * As the compel plugin code is not linked with libc,
+ * we must provide our own implementations of mem*() functions.
+ * Surely, these functions may also be used explicitly.
+ *
+ * For now, not having memmove() seems OK for both gcc and clang.
+ */
+
+#ifndef ARCH_HAS_MEMCPY
+void *memcpy(void *to, const void *from, size_t n)
 {
-	char *tmp = to;
-	const char *s = from;
+	size_t i;
+	unsigned char *cto = to;
+	const unsigned char *cfrom = from;
+
+	for (i = 0; i < n; ++i, ++cto, ++cfrom) {
+		*cto = *cfrom;
+	}
 
-	while (n--)
-		*tmp++ = *s++;
 	return to;
 }
+#endif
 
-int std_memcmp(const void *cs, const void *ct, size_t count)
+#ifndef ARCH_HAS_MEMCMP
+int memcmp(const void *cs, const void *ct, size_t count)
 {
 	const unsigned char *su1, *su2;
 	int res = 0;
@@ -240,6 +258,20 @@ int std_memcmp(const void *cs, const void *ct, size_t count)
 			break;
 	return res;
 }
+#endif
+
+#ifndef ARCH_HAS_MEMSET
+void *memset(void *s, const int c, size_t count)
+{
+	volatile char *dest = s;
+	size_t i = 0;
+
+	while (i < count)
+		dest[i++] = (char) c;
+
+	return s;
+}
+#endif
 
 int std_strcmp(const char *cs, const char *ct)
 {
@@ -255,3 +287,16 @@ int std_strcmp(const char *cs, const char *ct)
 	}
 	return 0;
 }
+
+int std_strncmp(const char *cs, const char *ct, size_t count)
+{
+	size_t i;
+
+	for (i = 0; i < count; i++) {
+		if (cs[i] != ct[i])
+			return cs[i] < ct[i] ? -1 : 1;
+		if (!cs[i])
+			break;
+	}
+	return 0;
+}
diff --git a/compel/src/lib/infect.c b/compel/src/lib/infect.c
index 599c372..3aa4905 100644
--- a/compel/src/lib/infect.c
+++ b/compel/src/lib/infect.c
@@ -30,7 +30,6 @@
 #include "infect-util.h"
 
 #define __sys(foo)	foo
-#define __memcpy	memcpy
 
 #include "common/scm.h"
 #include "common/scm-code.c"
diff --git a/criu/arch/aarch64/include/asm/string.h b/criu/arch/aarch64/include/asm/string.h
deleted file mode 100644
index 020a8ec..0000000
--- a/criu/arch/aarch64/include/asm/string.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#ifndef __CR_ASM_STRING_H__
-#define __CR_ASM_STRING_H__
-
-#include "common/compiler.h"
-#include "asm-generic/string.h"
-
-#endif /* __CR_ASM_STRING_H__ */
diff --git a/criu/arch/aarch64/restorer.c b/criu/arch/aarch64/restorer.c
index d9fbead..ce9c1b4 100644
--- a/criu/arch/aarch64/restorer.c
+++ b/criu/arch/aarch64/restorer.c
@@ -2,7 +2,6 @@
 
 #include "restorer.h"
 #include "asm/restorer.h"
-#include "asm/string.h"
 
 #include <compel/plugins/std/syscall.h>
 #include "log.h"
diff --git a/criu/arch/aarch64/vdso-pie.c b/criu/arch/aarch64/vdso-pie.c
index e2dd253..55de8cb 100644
--- a/criu/arch/aarch64/vdso-pie.c
+++ b/criu/arch/aarch64/vdso-pie.c
@@ -1,6 +1,5 @@
 #include <unistd.h>
 
-#include "asm/string.h"
 #include "asm/types.h"
 
 #include <compel/plugins/std/syscall.h>
diff --git a/criu/arch/arm/include/asm/string.h b/criu/arch/arm/include/asm/string.h
deleted file mode 100644
index 020a8ec..0000000
--- a/criu/arch/arm/include/asm/string.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#ifndef __CR_ASM_STRING_H__
-#define __CR_ASM_STRING_H__
-
-#include "common/compiler.h"
-#include "asm-generic/string.h"
-
-#endif /* __CR_ASM_STRING_H__ */
diff --git a/criu/arch/arm/restorer.c b/criu/arch/arm/restorer.c
index 73fa307..21234b9 100644
--- a/criu/arch/arm/restorer.c
+++ b/criu/arch/arm/restorer.c
@@ -2,7 +2,6 @@
 
 #include "restorer.h"
 #include "asm/restorer.h"
-#include "asm/string.h"
 
 #include <compel/plugins/std/syscall.h>
 #include "log.h"
diff --git a/criu/arch/ppc64/include/asm/string.h b/criu/arch/ppc64/include/asm/string.h
deleted file mode 100644
index 93eb428..0000000
--- a/criu/arch/ppc64/include/asm/string.h
+++ /dev/null
@@ -1,28 +0,0 @@
-#ifndef __CR_ASM_STRING_H__
-#define __CR_ASM_STRING_H__
-
-#include "common/compiler.h"
-
-#define HAS_BUILTIN_MEMCPY
-#define HAS_BUILTIN_MEMCMP
-
-#include "asm-generic/string.h"
-
-#ifdef CR_NOGLIBC
-extern void memcpy_power7(void *to, const void *from, unsigned long n);
-static inline void *builtin_memcpy(void *to, const void *from, unsigned long n)
-{
-	if (n)
-		memcpy_power7(to, from, n);
-	return to;
-}
-extern int builtin_memcmp(const void *cs, const void *ct, size_t count);
-#else
-/*
- * When building with the C library, call its services
- */
-#define builtin_memcpy memcpy
-#define builtin_memcmp memcmp
-#endif
-
-#endif /* __CR_ASM_STRING_H__ */
diff --git a/criu/arch/ppc64/vdso-pie.c b/criu/arch/ppc64/vdso-pie.c
index 33f3dbd..f13ea4a 100644
--- a/criu/arch/ppc64/vdso-pie.c
+++ b/criu/arch/ppc64/vdso-pie.c
@@ -1,8 +1,8 @@
 #include <unistd.h>
 
-#include "asm/string.h"
 #include "asm/types.h"
 
+#include <compel/plugins/std/string.h>
 #include <compel/plugins/std/syscall.h>
 #include "parasite-vdso.h"
 #include "log.h"
@@ -104,7 +104,7 @@ static unsigned long put_trampoline(unsigned long at, struct vdso_symtable *sym)
 
 			pr_debug("Putting vDSO trampoline in %s at %lx\n",
 				 sym->symbols[i].name, trampoline);
-			builtin_memcpy((void *)trampoline, &vdso_trampoline,
+			memcpy((void *)trampoline, &vdso_trampoline,
 				       size);
 			invalidate_caches(trampoline);
 		}
diff --git a/criu/arch/x86/include/asm/parasite.h b/criu/arch/x86/include/asm/parasite.h
index 7b259d7..68fad09 100644
--- a/criu/arch/x86/include/asm/parasite.h
+++ b/criu/arch/x86/include/asm/parasite.h
@@ -1,7 +1,7 @@
 #ifndef __ASM_PARASITE_H__
 #define __ASM_PARASITE_H__
 
-#include "asm-generic/string.h"
+#include <compel/plugins/std/string.h>
 #include <compel/plugins/std/syscall-codes.h>
 #include "asm/compat.h"
 
@@ -68,11 +68,11 @@ static void arch_get_tls(tls_t *ptls)
 	{
 		user_desc_t *d = syscall_mem;
 
-		builtin_memset(d, 0, sizeof(user_desc_t));
+		memset(d, 0, sizeof(user_desc_t));
 		d->seg_not_present = 1;
 		d->entry_number = GDT_ENTRY_TLS_MIN + i;
 		arch_get_user_desc(d);
-		builtin_memcpy(&ptls->desc[i], d, sizeof(user_desc_t));
+		memcpy(&ptls->desc[i], d, sizeof(user_desc_t));
 	}
 
 	free_compat_syscall_stack(syscall_mem);
diff --git a/criu/arch/x86/include/asm/string.h b/criu/arch/x86/include/asm/string.h
deleted file mode 100644
index 415bf5c..0000000
--- a/criu/arch/x86/include/asm/string.h
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef __CR_ASM_STRING_H__
-#define __CR_ASM_STRING_H__
-
-#define HAS_BUILTIN_MEMCPY
-
-#include "common/compiler.h"
-#include "asm-generic/string.h"
-
-#ifdef CR_NOGLIBC
-extern void *memcpy_x86(void *to, const void *from, size_t n);
-static inline void *builtin_memcpy(void *to, const void *from, size_t n)
-{
-	if (n)
-		memcpy_x86(to, from, n);
-	return to;
-}
-#else
-#define builtin_memcpy memcpy
-#endif /* CR_NOGLIBC */
-
-#endif /* __CR_ASM_STRING_H__ */
diff --git a/criu/arch/x86/restorer.c b/criu/arch/x86/restorer.c
index 4510704..a4ddee5 100644
--- a/criu/arch/x86/restorer.c
+++ b/criu/arch/x86/restorer.c
@@ -5,8 +5,8 @@
 #include "restorer.h"
 #include "asm/restorer.h"
 #include <compel/asm/fpu.h>
-#include "asm/string.h"
 
+#include <compel/plugins/std/string.h>
 #include <compel/plugins/std/syscall.h>
 #include "log.h"
 #include "cpu.h"
@@ -71,7 +71,7 @@ void restore_tls(tls_t *ptls)
 		if (prepare_stack32(&stack32) < 0)
 			return;
 
-		builtin_memcpy(stack32, desc, sizeof(user_desc_t));
+		memcpy(stack32, desc, sizeof(user_desc_t));
 		asm volatile (
 		"       mov %1,%%eax                    \n"
 		"       mov %2,%%ebx                    \n"
diff --git a/criu/arch/x86/sigaction_compat.c b/criu/arch/x86/sigaction_compat.c
index 168d3dd..52f22a4 100644
--- a/criu/arch/x86/sigaction_compat.c
+++ b/criu/arch/x86/sigaction_compat.c
@@ -1,11 +1,11 @@
 #include "log.h"
 #include "asm/restorer.h"
 #include <compel/asm/fpu.h>
-#include "asm/string.h"
 #include "asm/compat.h"
 
 #ifdef CR_NOGLIBC
 # include <compel/plugins/std/syscall.h>
+# include <compel/plugins/std/string.h>
 #else
 # ifndef  __NR32_rt_sigaction
 #  define  __NR32_rt_sigaction 174
@@ -38,7 +38,7 @@ int arch_compat_rt_sigaction(void *stack32, int sig, rt_sigaction_t_compat *act)
 	 * To be sure, that sigaction pointer lies under 4G,
 	 * coping it on the bottom of the stack.
 	 */
-	builtin_memcpy(stack32, act, sizeof(rt_sigaction_t_compat));
+	memcpy(stack32, act, sizeof(rt_sigaction_t_compat));
 
 	asm volatile ("\t movl %%ebx,%%ebx\n" : :"b"(sig));	/* signum */
 	asm volatile ("\t movl %%ecx,%%ecx\n" : :"c"(stack32));	/* act */
diff --git a/criu/arch/x86/vdso-pie.c b/criu/arch/x86/vdso-pie.c
index 63f8d8d..24d4496 100644
--- a/criu/arch/x86/vdso-pie.c
+++ b/criu/arch/x86/vdso-pie.c
@@ -1,8 +1,8 @@
 #include <unistd.h>
 
-#include "asm/string.h"
 #include "asm/types.h"
 
+#include <compel/plugins/std/string.h>
 #include <compel/plugins/std/syscall.h>
 #include "parasite-vdso.h"
 #include "log.h"
@@ -59,7 +59,7 @@ int vdso_redirect_calls(unsigned long base_to, unsigned long base_from,
 			 base_to, to->symbols[i].offset, i);
 
 		IMMEDIATE(jmp) = base_to + to->symbols[i].offset;
-		builtin_memcpy((void *)(base_from + from->symbols[i].offset), &jmp, sizeof(jmp));
+		memcpy((void *)(base_from + from->symbols[i].offset), &jmp, sizeof(jmp));
 	}
 
 	return 0;
diff --git a/criu/include/asm-generic/string.h b/criu/include/asm-generic/string.h
deleted file mode 100644
index 6e0f659..0000000
--- a/criu/include/asm-generic/string.h
+++ /dev/null
@@ -1,82 +0,0 @@
-#ifndef __CR_ASM_GENERIC_STRING_H__
-#define __CR_ASM_GENERIC_STRING_H__
-
-#include "common/compiler.h"
-
-/* C compiler may generate calls to memcmp, memset, memcpy and memmove,
- * so it relies on those to be available during linking.
- * In case we are not linking our code against glibc, we set CR_NOGLIBC
- * and have to provide our own implementations of mem*() functions.
- *
- * For now, not having memmove() seems OK for both gcc and clang.
- */
-
-#ifndef HAS_BUILTIN_MEMCPY
-static __maybe_unused void *builtin_memcpy(void *to, const void *from, size_t n)
-{
-	size_t i;
-	unsigned char *cto = to;
-	const unsigned char *cfrom = from;
-
-	for (i = 0; i < n; ++i, ++cto, ++cfrom) {
-		*cto = *cfrom;
-	}
-
-	return to;
-}
-#ifdef CR_NOGLIBC
-void *memcpy(void *to, const void *from, size_t n)	\
-	     __attribute__ ((weak, alias ("builtin_memcpy")));
-#endif
-#endif
-
-#ifndef HAS_BUILTIN_MEMCMP
-static __maybe_unused int builtin_memcmp(const void *cs, const void *ct, size_t count)
-{
-	const unsigned char *su1, *su2;
-	int res = 0;
-
-	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
-		if ((res = *su1 - *su2) != 0)
-			break;
-	return res;
-}
-#ifdef CR_NOGLIBC
-int memcmp(const void *cs, const void *ct, size_t count)	\
-	     __attribute__ ((weak, alias ("builtin_memcmp")));
-#endif
-#endif
-
-#ifndef HAS_BUILTIN_STRNCMP
-static always_inline int builtin_strncmp(const char *cs, const char *ct, size_t count)
-{
-	size_t i;
-
-	for (i = 0; i < count; i++) {
-		if (cs[i] != ct[i])
-			return cs[i] < ct[i] ? -1 : 1;
-		if (!cs[i])
-			break;
-	}
-	return 0;
-}
-#endif
-
-#ifndef HAS_BUILTIN_MEMSET
-static __maybe_unused void *builtin_memset(void *s, const int c, size_t count)
-{
-	volatile char *dest = s;
-	size_t i = 0;
-
-	while (i < count)
-		dest[i++] = (char) c;
-
-	return s;
-}
-#ifdef CR_NOGLIBC
-void *memset(void *s, const int c, size_t count)	\
-	     __attribute__ ((weak, alias ("builtin_memset")));
-#endif
-#endif
-
-#endif /* __CR_ASM_GENERIC_STRING_H__ */
diff --git a/criu/include/string.h b/criu/include/string.h
index 53c0098..f9b4a38 100644
--- a/criu/include/string.h
+++ b/criu/include/string.h
@@ -2,8 +2,6 @@
 #define __CR_STRING_H__
 
 #include <sys/types.h>
-#include <string.h>
-#include "asm/string.h"
 
 #ifdef CONFIG_HAS_LIBBSD
 # include <bsd/string.h>
diff --git a/criu/pie/Makefile.library b/criu/pie/Makefile.library
index f6a1b42..726a41c 100644
--- a/criu/pie/Makefile.library
+++ b/criu/pie/Makefile.library
@@ -18,8 +18,7 @@ ifeq ($(VDSO),y)
 endif
 
 ifeq ($(SRCARCH),ppc64)
-        lib-y		+= ./$(ARCH_DIR)/memcpy_power7.o		\
-			   ./$(ARCH_DIR)/memcmp_64.o ./$(ARCH_DIR)/misc.o
+        lib-y		+= ./$(ARCH_DIR)/misc.o
 endif
 
 ifeq ($(SRCARCH),x86)
@@ -27,8 +26,6 @@ ifeq ($(SRCARCH),x86)
                 lib-y	+= util-vdso-elf32.o
         endif
         CFLAGS_util-vdso-elf32.o	+= -DCONFIG_VDSO_32
-
-        lib-y		+= ./$(ARCH_DIR)/memcpy.o
 endif
 
 #
diff --git a/criu/pie/restorer.c b/criu/pie/restorer.c
index e50dac8..3870434 100644
--- a/criu/pie/restorer.c
+++ b/criu/pie/restorer.c
@@ -22,7 +22,6 @@
 #include "int.h"
 #include "types.h"
 #include "common/compiler.h"
-#include "string.h"
 #include <compel/plugins/std/syscall.h>
 #include <compel/plugins/std/log.h>
 #include <compel/ksigset.h>
@@ -700,7 +699,7 @@ static int restore_aio_ring(struct rst_aio_ring *raio)
 
 populate:
 	i = offsetof(struct aio_ring, io_events);
-	builtin_memcpy((void *)ctx + i, (void *)ring + i, raio->len - i);
+	memcpy((void *)ctx + i, (void *)ring + i, raio->len - i);
 
 	/*
 	 * If we failed to get the proper nr_req right and
diff --git a/criu/pie/util-vdso.c b/criu/pie/util-vdso.c
index c02187c..ef87f07 100644
--- a/criu/pie/util-vdso.c
+++ b/criu/pie/util-vdso.c
@@ -10,13 +10,19 @@
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-#include "string.h"
 #include "image.h"
 #include "util-vdso.h"
 #include "vma.h"
 #include "log.h"
 #include "common/bug.h"
 
+#ifdef CR_NOGLIBC
+# include <compel/plugins/std/string.h>
+#else
+# include <string.h>
+# define std_strncmp strncmp
+#endif
+
 #ifdef LOG_PREFIX
 # undef LOG_PREFIX
 #endif
@@ -81,7 +87,7 @@ static int has_elf_identity(Ehdr_t *ehdr)
 
 	BUILD_BUG_ON(sizeof(elf_ident) != sizeof(ehdr->e_ident));
 
-	if (builtin_memcmp(ehdr->e_ident, elf_ident, sizeof(elf_ident)))
+	if (memcmp(ehdr->e_ident, elf_ident, sizeof(elf_ident)))
 		return false;
 	return true;
 }
@@ -239,10 +245,10 @@ static void parse_elf_symbols(uintptr_t mem, size_t size, Phdr_t *load,
 				continue;
 			name = (void *)addr;
 
-			if (builtin_strncmp(name, symbol, vdso_symbol_length))
+			if (std_strncmp(name, symbol, vdso_symbol_length))
 				continue;
 
-			builtin_memcpy(t->symbols[i].name, name, vdso_symbol_length);
+			memcpy(t->symbols[i].name, name, vdso_symbol_length);
 			t->symbols[i].offset = (unsigned long)sym->st_value - load->p_vaddr;
 			break;
 		}
diff --git a/include/common/scm-code.c b/include/common/scm-code.c
index 9808fed..df3b78a 100644
--- a/include/common/scm-code.c
+++ b/include/common/scm-code.c
@@ -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)
 {
@@ -63,7 +59,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)
@@ -115,7 +111,7 @@ int recv_fds(int sock, int *fds, int nr_fds, void *data, unsigned ch_size)
 		if (unlikely(min_fd <= 0))
 			return -1;
 
-		__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;
 	}
-- 
2.9.3



More information about the CRIU mailing list