15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011, Google Inc.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification, are permitted provided that the following conditions are
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met:
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions of source code must retain the above copyright
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notice, this list of conditions and the following disclaimer.
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions in binary form must reproduce the above
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the documentation and/or other materials provided with the
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// distribution.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contributors may be used to endorse or promote products derived from
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this software without specific prior written permission.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Override mmap/munmap/mremap/sbrk to provide support for calling the
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// related hooks (in addition, of course, to doing what these
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// functions normally do).
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef __FreeBSD__
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# error Should only be including malloc_hook_mmap_freebsd.h on FreeBSD systems.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unistd.h>
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/syscall.h>
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/mman.h>
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <errno.h>
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Make sure mmap doesn't get #define'd away by <sys/mman.h>
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef mmap
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// According to the FreeBSD documentation, use syscall if you do not
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// need 64-bit alignment otherwise use __syscall. Indeed, syscall
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// doesn't work correctly in most situations on 64-bit. It's return
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// type is 'int' so for things like SYS_mmap, it actually truncates
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the returned address to 32-bits.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(__amd64__) || defined(__x86_64__)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# define MALLOC_HOOK_SYSCALL __syscall
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# define MALLOC_HOOK_SYSCALL syscall
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* mmap(void *start, size_t length,int prot, int flags,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             int fd, off_t offset) __THROW
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ATTRIBUTE_SECTION(malloc_hook);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int munmap(void* start, size_t length) __THROW
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ATTRIBUTE_SECTION(malloc_hook);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* sbrk(intptr_t increment) __THROW
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ATTRIBUTE_SECTION(malloc_hook);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static inline void* do_mmap(void *start, size_t length,
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            int prot, int flags,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            int fd, off_t offset) __THROW {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return (void *)MALLOC_HOOK_SYSCALL(SYS_mmap,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     start, length, prot, flags, fd, offset);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static inline void* do_sbrk(intptr_t increment) {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* curbrk = 0;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(__x86_64__) || defined(__amd64__)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# ifdef PIC
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  __asm__ __volatile__(
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "movq .curbrk@GOTPCREL(%%rip), %%rdx;"
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "movq (%%rdx), %%rax;"
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "movq %%rax, %0;"
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : "=r" (curbrk)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      :: "%rdx", "%rax");
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# else
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  __asm__ __volatile__(
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "movq .curbrk(%%rip), %%rax;"
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "movq %%rax, %0;"
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : "=r" (curbrk)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      :: "%rax");
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# endif
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  __asm__ __volatile__(
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "movl .curbrk, %%eax;"
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "movl %%eax, %0;"
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : "=r" (curbrk)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      :: "%eax");
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (increment == 0) {
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return curbrk;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char* prevbrk = static_cast<char*>(curbrk);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* newbrk = prevbrk + increment;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (brk(newbrk) == -1) {
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return reinterpret_cast<void*>(static_cast<intptr_t>(-1));
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return prevbrk;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" void* mmap(void *start, size_t length, int prot, int flags,
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      int fd, off_t offset) __THROW {
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset);
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void *result;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!MallocHook::InvokeMmapReplacement(
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          start, length, prot, flags, fd, offset, &result)) {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    result = do_mmap(start, length, prot, flags, fd,
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       static_cast<size_t>(offset)); // avoid sign extension
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" int munmap(void* start, size_t length) __THROW {
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocHook::InvokeMunmapHook(start, length);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int result;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) {
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    result = MALLOC_HOOK_SYSCALL(SYS_munmap, start, length);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" void* sbrk(intptr_t increment) __THROW {
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocHook::InvokePreSbrkHook(increment);
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void *result = do_sbrk(increment);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocHook::InvokeSbrkHook(result, increment);
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot,
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         int flags, int fd, off_t offset) {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* result;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!MallocHook::InvokeMmapReplacement(
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	  start, length, prot, flags, fd, offset, &result)) {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    result = do_mmap(start, length, prot, flags, fd, offset);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) {
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int result;
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) {
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    result = MALLOC_HOOK_SYSCALL(SYS_munmap, start, length);
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef MALLOC_HOOK_SYSCALL
166