Chromium Code Reviews| 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 | |
| 52 // I test for 64-bit first so I don't have to do things like | |
| 53 // '#if (defined(__mips__) && !defined(__MIPS64__))' as a mips32 check. | |
| 54 #if defined(__x86_64__) || defined(__PPC64__) || (defined(_MIPS_SIM) && _MIPS_SI M == _ABI64) | |
| 55 | |
| 56 static inline void* do_mmap64(void *start, size_t length, | |
| 57 int prot, int flags, | |
| 58 int fd, __off64_t offset) __THROW { | |
| 59 /* | |
| 60 return sys_mmap(start, length, prot, flags, fd, offset); | |
| 61 */ | |
|
Dai Mikurube (NOT FULLTIME)
2012/02/14 11:32:09
I know these comment-outs are redundant.
It may h
| |
| 62 return (void *)syscall(SYS_mmap, start, length, prot, flags, fd, offset); | |
| 63 } | |
| 64 | |
| 65 #define MALLOC_HOOK_HAVE_DO_MMAP64 1 | |
| 66 | |
| 67 #elif defined(__i386__) || defined(__PPC__) || defined(__mips__) || \ | |
| 68 defined(__arm__) | |
| 69 | |
| 70 static inline void* do_mmap64(void *start, size_t length, | |
| 71 int prot, int flags, | |
| 72 int fd, __off64_t offset) __THROW { | |
| 73 void *result; | |
| 74 | |
| 75 // Try mmap2() unless it's not supported | |
| 76 static bool have_mmap2 = true; | |
| 77 if (have_mmap2) { | |
| 78 static int pagesize = 0; | |
| 79 if (!pagesize) pagesize = getpagesize(); | |
| 80 | |
| 81 // Check that the offset is page aligned | |
| 82 if (offset & (pagesize - 1)) { | |
| 83 result = MAP_FAILED; | |
| 84 errno = EINVAL; | |
| 85 goto out; | |
| 86 } | |
| 87 | |
| 88 result = (void *)syscall(SYS_mmap2, | |
| 89 start, length, prot, flags, fd, | |
| 90 (off_t) (offset / pagesize)); | |
| 91 if (result != MAP_FAILED || errno != ENOSYS) goto out; | |
| 92 | |
| 93 // We don't have mmap2() after all - don't bother trying it in future | |
| 94 have_mmap2 = false; | |
| 95 } | |
| 96 | |
| 97 if (((off_t)offset) != offset) { | |
| 98 // If we're trying to map a 64-bit offset, fail now since we don't | |
| 99 // have 64-bit mmap() support. | |
| 100 result = MAP_FAILED; | |
| 101 errno = EINVAL; | |
| 102 goto out; | |
| 103 } | |
| 104 | |
| 105 #ifdef __NR_mmap | |
| 106 { | |
| 107 // Fall back to old 32-bit offset mmap() call | |
| 108 // Old syscall interface cannot handle six args, so pass in an array | |
| 109 int32 args[6] = { (int32) start, (int32) length, prot, flags, fd, | |
| 110 (off_t) offset }; | |
| 111 result = (void *)syscall(SYS_mmap, args); | |
| 112 } | |
| 113 #else | |
| 114 // Some Linux ports like ARM EABI Linux has no mmap, just mmap2. | |
| 115 result = MAP_FAILED; | |
| 116 #endif | |
| 117 | |
| 118 out: | |
| 119 return result; | |
| 120 } | |
| 121 | |
| 122 #define MALLOC_HOOK_HAVE_DO_MMAP64 1 | |
| 123 | |
| 124 #endif // #if defined(__x86_64__) | |
| 125 | |
| 126 | |
| 127 #ifdef MALLOC_HOOK_HAVE_DO_MMAP64 | |
| 128 | |
| 129 // We use do_mmap64 abstraction to put MallocHook::InvokeMmapHook | |
| 130 // calls right into mmap and mmap64, so that the stack frames in the caller's | |
| 131 // stack are at the same offsets for all the calls of memory allocating | |
| 132 // functions. | |
| 133 | |
| 134 // Put all callers of MallocHook::Invoke* in this module into | |
| 135 // malloc_hook section, | |
| 136 // so that MallocHook::GetCallerStackTrace can function accurately: | |
| 137 | |
| 138 // Make sure mmap doesn't get #define'd away by <sys/mman.h> | |
| 139 # undef mmap | |
| 140 | |
| 141 extern "C" { | |
| 142 void* mmap64(void *start, size_t length, int prot, int flags, | |
| 143 int fd, __off64_t offset ) __THROW | |
| 144 ATTRIBUTE_SECTION(malloc_hook); | |
| 145 void* mmap(void *start, size_t length,int prot, int flags, | |
| 146 int fd, off_t offset) __THROW | |
| 147 ATTRIBUTE_SECTION(malloc_hook); | |
| 148 int munmap(void* start, size_t length) __THROW | |
| 149 ATTRIBUTE_SECTION(malloc_hook); | |
| 150 void* mremap(void* old_addr, size_t old_size, size_t new_size, | |
| 151 int flags, ...) __THROW | |
| 152 ATTRIBUTE_SECTION(malloc_hook); | |
| 153 void* sbrk(ptrdiff_t increment) __THROW | |
| 154 ATTRIBUTE_SECTION(malloc_hook); | |
| 155 } | |
| 156 | |
| 157 extern "C" void* mmap64(void *start, size_t length, int prot, int flags, | |
| 158 int fd, __off64_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, offset); | |
| 164 } | |
| 165 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); | |
| 166 return result; | |
| 167 } | |
| 168 | |
| 169 # if !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH) | |
| 170 | |
| 171 extern "C" void* mmap(void *start, size_t length, int prot, int flags, | |
| 172 int fd, off_t offset) __THROW { | |
| 173 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); | |
| 174 void *result; | |
| 175 if (!MallocHook::InvokeMmapReplacement( | |
| 176 start, length, prot, flags, fd, offset, &result)) { | |
| 177 result = do_mmap64(start, length, prot, flags, fd, | |
| 178 static_cast<size_t>(offset)); // avoid sign extension | |
| 179 } | |
| 180 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); | |
| 181 return result; | |
| 182 } | |
| 183 | |
| 184 # endif // !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH) | |
| 185 | |
| 186 extern "C" int munmap(void* start, size_t length) __THROW { | |
| 187 MallocHook::InvokeMunmapHook(start, length); | |
| 188 int result; | |
| 189 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { | |
| 190 /* | |
| 191 result = sys_munmap(start, length); | |
| 192 */ | |
| 193 result = syscall(SYS_munmap, start, length); | |
| 194 } | |
| 195 return result; | |
| 196 } | |
| 197 | |
| 198 extern "C" void* mremap(void* old_addr, size_t old_size, size_t new_size, | |
| 199 int flags, ...) __THROW { | |
| 200 va_list ap; | |
| 201 va_start(ap, flags); | |
| 202 void *new_address = va_arg(ap, void *); | |
| 203 va_end(ap); | |
| 204 /* | |
| 205 void* result = sys_mremap(old_addr, old_size, new_size, flags, new_address); | |
| 206 */ | |
| 207 void* result = (void *)syscall( | |
| 208 SYS_mremap, old_addr, old_size, new_size, flags, new_address); | |
| 209 MallocHook::InvokeMremapHook(result, old_addr, old_size, new_size, flags, | |
| 210 new_address); | |
| 211 return result; | |
| 212 } | |
| 213 | |
| 214 // libc's version: | |
| 215 extern "C" void* __sbrk(ptrdiff_t increment); | |
| 216 | |
| 217 extern "C" void* sbrk(ptrdiff_t increment) __THROW { | |
| 218 MallocHook::InvokePreSbrkHook(increment); | |
| 219 void *result = __sbrk(increment); | |
| 220 MallocHook::InvokeSbrkHook(result, increment); | |
| 221 return result; | |
| 222 } | |
| 223 | |
| 224 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, | |
| 225 int flags, int fd, off_t offset) { | |
| 226 void* result; | |
| 227 if (!MallocHook::InvokeMmapReplacement( | |
| 228 start, length, prot, flags, fd, offset, &result)) { | |
| 229 result = do_mmap64(start, length, prot, flags, fd, offset); | |
| 230 } | |
| 231 return result; | |
| 232 } | |
| 233 | |
| 234 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { | |
| 235 int result; | |
| 236 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { | |
| 237 result = syscall(SYS_munmap, start, length); | |
| 238 } | |
| 239 return result; | |
| 240 } | |
| 241 | |
| 242 #undef MALLOC_HOOK_HAVE_DO_MMAP64 | |
| 243 | |
| 244 #endif // #ifdef MALLOC_HOOK_HAVE_DO_MMAP64 | |
| OLD | NEW |