Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: third_party/tcmalloc/chromium/src/malloc_hook_mmap_linux.h

Issue 9311003: Update the tcmalloc chromium branch to r144 (gperftools 2.0), and merge chromium-specific changes. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Rebasec Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 #include <syscall.h>
44 #include <sys/mman.h>
45 #include <errno.h>
46 #include "base/linux_syscall_support.h"
47
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.
54 #if defined(__x86_64__) || defined(__PPC64__) || (defined(_MIPS_SIM) && _MIPS_SI M == _ABI64)
55
56 static inline void* do_mmap64(void *start, size_t length,
57 int prot, int flags,
58 int fd, __off64_t offset) __THROW {
59 // The original gperftools uses sys_mmap() here. But, it is not allowed by
60 // Chromium's sandbox.
61 return (void *)syscall(SYS_mmap, start, length, prot, flags, fd, offset);
62 }
63
64 #define MALLOC_HOOK_HAVE_DO_MMAP64 1
65
66 #elif defined(__i386__) || defined(__PPC__) || defined(__mips__) || \
67 defined(__arm__)
68
69 static inline void* do_mmap64(void *start, size_t length,
70 int prot, int flags,
71 int fd, __off64_t offset) __THROW {
72 void *result;
73
74 // Try mmap2() unless it's not supported
75 static bool have_mmap2 = true;
76 if (have_mmap2) {
77 static int pagesize = 0;
78 if (!pagesize) pagesize = getpagesize();
79
80 // Check that the offset is page aligned
81 if (offset & (pagesize - 1)) {
82 result = MAP_FAILED;
83 errno = EINVAL;
84 goto out;
85 }
86
87 result = (void *)syscall(SYS_mmap2,
88 start, length, prot, flags, fd,
89 (off_t) (offset / pagesize));
90 if (result != MAP_FAILED || errno != ENOSYS) goto out;
91
92 // We don't have mmap2() after all - don't bother trying it in future
93 have_mmap2 = false;
94 }
95
96 if (((off_t)offset) != offset) {
97 // If we're trying to map a 64-bit offset, fail now since we don't
98 // have 64-bit mmap() support.
99 result = MAP_FAILED;
100 errno = EINVAL;
101 goto out;
102 }
103
104 #ifdef __NR_mmap
105 {
106 // Fall back to old 32-bit offset mmap() call
107 // Old syscall interface cannot handle six args, so pass in an array
108 int32 args[6] = { (int32) start, (int32) length, prot, flags, fd,
109 (off_t) offset };
110 result = (void *)syscall(SYS_mmap, args);
111 }
112 #else
113 // Some Linux ports like ARM EABI Linux has no mmap, just mmap2.
114 result = MAP_FAILED;
115 #endif
116
117 out:
118 return result;
119 }
120
121 #define MALLOC_HOOK_HAVE_DO_MMAP64 1
122
123 #endif // #if defined(__x86_64__)
124
125
126 #ifdef MALLOC_HOOK_HAVE_DO_MMAP64
127
128 // We use do_mmap64 abstraction to put MallocHook::InvokeMmapHook
129 // calls right into mmap and mmap64, so that the stack frames in the caller's
130 // stack are at the same offsets for all the calls of memory allocating
131 // functions.
132
133 // Put all callers of MallocHook::Invoke* in this module into
134 // malloc_hook section,
135 // so that MallocHook::GetCallerStackTrace can function accurately:
136
137 // Make sure mmap doesn't get #define'd away by <sys/mman.h>
138 # undef mmap
139
140 extern "C" {
141 void* mmap64(void *start, size_t length, int prot, int flags,
142 int fd, __off64_t offset ) __THROW
143 ATTRIBUTE_SECTION(malloc_hook);
144 void* mmap(void *start, size_t length,int prot, int flags,
145 int fd, off_t offset) __THROW
146 ATTRIBUTE_SECTION(malloc_hook);
147 int munmap(void* start, size_t length) __THROW
148 ATTRIBUTE_SECTION(malloc_hook);
149 void* mremap(void* old_addr, size_t old_size, size_t new_size,
150 int flags, ...) __THROW
151 ATTRIBUTE_SECTION(malloc_hook);
152 void* sbrk(ptrdiff_t increment) __THROW
153 ATTRIBUTE_SECTION(malloc_hook);
154 }
155
156 extern "C" void* mmap64(void *start, size_t length, int prot, int flags,
157 int fd, __off64_t offset) __THROW {
158 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset);
159 void *result;
160 if (!MallocHook::InvokeMmapReplacement(
161 start, length, prot, flags, fd, offset, &result)) {
162 result = do_mmap64(start, length, prot, flags, fd, offset);
163 }
164 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset);
165 return result;
166 }
167
168 # if !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH)
169
170 extern "C" void* mmap(void *start, size_t length, int prot, int flags,
171 int fd, off_t offset) __THROW {
172 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset);
173 void *result;
174 if (!MallocHook::InvokeMmapReplacement(
175 start, length, prot, flags, fd, offset, &result)) {
176 result = do_mmap64(start, length, prot, flags, fd,
177 static_cast<size_t>(offset)); // avoid sign extension
178 }
179 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset);
180 return result;
181 }
182
183 # endif // !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH)
184
185 extern "C" int munmap(void* start, size_t length) __THROW {
186 MallocHook::InvokeMunmapHook(start, length);
187 int result;
188 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) {
189 // The original gperftools uses sys_munmap() here. But, it is not allowed
190 // by Chromium's sandbox.
191 result = syscall(SYS_munmap, start, length);
192 }
193 return result;
194 }
195
196 extern "C" void* mremap(void* old_addr, size_t old_size, size_t new_size,
197 int flags, ...) __THROW {
198 va_list ap;
199 va_start(ap, flags);
200 void *new_address = va_arg(ap, void *);
201 va_end(ap);
202 // The original gperftools uses sys_mremap() here. But, it is not allowed by
203 // Chromium's sandbox.
204 void* result = (void *)syscall(
205 SYS_mremap, old_addr, old_size, new_size, flags, new_address);
206 MallocHook::InvokeMremapHook(result, old_addr, old_size, new_size, flags,
207 new_address);
208 return result;
209 }
210
211 // libc's version:
212 extern "C" void* __sbrk(ptrdiff_t increment);
213
214 extern "C" void* sbrk(ptrdiff_t increment) __THROW {
215 MallocHook::InvokePreSbrkHook(increment);
216 void *result = __sbrk(increment);
217 MallocHook::InvokeSbrkHook(result, increment);
218 return result;
219 }
220
221 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot,
222 int flags, int fd, off_t offset) {
223 void* result;
224 if (!MallocHook::InvokeMmapReplacement(
225 start, length, prot, flags, fd, offset, &result)) {
226 result = do_mmap64(start, length, prot, flags, fd, offset);
227 }
228 return result;
229 }
230
231 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) {
232 int result;
233 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) {
234 result = syscall(SYS_munmap, start, length);
235 }
236 return result;
237 }
238
239 #undef MALLOC_HOOK_HAVE_DO_MMAP64
240
241 #endif // #ifdef MALLOC_HOOK_HAVE_DO_MMAP64
OLDNEW
« no previous file with comments | « third_party/tcmalloc/chromium/src/malloc_hook_mmap_freebsd.h ('k') | third_party/tcmalloc/chromium/src/maybe_threads.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698