1 // Copyright (c) 2011, 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.
30 // Override mmap/munmap/mremap/sbrk to provide support for calling the
31 // related hooks (in addition, of course, to doing what these
32 // functions normally do).
35 # error Should only be including malloc_hook_mmap_freebsd.h on FreeBSD systems.
39 #include <sys/syscall.h>
43 // Make sure mmap doesn't get #define'd away by <sys/mman.h>
46 // According to the FreeBSD documentation, use syscall if you do not
47 // need 64-bit alignment otherwise use __syscall. Indeed, syscall
48 // doesn't work correctly in most situations on 64-bit. It's return
49 // type is 'int' so for things like SYS_mmap, it actually truncates
50 // the returned address to 32-bits.
51 #if defined(__amd64__) || defined(__x86_64__)
52 # define MALLOC_HOOK_SYSCALL __syscall
54 # define MALLOC_HOOK_SYSCALL syscall
59 void* mmap(void *start
, size_t length
,int prot
, int flags
,
60 int fd
, off_t offset
) __THROW
61 ATTRIBUTE_SECTION(malloc_hook
);
62 int munmap(void* start
, size_t length
) __THROW
63 ATTRIBUTE_SECTION(malloc_hook
);
64 void* sbrk(intptr_t increment
) __THROW
65 ATTRIBUTE_SECTION(malloc_hook
);
68 static inline void* do_mmap(void *start
, size_t length
,
70 int fd
, off_t offset
) __THROW
{
71 return (void *)MALLOC_HOOK_SYSCALL(SYS_mmap
,
72 start
, length
, prot
, flags
, fd
, offset
);
75 static inline void* do_sbrk(intptr_t increment
) {
78 #if defined(__x86_64__) || defined(__amd64__)
81 "movq .curbrk@GOTPCREL(%%rip), %%rdx;"
82 "movq (%%rdx), %%rax;"
88 "movq .curbrk(%%rip), %%rax;"
95 "movl .curbrk, %%eax;"
101 if (increment
== 0) {
105 char* prevbrk
= static_cast<char*>(curbrk
);
106 void* newbrk
= prevbrk
+ increment
;
108 if (brk(newbrk
) == -1) {
109 return reinterpret_cast<void*>(static_cast<intptr_t>(-1));
116 extern "C" void* mmap(void *start
, size_t length
, int prot
, int flags
,
117 int fd
, off_t offset
) __THROW
{
118 MallocHook::InvokePreMmapHook(start
, length
, prot
, flags
, fd
, offset
);
120 if (!MallocHook::InvokeMmapReplacement(
121 start
, length
, prot
, flags
, fd
, offset
, &result
)) {
122 result
= do_mmap(start
, length
, prot
, flags
, fd
,
123 static_cast<size_t>(offset
)); // avoid sign extension
125 MallocHook::InvokeMmapHook(result
, start
, length
, prot
, flags
, fd
, offset
);
129 extern "C" int munmap(void* start
, size_t length
) __THROW
{
130 MallocHook::InvokeMunmapHook(start
, length
);
132 if (!MallocHook::InvokeMunmapReplacement(start
, length
, &result
)) {
133 result
= MALLOC_HOOK_SYSCALL(SYS_munmap
, start
, length
);
139 extern "C" void* sbrk(intptr_t increment
) __THROW
{
140 MallocHook::InvokePreSbrkHook(increment
);
141 void *result
= do_sbrk(increment
);
142 MallocHook::InvokeSbrkHook(result
, increment
);
146 /*static*/void* MallocHook::UnhookedMMap(void *start
, size_t length
, int prot
,
147 int flags
, int fd
, off_t offset
) {
149 if (!MallocHook::InvokeMmapReplacement(
150 start
, length
, prot
, flags
, fd
, offset
, &result
)) {
151 result
= do_mmap(start
, length
, prot
, flags
, fd
, offset
);
157 /*static*/int MallocHook::UnhookedMUnmap(void *start
, size_t length
) {
159 if (!MallocHook::InvokeMunmapReplacement(start
, length
, &result
)) {
160 result
= MALLOC_HOOK_SYSCALL(SYS_munmap
, start
, length
);
165 #undef MALLOC_HOOK_SYSCALL