|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment
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@xxxxxxx>
Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
---
v2: Also include xen/errno.h in selftest section. Fix block moving up in-
place due to higher alignment. Add respective case to self-test.
--- a/xen/common/xmalloc_tlsf.c
+++ b/xen/common/xmalloc_tlsf.c
@@ -578,17 +578,23 @@ static void *strip_padding(void *p)
return p;
}
-static void *add_padding(void *p, unsigned long align)
+static void *add_padding(void *p, unsigned long align, bool dry)
{
unsigned int pad;
if ( (pad = -(long)p & (align - 1)) != 0 )
{
void *q = p + pad;
- struct bhdr *b = q - BHDR_OVERHEAD;
ASSERT(q > p);
- b->size = pad | FREE_BLOCK;
+
+ if ( !dry )
+ {
+ struct bhdr *b = q - BHDR_OVERHEAD;
+
+ b->size = pad | FREE_BLOCK;
+ }
+
p = q;
}
@@ -622,7 +628,7 @@ void *_xmalloc(unsigned long size, unsig
return xmalloc_whole_pages(size - align + MEM_ALIGN, align);
/* Add alignment padding. */
- p = add_padding(p, align);
+ p = add_padding(p, align, false);
ASSERT(((unsigned long)p & (align - 1)) == 0);
return p;
@@ -681,13 +687,23 @@ void *_xrealloc(void *ptr, unsigned long
if ( tmp_size <= curr_size )
{
- /* Add alignment padding. */
- p = add_padding(p, align);
+ void *p0 = p;
+ /* Determine alignment padding. */
+ p = add_padding(p, align, true);
ASSERT(!((unsigned long)p & (align - 1)));
+ if ( p != ptr )
+ ptr = memmove(p, ptr, min(curr_size - (p - p0), size));
+
+ /* Add alignment padding. */
+ p = add_padding(p0, align, false);
+ ASSERT(p == ptr);
+
return p;
}
+
+ curr_size -= ptr - p;
}
p = _xmalloc(size, align);
@@ -730,3 +746,83 @@ void xfree(void *p)
xmem_pool_free(p, xenpool);
}
+
+#ifdef CONFIG_SELF_TESTS
+
+#include <xen/errno.h>
+#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 << 2);
+ if ( !new )
+ goto out;
+ BUG_ON(!IS_ALIGNED((unsigned long)new, MEM_ALIGN << 2));
+ 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 */
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |