[PATCH] xmalloc: correct _xrealloc() for large or changing alignment

Jan Beulich posted 1 patch 1 day, 2 hours ago
[PATCH] xmalloc: correct _xrealloc() for large or changing alignment
Posted by Jan Beulich 1 day, 2 hours ago
The curr_size calculation on the non-page-aligned path may include leading
padding; therefore that size cannot be passed unadjusted to memcpy() (or
the min() used in its invocation).

Further, for the "can be re-used" case on the non-page-aligned path, if
alignment changes from what was used for the original calculation, the new
pointer could differ from the original one. Hence memory contents may need
moving.

No in-tree users presently exercise either of the problem cases. Add some
self-testing to cover them.

Fixes: 50879e805edb ("xen/common: Introduce _xrealloc function")
Reported-by: Roger Pau Monné <roger.pau@amd.com>
Signed-off-by: Jan Beulich <jbeulich@suse.com>

--- a/xen/common/xmalloc_tlsf.c
+++ b/xen/common/xmalloc_tlsf.c
@@ -681,13 +681,20 @@ void *_xrealloc(void *ptr, unsigned long
 
         if ( tmp_size <= curr_size )
         {
+            void *p0 = p;
+
             /* Add alignment padding. */
             p = add_padding(p, align);
 
             ASSERT(!((unsigned long)p & (align - 1)));
 
-            return p;
+            if ( p == ptr )
+                return ptr;
+
+            return memmove(p, ptr, min(curr_size - (p - p0), size));
         }
+
+        curr_size -= ptr - p;
     }
 
     p = _xmalloc(size, align);
@@ -730,3 +737,73 @@ void xfree(void *p)
 
     xmem_pool_free(p, xenpool);
 }
+
+#ifdef CONFIG_SELF_TESTS
+
+#include <xen/init.h>
+
+static int cf_check __init selftest(void)
+{
+    unsigned char *ptr = _xmalloc(1, 1), *new;
+    unsigned long align;
+
+    if ( !ptr )
+        return -ENOMEM;
+
+    BUG_ON(!IS_ALIGNED((unsigned long)ptr, MEM_ALIGN));
+    ptr[0] = 'A';
+
+    new = _xrealloc(ptr, MEM_ALIGN, MEM_ALIGN);
+    if ( !new )
+        goto out;
+    BUG_ON(!IS_ALIGNED((unsigned long)new, MEM_ALIGN));
+    BUG_ON(new[0] != 'A');
+    memset(new + 1, 'B', MEM_ALIGN - 1);
+    ptr = new;
+
+    for ( align = MEM_ALIGN << 2; align < PAGE_SIZE; align <<= 1 )
+        if ( !IS_ALIGNED((unsigned long)ptr, align) )
+            break;
+    new = _xrealloc(ptr, MEM_ALIGN, align);
+    if ( !new )
+        goto out;
+    BUG_ON(!IS_ALIGNED((unsigned long)new, align));
+    BUG_ON(new[0] != 'A');
+    BUG_ON(memchr_inv(new + 1, 'B', MEM_ALIGN - 1));
+    ptr = new;
+
+    new = _xrealloc(ptr, align >>= 1, MEM_ALIGN);
+    if ( !new )
+        goto out;
+    BUG_ON(!IS_ALIGNED((unsigned long)new, MEM_ALIGN));
+    BUG_ON(new[0] != 'A');
+    BUG_ON(memchr_inv(new + 1, 'B', MEM_ALIGN - 1));
+    memset(new + MEM_ALIGN, 'C', align - MEM_ALIGN);
+    ptr = new;
+
+    new = _xrealloc(ptr, align << 1, MEM_ALIGN << 1);
+    if ( !new )
+        goto out;
+    BUG_ON(!IS_ALIGNED((unsigned long)new, MEM_ALIGN << 1));
+    BUG_ON(new[0] != 'A');
+    BUG_ON(memchr_inv(new + 1, 'B', MEM_ALIGN - 1));
+    BUG_ON(memchr_inv(new + MEM_ALIGN, 'C', align - MEM_ALIGN));
+    ptr = new;
+
+    new = _xrealloc(ptr, align, MEM_ALIGN);
+    if ( !new )
+        goto out;
+    BUG_ON(!IS_ALIGNED((unsigned long)new, MEM_ALIGN));
+    BUG_ON(new[0] != 'A');
+    BUG_ON(memchr_inv(new + 1, 'B', MEM_ALIGN - 1));
+    BUG_ON(memchr_inv(new + MEM_ALIGN, 'C', align - MEM_ALIGN));
+    ptr = new;
+
+ out:
+    xfree(ptr);
+
+    return new ? 0 : -ENOMEM;
+}
+presmp_initcall(selftest);
+
+#endif /* CONFIG_SELF_TESTS */