[RFCPT] CONFIG_DEBUG_MEMCPY: memcpy() and overlapping areas

From: Alexey Dobriyan
Date: Sat Apr 21 2007 - 19:24:47 EST


Overlapping areas are subject to memmove() not memcpy().

CONFIG_DEBUG_MEMCPY will print pointers and length in question as well as
backtrace to logs. Also, it should help if one of 3 variables got
corrupted somehow and you were lucky enough.

So far it was booted successfully on one x86_64 box and spit a couple of test
overlapping memcpy() becktraces, which means it's ready.
---

arch/x86_64/lib/memmove.c | 8 ++++++++
include/asm-x86_64/string.h | 9 +++++++++
lib/Kconfig.debug | 6 ++++++
3 files changed, 23 insertions(+)

--- a/arch/x86_64/lib/memmove.c
+++ b/arch/x86_64/lib/memmove.c
@@ -9,7 +9,15 @@
void *memmove(void * dest,const void *src,size_t count)
{
if (dest < src) {
+#ifdef CONFIG_DEBUG_MEMCPY
+ char *d = dest;
+ const char *s = src;
+ while (count--)
+ *d++ = *s++;
+ return dest;
+#else
return memcpy(dest,src,count);
+#endif
} else {
char *p = (char *) dest + count;
char *s = (char *) src + count;
--- a/include/asm-x86_64/string.h
+++ b/include/asm-x86_64/string.h
@@ -3,6 +3,8 @@

#ifdef __KERNEL__

+#include <linux/kernel.h>
+
/* Written 2002 by Andi Kleen */

/* Only used for special circumstances. Stolen from i386/string.h */
@@ -32,6 +34,13 @@ return (to);
extern void *__memcpy(void *to, const void *from, size_t len);
static inline void *memcpy(void *dst, const void *src, size_t len)
{
+#ifdef CONFIG_DEBUG_MEMCPY
+ if ((src < dst && src + len > dst) ||
+ (src > dst && dst + len > src)) {
+ printk("memcpy(0x%p, 0x%p, %zu);\n", dst, src, len);
+ WARN_ON(1);
+ }
+#endif
if (__builtin_constant_p(len) && len >= 64)
return __memcpy(dst, src, len);
else
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -355,6 +355,12 @@ config DEBUG_LIST

If unsure, say N.

+config DEBUG_MEMCPY
+ bool "Debug memcpy() function calls"
+ depends on DEBUG_KERNEL
+ help
+ Enable checking for memcpy() on overlapping areas.
+
config FRAME_POINTER
bool "Compile the kernel with frame pointers"
depends on DEBUG_KERNEL && (X86 || CRIS || M68K || M68KNOMMU || FRV || UML || S390 || AVR32 || SUPERH)

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/