issue-599: fixing FreeBSD issue with sbrk

Applied patch by yurivict.

It was wrong assembly specifically for FreeBSD in sbrk overriding
code.
This commit is contained in:
Aliaksey Kandratsenka 2014-01-19 22:37:44 -08:00
parent 71a239e559
commit fa4b1c401d

View File

@ -40,6 +40,7 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <errno.h> #include <errno.h>
#include <dlfcn.h>
// Make sure mmap doesn't get #define'd away by <sys/mman.h> // Make sure mmap doesn't get #define'd away by <sys/mman.h>
#undef mmap #undef mmap
@ -74,43 +75,11 @@ static inline void* do_mmap(void *start, size_t length,
} }
static inline void* do_sbrk(intptr_t increment) { 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__) return libc_sbrk(increment);
# 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<char*>(curbrk);
void* newbrk = prevbrk + increment;
if (brk(newbrk) == -1) {
return reinterpret_cast<void*>(static_cast<intptr_t>(-1));
}
return prevbrk;
} }