blob: cbf37825d45961251b86ccacb5d54d2ac585a755 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2005, Google Inc.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// ---
32// Author: Sanjay Ghemawat <opensource@google.com>
33
34// We define mmap() and mmap64(), which somewhat reimplements libc's mmap
35// syscall stubs. Unfortunately libc only exports the stubs via weak symbols
36// (which we're overriding with our mmap64() and mmap() wrappers) so we can't
37// just call through to them.
38
39#ifndef __linux
40# error Should only be including malloc_hook_mmap_linux.h on linux systems.
41#endif
42
Austin Schuh745610d2015-09-06 18:19:50 -070043#include <errno.h>
Brian Silverman20350ac2021-11-17 18:19:55 -080044#include <sys/mman.h>
45#include <sys/syscall.h>
46#include <unistd.h>
Austin Schuh745610d2015-09-06 18:19:50 -070047
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().
51
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.
Brian Silverman20350ac2021-11-17 18:19:55 -080054#if defined(__x86_64__) \
55 || defined(__PPC64__) \
56 || defined(__aarch64__) \
57 || (defined(_MIPS_SIM) && (_MIPS_SIM == _ABI64 || _MIPS_SIM == _ABIN32)) \
58 || defined(__s390__) || (defined(__riscv) && __riscv_xlen == 64) \
59 || defined(__e2k__)
Austin Schuh745610d2015-09-06 18:19:50 -070060
61static inline void* do_mmap64(void *start, size_t length,
62 int prot, int flags,
Brian Silverman20350ac2021-11-17 18:19:55 -080063 int fd, off64_t offset) __THROW {
64#if defined(__s390__)
65 long args[6] = { (long)start, (long)length, (long)prot, (long)flags,
66 (long)fd, (long)offset };
67 return (void*)syscall(SYS_mmap, args);
68#else
69 return (void*)syscall(SYS_mmap, start, length, prot, flags, fd, offset);
70#endif
Austin Schuh745610d2015-09-06 18:19:50 -070071}
72
73#define MALLOC_HOOK_HAVE_DO_MMAP64 1
74
75#elif defined(__i386__) || defined(__PPC__) || defined(__mips__) || \
76 defined(__arm__)
77
78static inline void* do_mmap64(void *start, size_t length,
79 int prot, int flags,
Brian Silverman20350ac2021-11-17 18:19:55 -080080 int fd, off64_t offset) __THROW {
Austin Schuh745610d2015-09-06 18:19:50 -070081 void *result;
82
83 // Try mmap2() unless it's not supported
84 static bool have_mmap2 = true;
85 if (have_mmap2) {
86 static int pagesize = 0;
87 if (!pagesize) pagesize = getpagesize();
88
89 // Check that the offset is page aligned
90 if (offset & (pagesize - 1)) {
91 result = MAP_FAILED;
92 errno = EINVAL;
93 goto out;
94 }
95
96 result = (void *)syscall(SYS_mmap2,
97 start, length, prot, flags, fd,
98 (off_t) (offset / pagesize));
99 if (result != MAP_FAILED || errno != ENOSYS) goto out;
100
101 // We don't have mmap2() after all - don't bother trying it in future
102 have_mmap2 = false;
103 }
104
105 if (((off_t)offset) != offset) {
106 // If we're trying to map a 64-bit offset, fail now since we don't
107 // have 64-bit mmap() support.
108 result = MAP_FAILED;
109 errno = EINVAL;
110 goto out;
111 }
112
113#ifdef __NR_mmap
114 {
115 // Fall back to old 32-bit offset mmap() call
116 // Old syscall interface cannot handle six args, so pass in an array
117 int32 args[6] = { (int32) start, (int32) length, prot, flags, fd,
118 (int32)(off_t) offset };
119 result = (void *)syscall(SYS_mmap, args);
120 }
121#else
122 // Some Linux ports like ARM EABI Linux has no mmap, just mmap2.
123 result = MAP_FAILED;
124#endif
125
126 out:
127 return result;
128}
129
130#define MALLOC_HOOK_HAVE_DO_MMAP64 1
131
132#endif // #if defined(__x86_64__)
133
134
135#ifdef MALLOC_HOOK_HAVE_DO_MMAP64
136
137// We use do_mmap64 abstraction to put MallocHook::InvokeMmapHook
138// calls right into mmap and mmap64, so that the stack frames in the caller's
139// stack are at the same offsets for all the calls of memory allocating
140// functions.
141
142// Put all callers of MallocHook::Invoke* in this module into
143// malloc_hook section,
144// so that MallocHook::GetCallerStackTrace can function accurately:
145
Brian Silverman20350ac2021-11-17 18:19:55 -0800146// Make sure mmap64 and mmap doesn't get #define'd away by <sys/mman.h>
147# undef mmap64
Austin Schuh745610d2015-09-06 18:19:50 -0700148# undef mmap
149
150extern "C" {
151 void* mmap64(void *start, size_t length, int prot, int flags,
Brian Silverman20350ac2021-11-17 18:19:55 -0800152 int fd, off64_t offset ) __THROW
Austin Schuh745610d2015-09-06 18:19:50 -0700153 ATTRIBUTE_SECTION(malloc_hook);
154 void* mmap(void *start, size_t length,int prot, int flags,
155 int fd, off_t offset) __THROW
156 ATTRIBUTE_SECTION(malloc_hook);
157 int munmap(void* start, size_t length) __THROW
158 ATTRIBUTE_SECTION(malloc_hook);
159 void* mremap(void* old_addr, size_t old_size, size_t new_size,
160 int flags, ...) __THROW
161 ATTRIBUTE_SECTION(malloc_hook);
Brian Silverman20350ac2021-11-17 18:19:55 -0800162 void* sbrk(intptr_t increment) __THROW
Austin Schuh745610d2015-09-06 18:19:50 -0700163 ATTRIBUTE_SECTION(malloc_hook);
164}
165
166extern "C" void* mmap64(void *start, size_t length, int prot, int flags,
Brian Silverman20350ac2021-11-17 18:19:55 -0800167 int fd, off64_t offset) __THROW {
Austin Schuh745610d2015-09-06 18:19:50 -0700168 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset);
169 void *result;
170 if (!MallocHook::InvokeMmapReplacement(
171 start, length, prot, flags, fd, offset, &result)) {
172 result = do_mmap64(start, length, prot, flags, fd, offset);
173 }
174 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset);
175 return result;
176}
177
178# if !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH)
179
180extern "C" void* mmap(void *start, size_t length, int prot, int flags,
181 int fd, off_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,
187 static_cast<size_t>(offset)); // avoid sign extension
188 }
189 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset);
190 return result;
191}
192
193# endif // !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH)
194
195extern "C" int munmap(void* start, size_t length) __THROW {
196 MallocHook::InvokeMunmapHook(start, length);
197 int result;
198 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) {
Brian Silverman20350ac2021-11-17 18:19:55 -0800199 result = syscall(SYS_munmap, start, length);
Austin Schuh745610d2015-09-06 18:19:50 -0700200 }
201 return result;
202}
203
204extern "C" void* mremap(void* old_addr, size_t old_size, size_t new_size,
205 int flags, ...) __THROW {
206 va_list ap;
207 va_start(ap, flags);
208 void *new_address = va_arg(ap, void *);
209 va_end(ap);
Brian Silverman20350ac2021-11-17 18:19:55 -0800210 void* result = (void*)syscall(SYS_mremap, old_addr, old_size, new_size, flags,
211 new_address);
Austin Schuh745610d2015-09-06 18:19:50 -0700212 MallocHook::InvokeMremapHook(result, old_addr, old_size, new_size, flags,
213 new_address);
214 return result;
215}
216
Brian Silverman20350ac2021-11-17 18:19:55 -0800217#ifdef HAVE___SBRK
Austin Schuh745610d2015-09-06 18:19:50 -0700218// libc's version:
Brian Silverman20350ac2021-11-17 18:19:55 -0800219extern "C" void* __sbrk(intptr_t increment);
Austin Schuh745610d2015-09-06 18:19:50 -0700220
Brian Silverman20350ac2021-11-17 18:19:55 -0800221extern "C" void* sbrk(intptr_t increment) __THROW {
Austin Schuh745610d2015-09-06 18:19:50 -0700222 MallocHook::InvokePreSbrkHook(increment);
223 void *result = __sbrk(increment);
224 MallocHook::InvokeSbrkHook(result, increment);
225 return result;
226}
227
228#endif
229
230/*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot,
231 int flags, int fd, off_t offset) {
232 void* result;
233 if (!MallocHook::InvokeMmapReplacement(
234 start, length, prot, flags, fd, offset, &result)) {
235 result = do_mmap64(start, length, prot, flags, fd, offset);
236 }
237 return result;
238}
239
240/*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) {
241 int result;
242 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) {
243 result = syscall(SYS_munmap, start, length);
244 }
245 return result;
246}
247
248#undef MALLOC_HOOK_HAVE_DO_MMAP64
249
250#endif // #ifdef MALLOC_HOOK_HAVE_DO_MMAP64