1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: Sanjay Ghemawat <opensource@google.com>
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.
39 # error Should only be including malloc_hook_mmap_linux.h on linux systems.
43 #if defined(__ANDROID__)
44 #include <sys/syscall.h>
45 #include <sys/linux-syscalls.h>
51 #include "base/linux_syscall_support.h"
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
59 #define SYS_mmap2 __NR_mmap2
62 #define SYS_munmap __NR_munmap
65 #define SYS_mremap __NR_mremap
67 typedef off64_t __off64_t
;
68 #endif // defined(__ANDROID__)
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().
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)
78 static inline void* do_mmap64(void *start
, size_t length
,
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
);
86 #define MALLOC_HOOK_HAVE_DO_MMAP64 1
88 #elif defined(__i386__) || defined(__PPC__) || defined(__mips__) || \
91 static inline void* do_mmap64(void *start
, size_t length
,
93 int fd
, __off64_t offset
) __THROW
{
96 // Try mmap2() unless it's not supported
97 static bool have_mmap2
= true;
99 static int pagesize
= 0;
100 if (!pagesize
) pagesize
= getpagesize();
102 // Check that the offset is page aligned
103 if (offset
& (pagesize
- 1)) {
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
;
114 // We don't have mmap2() after all - don't bother trying it in future
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.
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
,
132 result
= (void *)syscall(SYS_mmap
, args
);
135 // Some Linux ports like ARM EABI Linux has no mmap, just mmap2.
143 #define MALLOC_HOOK_HAVE_DO_MMAP64 1
145 #endif // #if defined(__x86_64__)
148 #ifdef MALLOC_HOOK_HAVE_DO_MMAP64
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
155 // Put all callers of MallocHook::Invoke* in this module into
156 // malloc_hook section,
157 // so that MallocHook::GetCallerStackTrace can function accurately:
159 // Make sure mmap doesn't get #define'd away by <sys/mman.h>
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
);
180 extern "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
);
184 if (!MallocHook::InvokeMmapReplacement(
185 start
, length
, prot
, flags
, fd
, offset
, &result
)) {
186 result
= do_mmap64(start
, length
, prot
, flags
, fd
, offset
);
188 MallocHook::InvokeMmapHook(result
, start
, length
, prot
, flags
, fd
, offset
);
192 # if !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH)
194 extern "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
);
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
203 MallocHook::InvokeMmapHook(result
, start
, length
, prot
, flags
, fd
, offset
);
207 # endif // !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH)
209 extern "C" int munmap(void* start
, size_t length
) __THROW
{
210 MallocHook::InvokeMunmapHook(start
, length
);
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
);
220 extern "C" void* mremap(void* old_addr
, size_t old_size
, size_t new_size
,
221 int flags
, ...) __THROW
{
224 void *new_address
= va_arg(ap
, void *);
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
,
235 // Don't hook sbrk() in Android, since it doesn't expose __sbrk.
236 #if !defined(__ANDROID__)
238 extern "C" void* __sbrk(ptrdiff_t increment
);
240 extern "C" void* sbrk(ptrdiff_t increment
) __THROW
{
241 MallocHook::InvokePreSbrkHook(increment
);
242 void *result
= __sbrk(increment
);
243 MallocHook::InvokeSbrkHook(result
, increment
);
246 #endif // !defined(__ANDROID__)
248 /*static*/void* MallocHook::UnhookedMMap(void *start
, size_t length
, int prot
,
249 int flags
, int fd
, off_t offset
) {
251 if (!MallocHook::InvokeMmapReplacement(
252 start
, length
, prot
, flags
, fd
, offset
, &result
)) {
253 result
= do_mmap64(start
, length
, prot
, flags
, fd
, offset
);
258 /*static*/int MallocHook::UnhookedMUnmap(void *start
, size_t length
) {
260 if (!MallocHook::InvokeMunmapReplacement(start
, length
, &result
)) {
261 result
= syscall(SYS_munmap
, start
, length
);
266 #undef MALLOC_HOOK_HAVE_DO_MMAP64
268 #endif // #ifdef MALLOC_HOOK_HAVE_DO_MMAP64