From 78af17e3b301ebbe3b8de5f94a543a46b4fc6932 Mon Sep 17 00:00:00 2001 From: Daan Date: Fri, 25 Nov 2022 14:03:00 -0800 Subject: [PATCH 1/2] add extra alignment test --- test/test-api.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test-api.c b/test/test-api.c index 65578287..f202e7c1 100644 --- a/test/test-api.c +++ b/test/test-api.c @@ -191,6 +191,20 @@ int main(void) { } result = ok; }; + CHECK_BODY("malloc-aligned10") { + bool ok = true; + void* p[10+1]; + int align; + int j; + for(j = 0, align = 1; j <= 10 && ok; align *= 2, j++ ) { + p[j] = mi_malloc_aligned(43 + align, align); + ok = ((uintptr_t)p[j] % align) == 0; + } + for ( ; j > 0; j--) { + mi_free(p[j-1]); + } + result = ok; + } CHECK_BODY("malloc-aligned-at1") { void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p); }; From 604d4b259b5dafa57ae52433847e3f06185b01db Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Fri, 25 Nov 2022 14:27:48 -0800 Subject: [PATCH 2/2] fix assertion failure (issue #650) --- src/alloc-aligned.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/alloc-aligned.c b/src/alloc-aligned.c index b72600ea..73d5524c 100644 --- a/src/alloc-aligned.c +++ b/src/alloc-aligned.c @@ -56,9 +56,10 @@ static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_fallback(mi_heap_t* } // .. and align within the allocation - uintptr_t adjust = alignment - (((uintptr_t)p + offset) & align_mask); - mi_assert_internal(adjust <= alignment); - void* aligned_p = (adjust == alignment ? p : (void*)((uintptr_t)p + adjust)); + const uintptr_t poffset = ((uintptr_t)p + offset) & align_mask; + const uintptr_t adjust = (poffset == 0 ? 0 : alignment - poffset); + mi_assert_internal(adjust < alignment); + void* aligned_p = (void*)((uintptr_t)p + adjust); if (aligned_p != p) { mi_page_set_has_aligned(_mi_ptr_page(p), true); }