OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 // Override mmap/munmap/mremap/sbrk to provide support for calling the |
| 31 // related hooks (in addition, of course, to doing what these |
| 32 // functions normally do). |
| 33 |
| 34 #ifndef __FreeBSD__ |
| 35 # error Should only be including malloc_hook_mmap_freebsd.h on FreeBSD systems. |
| 36 #endif |
| 37 |
| 38 #include <unistd.h> |
| 39 #include <sys/syscall.h> |
| 40 #include <sys/mman.h> |
| 41 #include <errno.h> |
| 42 |
| 43 static inline void* do_mmap(void *start, size_t length, |
| 44 int prot, int flags, |
| 45 int fd, off_t offset) __THROW { |
| 46 return (void *)syscall(SYS_mmap, start, length, prot, flags, fd, offset); |
| 47 } |
| 48 |
| 49 // Make sure mmap doesn't get #define'd away by <sys/mman.h> |
| 50 #undef mmap |
| 51 |
| 52 extern "C" { |
| 53 void* mmap(void *start, size_t length,int prot, int flags, |
| 54 int fd, off_t offset) __THROW |
| 55 ATTRIBUTE_SECTION(malloc_hook); |
| 56 int munmap(void* start, size_t length) __THROW |
| 57 ATTRIBUTE_SECTION(malloc_hook); |
| 58 void* sbrk(intptr_t increment) __THROW |
| 59 ATTRIBUTE_SECTION(malloc_hook); |
| 60 } |
| 61 |
| 62 static inline void* do_sbrk(intptr_t increment) { |
| 63 void* curbrk; |
| 64 |
| 65 __asm__ __volatile__( |
| 66 "movl .curbrk, %%eax;" |
| 67 "movl %%eax, %0" |
| 68 : "=r" (curbrk) |
| 69 :: "%eax"); |
| 70 |
| 71 char* prevbrk = static_cast<char*>(curbrk); |
| 72 void* newbrk = prevbrk + increment; |
| 73 |
| 74 if (brk(newbrk) == -1) { |
| 75 assert(0); |
| 76 return reinterpret_cast<void*>(static_cast<intptr_t>(-1)); |
| 77 } |
| 78 |
| 79 return prevbrk; |
| 80 } |
| 81 |
| 82 |
| 83 extern "C" void* mmap(void *start, size_t length, int prot, int flags, |
| 84 int fd, off_t offset) __THROW { |
| 85 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); |
| 86 void *result = do_mmap(start, length, prot, flags, fd, |
| 87 static_cast<size_t>(offset)); // avoid sign extension |
| 88 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); |
| 89 return result; |
| 90 } |
| 91 |
| 92 extern "C" int munmap(void* start, size_t length) __THROW { |
| 93 MallocHook::InvokeMunmapHook(start, length); |
| 94 return syscall(SYS_munmap, start, length); |
| 95 } |
| 96 |
| 97 extern "C" void* sbrk(intptr_t increment) __THROW { |
| 98 MallocHook::InvokePreSbrkHook(increment); |
| 99 void *result = do_sbrk(increment); |
| 100 MallocHook::InvokeSbrkHook(result, increment); |
| 101 return result; |
| 102 } |
| 103 |
| 104 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, |
| 105 int flags, int fd, off_t offset) { |
| 106 return mmap(start, length, prot, flags, fd, offset); |
| 107 } |
| 108 |
| 109 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { |
| 110 return munmap(start, length); |
| 111 } |
OLD | NEW |