Index: third_party/tcmalloc/chromium/src/malloc_hook.cc |
=================================================================== |
--- third_party/tcmalloc/chromium/src/malloc_hook.cc (revision 124832) |
+++ third_party/tcmalloc/chromium/src/malloc_hook.cc (working copy) |
@@ -46,11 +46,12 @@ |
#include <stdint.h> |
#endif |
#include <algorithm> |
+#include "base/basictypes.h" |
#include "base/logging.h" |
#include "base/spinlock.h" |
#include "maybe_threads.h" |
#include "malloc_hook-inl.h" |
-#include <gperftools/malloc_hook.h> |
+#include <google/malloc_hook.h> |
// This #ifdef should almost never be set. Set NO_TCMALLOC_SAMPLES if |
// you're porting to a system where you really can't get a stacktrace. |
@@ -58,7 +59,7 @@ |
// We use #define so code compiles even if you #include stacktrace.h somehow. |
# define GetStackTrace(stack, depth, skip) (0) |
#else |
-# include <gperftools/stacktrace.h> |
+# include <google/stacktrace.h> |
#endif |
// __THROW is defined in glibc systems. It means, counter-intuitively, |
@@ -203,8 +204,14 @@ |
template <typename T> |
bool HookList<T>::Add(T value_as_t) { |
- AtomicWord value = bit_cast<AtomicWord>(value_as_t); |
+ // Note: we need to check this _before_ reinterpret_cast, since |
+ // reinterpret_cast may include random junk from memory. |
+ if (value_as_t == 0) { |
+ return false; |
+ } |
+ AtomicWord value = reinterpret_cast<const AtomicWord&>(value_as_t); |
if (value == 0) { |
+ // This should not actually happen, but just to be sure... |
return false; |
} |
SpinLockHolder l(&hooklist_spinlock); |
@@ -233,7 +240,8 @@ |
SpinLockHolder l(&hooklist_spinlock); |
AtomicWord hooks_end = base::subtle::Acquire_Load(&priv_end); |
int index = 0; |
- while (index < hooks_end && value_as_t != bit_cast<T>( |
+ // Note: we need to cast back to T since T may be smaller than AtomicWord. |
+ while (index < hooks_end && value_as_t != reinterpret_cast<T>( |
base::subtle::Acquire_Load(&priv_data[index]))) { |
++index; |
} |
@@ -260,7 +268,7 @@ |
for (int i = 0; i < hooks_end && n > 0; ++i) { |
AtomicWord data = base::subtle::Acquire_Load(&priv_data[i]); |
if (data != 0) { |
- *output_array++ = bit_cast<T>(data); |
+ *output_array++ = reinterpret_cast<const T&>(data); |
++actual_hooks_end; |
--n; |
} |
@@ -275,7 +283,7 @@ |
// Explicit instantiation for malloc_hook_test.cc. This ensures all the methods |
// are instantiated. |
-template struct HookList<MallocHook::NewHook>; |
+template class HookList<MallocHook::NewHook>; |
HookList<MallocHook::NewHook> new_hooks_ = |
INIT_HOOK_LIST_WITH_VALUE(&InitialNewHook); |
@@ -690,23 +698,197 @@ |
#endif |
} |
-// On systems where we know how, we override mmap/munmap/mremap/sbrk |
-// to provide support for calling the related hooks (in addition, |
-// of course, to doing what these functions normally do). |
+// On Linux/x86, we override mmap/munmap/mremap/sbrk |
+// and provide support for calling the related hooks. |
+// |
+// We define mmap() and mmap64(), which somewhat reimplements libc's mmap |
+// syscall stubs. Unfortunately libc only exports the stubs via weak symbols |
+// (which we're overriding with our mmap64() and mmap() wrappers) so we can't |
+// just call through to them. |
-#if defined(__linux) |
-# include "malloc_hook_mmap_linux.h" |
-#elif defined(__FreeBSD__) |
-# include "malloc_hook_mmap_freebsd.h" |
+#if defined(__linux) && \ |
+ (defined(__i386__) || defined(__x86_64__) || defined(__PPC__)) |
+#include <unistd.h> |
+#include <syscall.h> |
+#include <sys/mman.h> |
+#include <errno.h> |
+#include "base/linux_syscall_support.h" |
-#else |
+// The x86-32 case and the x86-64 case differ: |
+// 32b has a mmap2() syscall, 64b does not. |
+// 64b and 32b have different calling conventions for mmap(). |
+#if defined(__x86_64__) || defined(__PPC64__) |
+static inline void* do_mmap64(void *start, size_t length, |
+ int prot, int flags, |
+ int fd, __off64_t offset) __THROW { |
+ return (void *)syscall(SYS_mmap, start, length, prot, flags, fd, offset); |
+} |
+ |
+#elif defined(__i386__) || defined(__PPC__) |
+ |
+static inline void* do_mmap64(void *start, size_t length, |
+ int prot, int flags, |
+ int fd, __off64_t offset) __THROW { |
+ void *result; |
+ |
+ // Try mmap2() unless it's not supported |
+ static bool have_mmap2 = true; |
+ if (have_mmap2) { |
+ static int pagesize = 0; |
+ if (!pagesize) pagesize = getpagesize(); |
+ |
+ // Check that the offset is page aligned |
+ if (offset & (pagesize - 1)) { |
+ result = MAP_FAILED; |
+ errno = EINVAL; |
+ goto out; |
+ } |
+ |
+ result = (void *)syscall(SYS_mmap2, |
+ start, length, prot, flags, fd, |
+ (off_t) (offset / pagesize)); |
+ if (result != MAP_FAILED || errno != ENOSYS) goto out; |
+ |
+ // We don't have mmap2() after all - don't bother trying it in future |
+ have_mmap2 = false; |
+ } |
+ |
+ if (((off_t)offset) != offset) { |
+ // If we're trying to map a 64-bit offset, fail now since we don't |
+ // have 64-bit mmap() support. |
+ result = MAP_FAILED; |
+ errno = EINVAL; |
+ goto out; |
+ } |
+ |
+ { |
+ // Fall back to old 32-bit offset mmap() call |
+ // Old syscall interface cannot handle six args, so pass in an array |
+ int32 args[6] = { (int32) start, length, prot, flags, fd, (off_t) offset }; |
+ result = (void *)syscall(SYS_mmap, args); |
+ } |
+ out: |
+ return result; |
+} |
+ |
+# endif // defined(__x86_64__) |
+ |
+// We use do_mmap64 abstraction to put MallocHook::InvokeMmapHook |
+// calls right into mmap and mmap64, so that the stack frames in the caller's |
+// stack are at the same offsets for all the calls of memory allocating |
+// functions. |
+ |
+// Put all callers of MallocHook::Invoke* in this module into |
+// malloc_hook section, |
+// so that MallocHook::GetCallerStackTrace can function accurately: |
+ |
+// Make sure mmap doesn't get #define'd away by <sys/mman.h> |
+#undef mmap |
+ |
+extern "C" { |
+ void* mmap64(void *start, size_t length, int prot, int flags, |
+ int fd, __off64_t offset ) __THROW |
+ ATTRIBUTE_SECTION(malloc_hook); |
+ void* mmap(void *start, size_t length,int prot, int flags, |
+ int fd, off_t offset) __THROW |
+ ATTRIBUTE_SECTION(malloc_hook); |
+ int munmap(void* start, size_t length) __THROW |
+ ATTRIBUTE_SECTION(malloc_hook); |
+ void* mremap(void* old_addr, size_t old_size, size_t new_size, |
+ int flags, ...) __THROW |
+ ATTRIBUTE_SECTION(malloc_hook); |
+ void* sbrk(std::ptrdiff_t increment) __THROW |
+ ATTRIBUTE_SECTION(malloc_hook); |
+} |
+ |
+extern "C" void* mmap64(void *start, size_t length, int prot, int flags, |
+ int fd, __off64_t offset) __THROW { |
+ MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); |
+ void *result; |
+ if (!MallocHook::InvokeMmapReplacement( |
+ start, length, prot, flags, fd, offset, &result)) { |
+ result = do_mmap64(start, length, prot, flags, fd, offset); |
+ } |
+ MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); |
+ return result; |
+} |
+ |
+#if !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH) |
+ |
+extern "C" void* mmap(void *start, size_t length, int prot, int flags, |
+ int fd, off_t offset) __THROW { |
+ MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); |
+ void *result; |
+ if (!MallocHook::InvokeMmapReplacement( |
+ start, length, prot, flags, fd, offset, &result)) { |
+ result = do_mmap64(start, length, prot, flags, fd, |
+ static_cast<size_t>(offset)); // avoid sign extension |
+ } |
+ MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); |
+ return result; |
+} |
+ |
+#endif // !defined(__USE_FILE_OFFSET64) || !defined(__REDIRECT_NTH) |
+ |
+extern "C" int munmap(void* start, size_t length) __THROW { |
+ MallocHook::InvokeMunmapHook(start, length); |
+ int result; |
+ if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
+ result = syscall(SYS_munmap, start, length); |
+ } |
+ return result; |
+} |
+ |
+extern "C" void* mremap(void* old_addr, size_t old_size, size_t new_size, |
+ int flags, ...) __THROW { |
+ va_list ap; |
+ va_start(ap, flags); |
+ void *new_address = va_arg(ap, void *); |
+ va_end(ap); |
+ void* result = sys_mremap(old_addr, old_size, new_size, flags, new_address); |
+ MallocHook::InvokeMremapHook(result, old_addr, old_size, new_size, flags, |
+ new_address); |
+ return result; |
+} |
+ |
+// libc's version: |
+extern "C" void* __sbrk(std::ptrdiff_t increment); |
+ |
+extern "C" void* sbrk(std::ptrdiff_t increment) __THROW { |
+ MallocHook::InvokePreSbrkHook(increment); |
+ void *result = __sbrk(increment); |
+ MallocHook::InvokeSbrkHook(result, increment); |
+ return result; |
+} |
+ |
/*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, |
int flags, int fd, off_t offset) { |
void* result; |
if (!MallocHook::InvokeMmapReplacement( |
start, length, prot, flags, fd, offset, &result)) { |
+ result = do_mmap64(start, length, prot, flags, fd, offset); |
+ } |
+ return result; |
+} |
+ |
+/*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { |
+ int result; |
+ if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
+ result = sys_munmap(start, length); |
+ } |
+ return result; |
+} |
+ |
+#else // defined(__linux) && |
+ // (defined(__i386__) || defined(__x86_64__) || defined(__PPC__)) |
+ |
+/*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, |
+ int flags, int fd, off_t offset) { |
+ void* result; |
+ if (!MallocHook::InvokeMmapReplacement( |
+ start, length, prot, flags, fd, offset, &result)) { |
result = mmap(start, length, prot, flags, fd, offset); |
} |
return result; |
@@ -720,4 +902,5 @@ |
return result; |
} |
-#endif |
+#endif // defined(__linux) && |
+ // (defined(__i386__) || defined(__x86_64__) || defined(__PPC__)) |