add support for init/fini array in main program, and greatly simplify
[musl] / src / malloc / malloc.c
index 88a31ae..4044eb2 100644 (file)
@@ -196,7 +196,11 @@ static int init_malloc(size_t n)
                return 0;
        }
 
-       mal.brk = __brk(0) + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
+       mal.brk = __brk(0);
+#ifdef SHARED
+       mal.brk = mal.brk + PAGE_SIZE-1 & -PAGE_SIZE;
+#endif
+       mal.brk = mal.brk + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
 
        c = expand_heap(n);
 
@@ -414,6 +418,9 @@ void *realloc(void *p, size_t n)
 
        next = NEXT_CHUNK(self);
 
+       /* Crash on corrupted footer (likely from buffer overflow) */
+       if (next->psize != self->csize) a_crash();
+
        /* Merge adjacent chunks if we need more space. This is not
         * a waste of time even if we fail to get enough space, because our
         * subsequent call to free would otherwise have to do the merge. */
@@ -467,6 +474,9 @@ void free(void *p)
        final_size = new_size = CHUNK_SIZE(self);
        next = NEXT_CHUNK(self);
 
+       /* Crash on corrupted footer (likely from buffer overflow) */
+       if (next->psize != self->csize) a_crash();
+
        for (;;) {
                /* Replace middle of large chunks with fresh zero pages */
                if (reclaim && (self->psize & next->csize & C_INUSE)) {