Browse Source

Resolve bug when using unusual MALLOC_ALIGNMENT

Safe-Linking alignment checks should be done on the user's buffer and not
the mchunkptr. The new check adds support for cases in which:
MALLOC_ALIGNMENT != 2*(sizeof(size_t))

The default case for both 32 bits and 64 bits was already supported, and
this patch adds support for the described irregular case.
Eyal Itkin 4 years ago
parent
commit
440e6c1197
1 changed files with 5 additions and 2 deletions
  1. 5 2
      libc/stdlib/malloc-standard/malloc.h

+ 5 - 2
libc/stdlib/malloc-standard/malloc.h

@@ -849,8 +849,11 @@ typedef struct malloc_chunk* mfastbinptr;
 */
 #define PROTECT_PTR(pos, ptr)     ((mchunkptr)((((size_t)pos) >> PAGE_SHIFT) ^ ((size_t)ptr)))
 #define REVEAL_PTR(pos, ptr)      PROTECT_PTR(pos, ptr)
-#define CHECK_PTR(P)    \
-  if (!aligned_OK(P))   \
+#define PTR_FOR_ALIGNMENT_CHECK(P) \
+    (MALLOC_ALIGNMENT == 2*(sizeof(size_t)) ? (P) : chunk2mem(P))
+
+#define CHECK_PTR(P)                            \
+  if (!aligned_OK(PTR_FOR_ALIGNMENT_CHECK(P)))  \
       abort();
 
 /*