X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fmalloc%2Flite_malloc.c;h=43a988fbb8adcc3c6469c21b9a4ad13018f2758b;hb=595416b11dfbc82d40a59d0edd7e3b04ba7a2d6d;hp=7643fc2c993b1e331f15c3fb5b2bb06d26d0ec17;hpb=56fbaa3bbe73f12af2bfbbcf2adb196e6f9fe264;p=musl diff --git a/src/malloc/lite_malloc.c b/src/malloc/lite_malloc.c index 7643fc2c..43a988fb 100644 --- a/src/malloc/lite_malloc.c +++ b/src/malloc/lite_malloc.c @@ -2,45 +2,117 @@ #include #include #include +#include #include "libc.h" - -uintptr_t __brk(uintptr_t); +#include "lock.h" +#include "syscall.h" +#include "fork_impl.h" #define ALIGN 16 -void *__simple_malloc(size_t n) +/* This function returns true if the interval [old,new] + * intersects the 'len'-sized interval below &libc.auxv + * (interpreted as the main-thread stack) or below &b + * (the current stack). It is used to defend against + * buggy brk implementations that can cross the stack. */ + +static int traverses_stack_p(uintptr_t old, uintptr_t new) { - static uintptr_t cur, brk; - uintptr_t base, new; - static volatile int lock[2]; + const uintptr_t len = 8<<20; + uintptr_t a, b; + + b = (uintptr_t)libc.auxv; + a = b > len ? b-len : 0; + if (new>a && old len ? b-len : 0; + if (new>a && old SIZE_MAX/2) goto toobig; + if (n > SIZE_MAX/2) { + errno = ENOMEM; + return 0; + } + if (!n) n++; while (align SIZE_MAX - PAGE_SIZE - base) goto fail; - if (base+n > brk) { - new = base+n + PAGE_SIZE-1 & -PAGE_SIZE; - if (__brk(new) != new) goto fail; - brk = new; + + cur += -cur & align-1; + + if (n > end-cur) { + size_t req = n - (end-cur) + PAGE_SIZE-1 & -PAGE_SIZE; + + if (!cur) { + brk = __syscall(SYS_brk, 0); + brk += -brk & PAGE_SIZE-1; + cur = end = brk; + } + + if (brk == end && req < SIZE_MAX-brk + && !traverses_stack_p(brk, brk+req) + && __syscall(SYS_brk, brk+req)==brk+req) { + brk = end += req; + } else { + int new_area = 0; + req = n + PAGE_SIZE-1 & -PAGE_SIZE; + /* Only make a new area rather than individual mmap + * if wasted space would be over 1/8 of the map. */ + if (req-n > req/8) { + /* Geometric area size growth up to 64 pages, + * bounding waste by 1/8 of the area. */ + size_t min = PAGE_SIZE<<(mmap_step/2); + if (min-n > end-cur) { + if (req < min) { + req = min; + if (mmap_step < 12) + mmap_step++; + } + new_area = 1; + } + } + void *mem = __mmap(0, req, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (mem == MAP_FAILED || !new_area) { + UNLOCK(lock); + return mem==MAP_FAILED ? 0 : mem; + } + cur = (uintptr_t)mem; + end = cur + req; + } } - cur = base+n; + + p = (void *)cur; + cur += n; UNLOCK(lock); + return p; +} - return (void *)base; +weak_alias(__simple_malloc, __libc_malloc_impl); -fail: - UNLOCK(lock); -toobig: - errno = ENOMEM; - return 0; +void *__libc_malloc(size_t n) +{ + return __libc_malloc_impl(n); +} + +static void *default_malloc(size_t n) +{ + return __libc_malloc_impl(n); } -weak_alias(__simple_malloc, malloc); +weak_alias(default_malloc, malloc);