[Bf-blender-cvs] [f2cab8267f2] master: Memory: Fix guarded aligned malloc with small alignment

Jacques Lucke noreply at git.blender.org
Tue Aug 20 09:56:33 CEST 2019


Commit: f2cab8267f22293fc28de9a87f81b535b7955020
Author: Jacques Lucke
Date:   Tue Aug 20 09:45:47 2019 +0200
Branches: master
https://developer.blender.org/rBf2cab8267f22293fc28de9a87f81b535b7955020

Memory: Fix guarded aligned malloc with small alignment

When calling `MEM_guarded_mallocN_aligned` with an alignment of 4,
a pointer that was returned that is 4 byte but not 8 byte aligned.
When freeing this pointer, `MEM_guarded_freeN` thinks that it is an
illegal pointer, because it asserts that `((intptr_t)memh) & 0x7 == 0`.

The fix is to always use at least 8 byte alignment.

Reviewers: brecht

Differential Revision: https://developer.blender.org/D5529

===================================================================

M	intern/guardedalloc/intern/mallocn_guarded_impl.c

===================================================================

diff --git a/intern/guardedalloc/intern/mallocn_guarded_impl.c b/intern/guardedalloc/intern/mallocn_guarded_impl.c
index 8ba14b3887b..ef74f394fb1 100644
--- a/intern/guardedalloc/intern/mallocn_guarded_impl.c
+++ b/intern/guardedalloc/intern/mallocn_guarded_impl.c
@@ -552,7 +552,13 @@ void *MEM_guarded_malloc_arrayN(size_t len, size_t size, const char *str)
 
 void *MEM_guarded_mallocN_aligned(size_t len, size_t alignment, const char *str)
 {
-  MemHead *memh;
+  /* We only support alignment to a power of two. */
+  assert(IS_POW2(alignment));
+
+  /* Use a minimal alignment of 8. Otherwise MEM_guarded_freeN thinks it is an illegal pointer. */
+  if (alignment < 8) {
+    alignment = 8;
+  }
 
   /* It's possible that MemHead's size is not properly aligned,
    * do extra padding to deal with this.
@@ -567,13 +573,10 @@ void *MEM_guarded_mallocN_aligned(size_t len, size_t alignment, const char *str)
    */
   assert(alignment < 1024);
 
-  /* We only support alignment to a power of two. */
-  assert(IS_POW2(alignment));
-
   len = SIZET_ALIGN_4(len);
 
-  memh = (MemHead *)aligned_malloc(len + extra_padding + sizeof(MemHead) + sizeof(MemTail),
-                                   alignment);
+  MemHead *memh = (MemHead *)aligned_malloc(
+      len + extra_padding + sizeof(MemHead) + sizeof(MemTail), alignment);
 
   if (LIKELY(memh)) {
     /* We keep padding in the beginning of MemHead,



More information about the Bf-blender-cvs mailing list