Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame^] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2011, Google Inc. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // Override mmap/munmap/mremap/sbrk to provide support for calling the |
| 32 | // related hooks (in addition, of course, to doing what these |
| 33 | // functions normally do). |
| 34 | |
| 35 | #ifndef __FreeBSD__ |
| 36 | # error Should only be including malloc_hook_mmap_freebsd.h on FreeBSD systems. |
| 37 | #endif |
| 38 | |
| 39 | #include <unistd.h> |
| 40 | #include <sys/syscall.h> |
| 41 | #include <sys/mman.h> |
| 42 | #include <errno.h> |
| 43 | #include <dlfcn.h> |
| 44 | |
| 45 | // Make sure mmap doesn't get #define'd away by <sys/mman.h> |
| 46 | #undef mmap |
| 47 | |
| 48 | // According to the FreeBSD documentation, use syscall if you do not |
| 49 | // need 64-bit alignment otherwise use __syscall. Indeed, syscall |
| 50 | // doesn't work correctly in most situations on 64-bit. It's return |
| 51 | // type is 'int' so for things like SYS_mmap, it actually truncates |
| 52 | // the returned address to 32-bits. |
| 53 | #if defined(__amd64__) || defined(__x86_64__) |
| 54 | # define MALLOC_HOOK_SYSCALL __syscall |
| 55 | #else |
| 56 | # define MALLOC_HOOK_SYSCALL syscall |
| 57 | #endif |
| 58 | |
| 59 | |
| 60 | extern "C" { |
| 61 | void* mmap(void *start, size_t length,int prot, int flags, |
| 62 | int fd, off_t offset) __THROW |
| 63 | ATTRIBUTE_SECTION(malloc_hook); |
| 64 | int munmap(void* start, size_t length) __THROW |
| 65 | ATTRIBUTE_SECTION(malloc_hook); |
| 66 | void* sbrk(intptr_t increment) __THROW |
| 67 | ATTRIBUTE_SECTION(malloc_hook); |
| 68 | } |
| 69 | |
| 70 | static inline void* do_mmap(void *start, size_t length, |
| 71 | int prot, int flags, |
| 72 | int fd, off_t offset) __THROW { |
| 73 | return (void *)MALLOC_HOOK_SYSCALL(SYS_mmap, |
| 74 | start, length, prot, flags, fd, offset); |
| 75 | } |
| 76 | |
| 77 | static inline void* do_sbrk(intptr_t increment) { |
| 78 | static void *(*libc_sbrk)(intptr_t); |
| 79 | if (libc_sbrk == NULL) |
| 80 | libc_sbrk = (void *(*)(intptr_t))dlsym(RTLD_NEXT, "sbrk"); |
| 81 | |
| 82 | return libc_sbrk(increment); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | extern "C" void* mmap(void *start, size_t length, int prot, int flags, |
| 87 | int fd, off_t offset) __THROW { |
| 88 | MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); |
| 89 | void *result; |
| 90 | if (!MallocHook::InvokeMmapReplacement( |
| 91 | start, length, prot, flags, fd, offset, &result)) { |
| 92 | result = do_mmap(start, length, prot, flags, fd, |
| 93 | static_cast<size_t>(offset)); // avoid sign extension |
| 94 | } |
| 95 | MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | extern "C" int munmap(void* start, size_t length) __THROW { |
| 100 | MallocHook::InvokeMunmapHook(start, length); |
| 101 | int result; |
| 102 | if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
| 103 | result = MALLOC_HOOK_SYSCALL(SYS_munmap, start, length); |
| 104 | } |
| 105 | |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | extern "C" void* sbrk(intptr_t increment) __THROW { |
| 110 | MallocHook::InvokePreSbrkHook(increment); |
| 111 | void *result = do_sbrk(increment); |
| 112 | MallocHook::InvokeSbrkHook(result, increment); |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, |
| 117 | int flags, int fd, off_t offset) { |
| 118 | void* result; |
| 119 | if (!MallocHook::InvokeMmapReplacement( |
| 120 | start, length, prot, flags, fd, offset, &result)) { |
| 121 | result = do_mmap(start, length, prot, flags, fd, offset); |
| 122 | } |
| 123 | |
| 124 | return result; |
| 125 | } |
| 126 | |
| 127 | /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { |
| 128 | int result; |
| 129 | if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
| 130 | result = MALLOC_HOOK_SYSCALL(SYS_munmap, start, length); |
| 131 | } |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | #undef MALLOC_HOOK_SYSCALL |