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