malloc_hook_mmap_freebsd.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // Override mmap/munmap/mremap/sbrk to provide support for calling the
  31. // related hooks (in addition, of course, to doing what these
  32. // functions normally do).
  33. #ifndef __FreeBSD__
  34. # error Should only be including malloc_hook_mmap_freebsd.h on FreeBSD systems.
  35. #endif
  36. #include <unistd.h>
  37. #include <sys/syscall.h>
  38. #include <sys/mman.h>
  39. #include <errno.h>
  40. #include <dlfcn.h>
  41. // Make sure mmap doesn't get #define'd away by <sys/mman.h>
  42. #undef mmap
  43. // According to the FreeBSD documentation, use syscall if you do not
  44. // need 64-bit alignment otherwise use __syscall. Indeed, syscall
  45. // doesn't work correctly in most situations on 64-bit. It's return
  46. // type is 'int' so for things like SYS_mmap, it actually truncates
  47. // the returned address to 32-bits.
  48. #if defined(__amd64__) || defined(__x86_64__)
  49. # define MALLOC_HOOK_SYSCALL __syscall
  50. #else
  51. # define MALLOC_HOOK_SYSCALL syscall
  52. #endif
  53. extern "C" {
  54. void* mmap(void *start, size_t length,int prot, int flags,
  55. int fd, off_t offset) __THROW
  56. ATTRIBUTE_SECTION(malloc_hook);
  57. int munmap(void* start, size_t length) __THROW
  58. ATTRIBUTE_SECTION(malloc_hook);
  59. void* sbrk(intptr_t increment) __THROW
  60. ATTRIBUTE_SECTION(malloc_hook);
  61. }
  62. static inline void* do_mmap(void *start, size_t length,
  63. int prot, int flags,
  64. int fd, off_t offset) __THROW {
  65. return (void *)MALLOC_HOOK_SYSCALL(SYS_mmap,
  66. start, length, prot, flags, fd, offset);
  67. }
  68. static inline void* do_sbrk(intptr_t increment) {
  69. static void *(*libc_sbrk)(intptr_t);
  70. if (libc_sbrk == NULL)
  71. libc_sbrk = (void *(*)(intptr_t))dlsym(RTLD_NEXT, "sbrk");
  72. return libc_sbrk(increment);
  73. }
  74. extern "C" void* mmap(void *start, size_t length, int prot, int flags,
  75. int fd, off_t offset) __THROW {
  76. MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset);
  77. void *result;
  78. if (!MallocHook::InvokeMmapReplacement(
  79. start, length, prot, flags, fd, offset, &result)) {
  80. result = do_mmap(start, length, prot, flags, fd,
  81. static_cast<size_t>(offset)); // avoid sign extension
  82. }
  83. MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset);
  84. return result;
  85. }
  86. extern "C" int munmap(void* start, size_t length) __THROW {
  87. MallocHook::InvokeMunmapHook(start, length);
  88. int result;
  89. if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) {
  90. result = MALLOC_HOOK_SYSCALL(SYS_munmap, start, length);
  91. }
  92. return result;
  93. }
  94. extern "C" void* sbrk(intptr_t increment) __THROW {
  95. MallocHook::InvokePreSbrkHook(increment);
  96. void *result = do_sbrk(increment);
  97. MallocHook::InvokeSbrkHook(result, increment);
  98. return result;
  99. }
  100. /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot,
  101. int flags, int fd, off_t offset) {
  102. void* result;
  103. if (!MallocHook::InvokeMmapReplacement(
  104. start, length, prot, flags, fd, offset, &result)) {
  105. result = do_mmap(start, length, prot, flags, fd, offset);
  106. }
  107. return result;
  108. }
  109. /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) {
  110. int result;
  111. if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) {
  112. result = MALLOC_HOOK_SYSCALL(SYS_munmap, start, length);
  113. }
  114. return result;
  115. }
  116. #undef MALLOC_HOOK_SYSCALL