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.
46 #include "base/linux_syscall_support.h"
48 // The x86-32 case and the x86-64 case differ:
49 // 32b has a mmap2() syscall, 64b does not.
50 // 64b and 32b have different calling conventions for mmap().
52 // I test for 64-bit first so I don't have to do things like
53 // '#if (defined(__mips__) && !defined(__MIPS64__))' as a mips32 check.
54 #if defined(__x86_64__) || defined(__PPC64__) || (defined(_MIPS_SIM) && _MIPS_SIM == _ABI64)
56 static inline void* do_mmap64(void *start
, size_t length
,
58 int fd
, __off64_t offset
) __THROW
{
59 return sys_mmap(start
, length
, prot
, flags
, fd
, offset
);
62 #define MALLOC_HOOK_HAVE_DO_MMAP64 1
64 #elif defined(__i386__) || defined(__PPC__) || defined(__mips__) || \
67 static inline void* do_mmap64(void *start
, size_t length
,
69 int fd
, __off64_t offset
) __THROW
{
72 // Try mmap2() unless it's not supported
73 static bool have_mmap2
= true;
75 static int pagesize
= 0;
76 if (!pagesize
) pagesize
= getpagesize();
78 // Check that the offset is page aligned
79 if (offset
& (pagesize
- 1)) {
85 result
= (void *)syscall(SYS_mmap2
,
86 start
, length
, prot
, flags
, fd
,
87 (off_t
) (offset
/ pagesize
));
88 if (result
!= MAP_FAILED
|| errno
!= ENOSYS
) goto out
;
90 // We don't have mmap2() after all - don't bother trying it in future
94 if (((off_t
)offset
) != offset
) {
95 // If we're trying to map a 64-bit offset, fail now since we don't
96 // have 64-bit mmap() support.
104 // Fall back to old 32-bit offset mmap() call
105 // Old syscall interface cannot handle six args, so pass in an array
106 int32 args
[6] = { (int32
) start
, (int32
) length
, prot
, flags
, fd
,
108 result
= (void *)syscall(SYS_mmap
, args
);
111 // Some Linux ports like ARM EABI Linux has no mmap, just mmap2.
119 #define MALLOC_HOOK_HAVE_DO_MMAP64 1
121 #endif // #if defined(__x86_64__)
124 #ifdef MALLOC_HOOK_HAVE_DO_MMAP64
126 // We use do_mmap64 abstraction to put MallocHook::InvokeMmapHook
127 // calls right into mmap and mmap64, so that the stack frames in the caller's
128 // stack are at the same offsets for all the calls of memory allocating
131 // Put all callers of MallocHook::Invoke* in this module into
132 // malloc_hook section,
133 // so that MallocHook::GetCallerStackTrace can function accurately:
135 // Make sure mmap doesn't get #define'd away by <sys/mman.h>
139 void* mmap64(void *start
, size_t length
, int prot
, int flags
,
140 int fd
, __off64_t offset
) __THROW
141 ATTRIBUTE_SECTION(malloc_hook
);
142 void* mmap(void *start
, size_t length
,int prot
, int flags
,
143 int fd
, off_t offset
) __THROW
144 ATTRIBUTE_SECTION(malloc_hook
);
145 int munmap(void* start
, size_t length
) __THROW
146 ATTRIBUTE_SECTION(malloc_hook
);
147 void* mremap(void* old_addr
, size_t old_size
, size_t new_size
,
148 int flags
, ...) __THROW
149 ATTRIBUTE_SECTION(malloc_hook
);
150 void* sbrk(ptrdiff_t increment
) __THROW
151 ATTRIBUTE_SECTION(malloc_hook
);
154 extern "C" void* mmap64(void *start
, size_t length
, int prot
, int flags
,
155 int fd
, __off64_t offset
) __THROW
{
156 MallocHook::InvokePreMmapHook(start
, length
, prot
, flags
, fd
, offset
);
158 if (!MallocHook::InvokeMmapReplacement(
159 start
, length
, prot
, flags
, fd
, offset
, &result
)) {
160 result
= do_mmap64(start
, length
, prot
, flags
, fd
, offset
);
162 MallocHook::InvokeMmapHook(result
, start
, length
, prot
, flags
, fd
, offset
);
166 # if !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH)
168 extern "C" void* mmap(void *start
, size_t length
, int prot
, int flags
,
169 int fd
, off_t offset
) __THROW
{
170 MallocHook::InvokePreMmapHook(start
, length
, prot
, flags
, fd
, offset
);
172 if (!MallocHook::InvokeMmapReplacement(
173 start
, length
, prot
, flags
, fd
, offset
, &result
)) {
174 result
= do_mmap64(start
, length
, prot
, flags
, fd
,
175 static_cast<size_t>(offset
)); // avoid sign extension
177 MallocHook::InvokeMmapHook(result
, start
, length
, prot
, flags
, fd
, offset
);
181 # endif // !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH)
183 extern "C" int munmap(void* start
, size_t length
) __THROW
{
184 MallocHook::InvokeMunmapHook(start
, length
);
186 if (!MallocHook::InvokeMunmapReplacement(start
, length
, &result
)) {
187 result
= sys_munmap(start
, length
);
192 extern "C" void* mremap(void* old_addr
, size_t old_size
, size_t new_size
,
193 int flags
, ...) __THROW
{
196 void *new_address
= va_arg(ap
, void *);
198 void* result
= sys_mremap(old_addr
, old_size
, new_size
, flags
, new_address
);
199 MallocHook::InvokeMremapHook(result
, old_addr
, old_size
, new_size
, flags
,
205 extern "C" void* __sbrk(ptrdiff_t increment
);
207 extern "C" void* sbrk(ptrdiff_t increment
) __THROW
{
208 MallocHook::InvokePreSbrkHook(increment
);
209 void *result
= __sbrk(increment
);
210 MallocHook::InvokeSbrkHook(result
, increment
);
214 /*static*/void* MallocHook::UnhookedMMap(void *start
, size_t length
, int prot
,
215 int flags
, int fd
, off_t offset
) {
217 if (!MallocHook::InvokeMmapReplacement(
218 start
, length
, prot
, flags
, fd
, offset
, &result
)) {
219 result
= do_mmap64(start
, length
, prot
, flags
, fd
, offset
);
224 /*static*/int MallocHook::UnhookedMUnmap(void *start
, size_t length
) {
226 if (!MallocHook::InvokeMunmapReplacement(start
, length
, &result
)) {
227 result
= syscall(SYS_munmap
, start
, length
);
232 #undef MALLOC_HOOK_HAVE_DO_MMAP64
234 #endif // #ifdef MALLOC_HOOK_HAVE_DO_MMAP64