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