malloc_hook_mmap_linux.h 8.8 KB

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