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) 2005, 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 | // --- |
| 32 | // Author: Sanjay Ghemawat <opensource@google.com> |
| 33 | |
| 34 | // We define mmap() and mmap64(), which somewhat reimplements libc's mmap |
| 35 | // syscall stubs. Unfortunately libc only exports the stubs via weak symbols |
| 36 | // (which we're overriding with our mmap64() and mmap() wrappers) so we can't |
| 37 | // just call through to them. |
| 38 | |
| 39 | #ifndef __linux |
| 40 | # error Should only be including malloc_hook_mmap_linux.h on linux systems. |
| 41 | #endif |
| 42 | |
| 43 | #include <unistd.h> |
| 44 | #include <syscall.h> |
| 45 | #include <sys/mman.h> |
| 46 | #include <errno.h> |
| 47 | #include "base/linux_syscall_support.h" |
| 48 | |
| 49 | // The x86-32 case and the x86-64 case differ: |
| 50 | // 32b has a mmap2() syscall, 64b does not. |
| 51 | // 64b and 32b have different calling conventions for mmap(). |
| 52 | |
| 53 | // I test for 64-bit first so I don't have to do things like |
| 54 | // '#if (defined(__mips__) && !defined(__MIPS64__))' as a mips32 check. |
| 55 | #if defined(__x86_64__) || defined(__PPC64__) || defined(__aarch64__) || (defined(_MIPS_SIM) && _MIPS_SIM == _ABI64) |
| 56 | |
| 57 | static inline void* do_mmap64(void *start, size_t length, |
| 58 | int prot, int flags, |
| 59 | int fd, __off64_t offset) __THROW { |
| 60 | return sys_mmap(start, length, prot, flags, fd, offset); |
| 61 | } |
| 62 | |
| 63 | #define MALLOC_HOOK_HAVE_DO_MMAP64 1 |
| 64 | |
| 65 | #elif defined(__i386__) || defined(__PPC__) || defined(__mips__) || \ |
| 66 | defined(__arm__) |
| 67 | |
| 68 | static inline void* do_mmap64(void *start, size_t length, |
| 69 | int prot, int flags, |
| 70 | int fd, __off64_t offset) __THROW { |
| 71 | void *result; |
| 72 | |
| 73 | // Try mmap2() unless it's not supported |
| 74 | static bool have_mmap2 = true; |
| 75 | if (have_mmap2) { |
| 76 | static int pagesize = 0; |
| 77 | if (!pagesize) pagesize = getpagesize(); |
| 78 | |
| 79 | // Check that the offset is page aligned |
| 80 | if (offset & (pagesize - 1)) { |
| 81 | result = MAP_FAILED; |
| 82 | errno = EINVAL; |
| 83 | goto out; |
| 84 | } |
| 85 | |
| 86 | result = (void *)syscall(SYS_mmap2, |
| 87 | start, length, prot, flags, fd, |
| 88 | (off_t) (offset / pagesize)); |
| 89 | if (result != MAP_FAILED || errno != ENOSYS) goto out; |
| 90 | |
| 91 | // We don't have mmap2() after all - don't bother trying it in future |
| 92 | have_mmap2 = false; |
| 93 | } |
| 94 | |
| 95 | if (((off_t)offset) != offset) { |
| 96 | // If we're trying to map a 64-bit offset, fail now since we don't |
| 97 | // have 64-bit mmap() support. |
| 98 | result = MAP_FAILED; |
| 99 | errno = EINVAL; |
| 100 | goto out; |
| 101 | } |
| 102 | |
| 103 | #ifdef __NR_mmap |
| 104 | { |
| 105 | // Fall back to old 32-bit offset mmap() call |
| 106 | // Old syscall interface cannot handle six args, so pass in an array |
| 107 | int32 args[6] = { (int32) start, (int32) length, prot, flags, fd, |
| 108 | (int32)(off_t) offset }; |
| 109 | result = (void *)syscall(SYS_mmap, args); |
| 110 | } |
| 111 | #else |
| 112 | // Some Linux ports like ARM EABI Linux has no mmap, just mmap2. |
| 113 | result = MAP_FAILED; |
| 114 | #endif |
| 115 | |
| 116 | out: |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | #define MALLOC_HOOK_HAVE_DO_MMAP64 1 |
| 121 | |
| 122 | #endif // #if defined(__x86_64__) |
| 123 | |
| 124 | |
| 125 | #ifdef MALLOC_HOOK_HAVE_DO_MMAP64 |
| 126 | |
| 127 | // We use do_mmap64 abstraction to put MallocHook::InvokeMmapHook |
| 128 | // calls right into mmap and mmap64, so that the stack frames in the caller's |
| 129 | // stack are at the same offsets for all the calls of memory allocating |
| 130 | // functions. |
| 131 | |
| 132 | // Put all callers of MallocHook::Invoke* in this module into |
| 133 | // malloc_hook section, |
| 134 | // so that MallocHook::GetCallerStackTrace can function accurately: |
| 135 | |
| 136 | // Make sure mmap doesn't get #define'd away by <sys/mman.h> |
| 137 | # undef mmap |
| 138 | |
| 139 | extern "C" { |
| 140 | void* mmap64(void *start, size_t length, int prot, int flags, |
| 141 | int fd, __off64_t offset ) __THROW |
| 142 | ATTRIBUTE_SECTION(malloc_hook); |
| 143 | void* mmap(void *start, size_t length,int prot, int flags, |
| 144 | int fd, off_t offset) __THROW |
| 145 | ATTRIBUTE_SECTION(malloc_hook); |
| 146 | int munmap(void* start, size_t length) __THROW |
| 147 | ATTRIBUTE_SECTION(malloc_hook); |
| 148 | void* mremap(void* old_addr, size_t old_size, size_t new_size, |
| 149 | int flags, ...) __THROW |
| 150 | ATTRIBUTE_SECTION(malloc_hook); |
| 151 | void* sbrk(ptrdiff_t increment) __THROW |
| 152 | ATTRIBUTE_SECTION(malloc_hook); |
| 153 | } |
| 154 | |
| 155 | extern "C" void* mmap64(void *start, size_t length, int prot, int flags, |
| 156 | int fd, __off64_t offset) __THROW { |
| 157 | MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); |
| 158 | void *result; |
| 159 | if (!MallocHook::InvokeMmapReplacement( |
| 160 | start, length, prot, flags, fd, offset, &result)) { |
| 161 | result = do_mmap64(start, length, prot, flags, fd, offset); |
| 162 | } |
| 163 | MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); |
| 164 | return result; |
| 165 | } |
| 166 | |
| 167 | # if !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH) |
| 168 | |
| 169 | extern "C" void* mmap(void *start, size_t length, int prot, int flags, |
| 170 | int fd, off_t offset) __THROW { |
| 171 | MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); |
| 172 | void *result; |
| 173 | if (!MallocHook::InvokeMmapReplacement( |
| 174 | start, length, prot, flags, fd, offset, &result)) { |
| 175 | result = do_mmap64(start, length, prot, flags, fd, |
| 176 | static_cast<size_t>(offset)); // avoid sign extension |
| 177 | } |
| 178 | MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); |
| 179 | return result; |
| 180 | } |
| 181 | |
| 182 | # endif // !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH) |
| 183 | |
| 184 | extern "C" int munmap(void* start, size_t length) __THROW { |
| 185 | MallocHook::InvokeMunmapHook(start, length); |
| 186 | int result; |
| 187 | if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
| 188 | result = sys_munmap(start, length); |
| 189 | } |
| 190 | return result; |
| 191 | } |
| 192 | |
| 193 | extern "C" void* mremap(void* old_addr, size_t old_size, size_t new_size, |
| 194 | int flags, ...) __THROW { |
| 195 | va_list ap; |
| 196 | va_start(ap, flags); |
| 197 | void *new_address = va_arg(ap, void *); |
| 198 | va_end(ap); |
| 199 | void* result = sys_mremap(old_addr, old_size, new_size, flags, new_address); |
| 200 | MallocHook::InvokeMremapHook(result, old_addr, old_size, new_size, flags, |
| 201 | new_address); |
| 202 | return result; |
| 203 | } |
| 204 | |
| 205 | #ifndef __UCLIBC__ |
| 206 | // libc's version: |
| 207 | extern "C" void* __sbrk(ptrdiff_t increment); |
| 208 | |
| 209 | extern "C" void* sbrk(ptrdiff_t increment) __THROW { |
| 210 | MallocHook::InvokePreSbrkHook(increment); |
| 211 | void *result = __sbrk(increment); |
| 212 | MallocHook::InvokeSbrkHook(result, increment); |
| 213 | return result; |
| 214 | } |
| 215 | |
| 216 | #endif |
| 217 | |
| 218 | /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, |
| 219 | int flags, int fd, off_t offset) { |
| 220 | void* result; |
| 221 | if (!MallocHook::InvokeMmapReplacement( |
| 222 | start, length, prot, flags, fd, offset, &result)) { |
| 223 | result = do_mmap64(start, length, prot, flags, fd, offset); |
| 224 | } |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { |
| 229 | int result; |
| 230 | if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
| 231 | result = syscall(SYS_munmap, start, length); |
| 232 | } |
| 233 | return result; |
| 234 | } |
| 235 | |
| 236 | #undef MALLOC_HOOK_HAVE_DO_MMAP64 |
| 237 | |
| 238 | #endif // #ifdef MALLOC_HOOK_HAVE_DO_MMAP64 |