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