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

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

Issue 14859005: Support Android in MallocHook for mmap, munmap and mremap. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit fix Created 7 years, 7 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
OLDNEW
1 // Copyright (c) 2005, Google Inc. 1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 22 matching lines...) Expand all
33 // We define mmap() and mmap64(), which somewhat reimplements libc's mmap 33 // We define mmap() and mmap64(), which somewhat reimplements libc's mmap
34 // syscall stubs. Unfortunately libc only exports the stubs via weak symbols 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 35 // (which we're overriding with our mmap64() and mmap() wrappers) so we can't
36 // just call through to them. 36 // just call through to them.
37 37
38 #ifndef __linux 38 #ifndef __linux
39 # error Should only be including malloc_hook_mmap_linux.h on linux systems. 39 # error Should only be including malloc_hook_mmap_linux.h on linux systems.
40 #endif 40 #endif
41 41
42 #include <unistd.h> 42 #include <unistd.h>
43 #if defined(__ANDROID__) || defined(ANDROID)
44 #include <sys/syscall.h>
45 #include <sys/linux-syscalls.h>
46 #else
43 #include <syscall.h> 47 #include <syscall.h>
48 #endif
44 #include <sys/mman.h> 49 #include <sys/mman.h>
45 #include <errno.h> 50 #include <errno.h>
46 #include "base/linux_syscall_support.h" 51 #include "base/linux_syscall_support.h"
47 52
53 // SYS_mmap2, SYS_munmap, SYS_mremap and __off64_t are not defined in Android.
54 #if defined(__ANDROID__) || defined(ANDROID)
55 #ifndef SYS_mmap2
56 #define SYS_mmap2 __NR_mmap2
57 #endif
58 #ifndef SYS_munmap
59 #define SYS_munmap __NR_munmap
60 #endif
61 #ifndef SYS_mremap
62 #define SYS_mremap __NR_mremap
63 #endif
64 typedef off64_t __off64_t;
65 #endif // defined(__ANDROID__) || defined(ANDROID)
66
48 // The x86-32 case and the x86-64 case differ: 67 // The x86-32 case and the x86-64 case differ:
49 // 32b has a mmap2() syscall, 64b does not. 68 // 32b has a mmap2() syscall, 64b does not.
50 // 64b and 32b have different calling conventions for mmap(). 69 // 64b and 32b have different calling conventions for mmap().
51 70
52 // I test for 64-bit first so I don't have to do things like 71 // 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. 72 // '#if (defined(__mips__) && !defined(__MIPS64__))' as a mips32 check.
54 #if defined(__x86_64__) || defined(__PPC64__) || (defined(_MIPS_SIM) && _MIPS_SI M == _ABI64) 73 #if defined(__x86_64__) || defined(__PPC64__) || (defined(_MIPS_SIM) && _MIPS_SI M == _ABI64)
55 74
56 static inline void* do_mmap64(void *start, size_t length, 75 static inline void* do_mmap64(void *start, size_t length,
57 int prot, int flags, 76 int prot, int flags,
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 int fd, __off64_t offset ) __THROW 161 int fd, __off64_t offset ) __THROW
143 ATTRIBUTE_SECTION(malloc_hook); 162 ATTRIBUTE_SECTION(malloc_hook);
144 void* mmap(void *start, size_t length,int prot, int flags, 163 void* mmap(void *start, size_t length,int prot, int flags,
145 int fd, off_t offset) __THROW 164 int fd, off_t offset) __THROW
146 ATTRIBUTE_SECTION(malloc_hook); 165 ATTRIBUTE_SECTION(malloc_hook);
147 int munmap(void* start, size_t length) __THROW 166 int munmap(void* start, size_t length) __THROW
148 ATTRIBUTE_SECTION(malloc_hook); 167 ATTRIBUTE_SECTION(malloc_hook);
149 void* mremap(void* old_addr, size_t old_size, size_t new_size, 168 void* mremap(void* old_addr, size_t old_size, size_t new_size,
150 int flags, ...) __THROW 169 int flags, ...) __THROW
151 ATTRIBUTE_SECTION(malloc_hook); 170 ATTRIBUTE_SECTION(malloc_hook);
171 #if !defined(__ANDROID__) && !defined(ANDROID)
152 void* sbrk(ptrdiff_t increment) __THROW 172 void* sbrk(ptrdiff_t increment) __THROW
153 ATTRIBUTE_SECTION(malloc_hook); 173 ATTRIBUTE_SECTION(malloc_hook);
174 #endif
154 } 175 }
155 176
156 extern "C" void* mmap64(void *start, size_t length, int prot, int flags, 177 extern "C" void* mmap64(void *start, size_t length, int prot, int flags,
157 int fd, __off64_t offset) __THROW { 178 int fd, __off64_t offset) __THROW {
158 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); 179 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset);
159 void *result; 180 void *result;
160 if (!MallocHook::InvokeMmapReplacement( 181 if (!MallocHook::InvokeMmapReplacement(
161 start, length, prot, flags, fd, offset, &result)) { 182 start, length, prot, flags, fd, offset, &result)) {
162 result = do_mmap64(start, length, prot, flags, fd, offset); 183 result = do_mmap64(start, length, prot, flags, fd, offset);
163 } 184 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 va_end(ap); 222 va_end(ap);
202 // The original gperftools uses sys_mremap() here. But, it is not allowed by 223 // The original gperftools uses sys_mremap() here. But, it is not allowed by
203 // Chromium's sandbox. 224 // Chromium's sandbox.
204 void* result = (void *)syscall( 225 void* result = (void *)syscall(
205 SYS_mremap, old_addr, old_size, new_size, flags, new_address); 226 SYS_mremap, old_addr, old_size, new_size, flags, new_address);
206 MallocHook::InvokeMremapHook(result, old_addr, old_size, new_size, flags, 227 MallocHook::InvokeMremapHook(result, old_addr, old_size, new_size, flags,
207 new_address); 228 new_address);
208 return result; 229 return result;
209 } 230 }
210 231
232 // Don't hook sbrk in Androis since Android doesn't have __sbrk.
digit1 2013/05/13 16:14:42 Small typo here. Also probably better avoid the re
Dai Mikurube (NOT FULLTIME) 2013/05/13 21:25:52 Thanks. Replaced the comment.
233 #if !defined(__ANDROID__) && !defined(ANDROID)
211 // libc's version: 234 // libc's version:
212 extern "C" void* __sbrk(ptrdiff_t increment); 235 extern "C" void* __sbrk(ptrdiff_t increment);
213 236
214 extern "C" void* sbrk(ptrdiff_t increment) __THROW { 237 extern "C" void* sbrk(ptrdiff_t increment) __THROW {
215 MallocHook::InvokePreSbrkHook(increment); 238 MallocHook::InvokePreSbrkHook(increment);
216 void *result = __sbrk(increment); 239 void *result = __sbrk(increment);
217 MallocHook::InvokeSbrkHook(result, increment); 240 MallocHook::InvokeSbrkHook(result, increment);
218 return result; 241 return result;
219 } 242 }
243 #endif // !defined(__ANDROID__) && !defined(ANDROID)
220 244
221 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, 245 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot,
222 int flags, int fd, off_t offset) { 246 int flags, int fd, off_t offset) {
223 void* result; 247 void* result;
224 if (!MallocHook::InvokeMmapReplacement( 248 if (!MallocHook::InvokeMmapReplacement(
225 start, length, prot, flags, fd, offset, &result)) { 249 start, length, prot, flags, fd, offset, &result)) {
226 result = do_mmap64(start, length, prot, flags, fd, offset); 250 result = do_mmap64(start, length, prot, flags, fd, offset);
227 } 251 }
228 return result; 252 return result;
229 } 253 }
230 254
231 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { 255 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) {
232 int result; 256 int result;
233 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { 257 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) {
234 result = syscall(SYS_munmap, start, length); 258 result = syscall(SYS_munmap, start, length);
235 } 259 }
236 return result; 260 return result;
237 } 261 }
238 262
239 #undef MALLOC_HOOK_HAVE_DO_MMAP64 263 #undef MALLOC_HOOK_HAVE_DO_MMAP64
240 264
241 #endif // #ifdef MALLOC_HOOK_HAVE_DO_MMAP64 265 #endif // #ifdef MALLOC_HOOK_HAVE_DO_MMAP64
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698