From fa4b1c401da1ac381d4d72172825231b3d5518d9 Mon Sep 17 00:00:00 2001 From: Aliaksey Kandratsenka Date: Sun, 19 Jan 2014 22:37:44 -0800 Subject: [PATCH] issue-599: fixing FreeBSD issue with sbrk Applied patch by yurivict. It was wrong assembly specifically for FreeBSD in sbrk overriding code. --- src/malloc_hook_mmap_freebsd.h | 41 +++++----------------------------- 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/src/malloc_hook_mmap_freebsd.h b/src/malloc_hook_mmap_freebsd.h index ee62eec..8575dcc 100644 --- a/src/malloc_hook_mmap_freebsd.h +++ b/src/malloc_hook_mmap_freebsd.h @@ -40,6 +40,7 @@ #include #include #include +#include // Make sure mmap doesn't get #define'd away by #undef mmap @@ -74,43 +75,11 @@ static inline void* do_mmap(void *start, size_t length, } static inline void* do_sbrk(intptr_t increment) { - void* curbrk = 0; + static void *(*libc_sbrk)(intptr_t); + if (libc_sbrk == NULL) + libc_sbrk = (void *(*)(intptr_t))dlsym(RTLD_NEXT, "sbrk"); -#if defined(__x86_64__) || defined(__amd64__) -# ifdef PIC - __asm__ __volatile__( - "movq .curbrk@GOTPCREL(%%rip), %%rdx;" - "movq (%%rdx), %%rax;" - "movq %%rax, %0;" - : "=r" (curbrk) - :: "%rdx", "%rax"); -# else - __asm__ __volatile__( - "movq .curbrk(%%rip), %%rax;" - "movq %%rax, %0;" - : "=r" (curbrk) - :: "%rax"); -# endif -#else - __asm__ __volatile__( - "movl .curbrk, %%eax;" - "movl %%eax, %0;" - : "=r" (curbrk) - :: "%eax"); -#endif - - if (increment == 0) { - return curbrk; - } - - char* prevbrk = static_cast(curbrk); - void* newbrk = prevbrk + increment; - - if (brk(newbrk) == -1) { - return reinterpret_cast(static_cast(-1)); - } - - return prevbrk; + return libc_sbrk(increment); }