OLD | NEW |
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 28 matching lines...) Expand all Loading... |
39 # define mremap glibc_mremap | 39 # define mremap glibc_mremap |
40 # include <sys/mman.h> | 40 # include <sys/mman.h> |
41 # undef mremap | 41 # undef mremap |
42 #endif | 42 #endif |
43 | 43 |
44 #include <stddef.h> | 44 #include <stddef.h> |
45 #ifdef HAVE_STDINT_H | 45 #ifdef HAVE_STDINT_H |
46 #include <stdint.h> | 46 #include <stdint.h> |
47 #endif | 47 #endif |
48 #include <algorithm> | 48 #include <algorithm> |
| 49 #include "base/basictypes.h" |
49 #include "base/logging.h" | 50 #include "base/logging.h" |
50 #include "base/spinlock.h" | 51 #include "base/spinlock.h" |
51 #include "maybe_threads.h" | 52 #include "maybe_threads.h" |
52 #include "malloc_hook-inl.h" | 53 #include "malloc_hook-inl.h" |
53 #include <gperftools/malloc_hook.h> | 54 #include <google/malloc_hook.h> |
54 | 55 |
55 // This #ifdef should almost never be set. Set NO_TCMALLOC_SAMPLES if | 56 // This #ifdef should almost never be set. Set NO_TCMALLOC_SAMPLES if |
56 // you're porting to a system where you really can't get a stacktrace. | 57 // you're porting to a system where you really can't get a stacktrace. |
57 #ifdef NO_TCMALLOC_SAMPLES | 58 #ifdef NO_TCMALLOC_SAMPLES |
58 // We use #define so code compiles even if you #include stacktrace.h somehow. | 59 // We use #define so code compiles even if you #include stacktrace.h somehow. |
59 # define GetStackTrace(stack, depth, skip) (0) | 60 # define GetStackTrace(stack, depth, skip) (0) |
60 #else | 61 #else |
61 # include <gperftools/stacktrace.h> | 62 # include <google/stacktrace.h> |
62 #endif | 63 #endif |
63 | 64 |
64 // __THROW is defined in glibc systems. It means, counter-intuitively, | 65 // __THROW is defined in glibc systems. It means, counter-intuitively, |
65 // "This function will never throw an exception." It's an optional | 66 // "This function will never throw an exception." It's an optional |
66 // optimization tool, but we may need to use it to match glibc prototypes. | 67 // optimization tool, but we may need to use it to match glibc prototypes. |
67 #ifndef __THROW // I guess we're not on a glibc system | 68 #ifndef __THROW // I guess we're not on a glibc system |
68 # define __THROW // __THROW is just an optimization, so ok to make it "" | 69 # define __THROW // __THROW is just an optimization, so ok to make it "" |
69 #endif | 70 #endif |
70 | 71 |
71 using std::copy; | 72 using std::copy; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 // End of DEPRECATED code section. | 197 // End of DEPRECATED code section. |
197 | 198 |
198 // This lock is shared between all implementations of HookList::Add & Remove. | 199 // This lock is shared between all implementations of HookList::Add & Remove. |
199 // The potential for contention is very small. This needs to be a SpinLock and | 200 // The potential for contention is very small. This needs to be a SpinLock and |
200 // not a Mutex since it's possible for Mutex locking to allocate memory (e.g., | 201 // not a Mutex since it's possible for Mutex locking to allocate memory (e.g., |
201 // per-thread allocation in debug builds), which could cause infinite recursion. | 202 // per-thread allocation in debug builds), which could cause infinite recursion. |
202 static SpinLock hooklist_spinlock(base::LINKER_INITIALIZED); | 203 static SpinLock hooklist_spinlock(base::LINKER_INITIALIZED); |
203 | 204 |
204 template <typename T> | 205 template <typename T> |
205 bool HookList<T>::Add(T value_as_t) { | 206 bool HookList<T>::Add(T value_as_t) { |
206 AtomicWord value = bit_cast<AtomicWord>(value_as_t); | 207 // Note: we need to check this _before_ reinterpret_cast, since |
| 208 // reinterpret_cast may include random junk from memory. |
| 209 if (value_as_t == 0) { |
| 210 return false; |
| 211 } |
| 212 AtomicWord value = reinterpret_cast<const AtomicWord&>(value_as_t); |
207 if (value == 0) { | 213 if (value == 0) { |
| 214 // This should not actually happen, but just to be sure... |
208 return false; | 215 return false; |
209 } | 216 } |
210 SpinLockHolder l(&hooklist_spinlock); | 217 SpinLockHolder l(&hooklist_spinlock); |
211 // Find the first slot in data that is 0. | 218 // Find the first slot in data that is 0. |
212 int index = 0; | 219 int index = 0; |
213 while ((index < kHookListMaxValues) && | 220 while ((index < kHookListMaxValues) && |
214 (base::subtle::NoBarrier_Load(&priv_data[index]) != 0)) { | 221 (base::subtle::NoBarrier_Load(&priv_data[index]) != 0)) { |
215 ++index; | 222 ++index; |
216 } | 223 } |
217 if (index == kHookListMaxValues) { | 224 if (index == kHookListMaxValues) { |
218 return false; | 225 return false; |
219 } | 226 } |
220 AtomicWord prev_num_hooks = base::subtle::Acquire_Load(&priv_end); | 227 AtomicWord prev_num_hooks = base::subtle::Acquire_Load(&priv_end); |
221 base::subtle::Release_Store(&priv_data[index], value); | 228 base::subtle::Release_Store(&priv_data[index], value); |
222 if (prev_num_hooks <= index) { | 229 if (prev_num_hooks <= index) { |
223 base::subtle::Release_Store(&priv_end, index + 1); | 230 base::subtle::Release_Store(&priv_end, index + 1); |
224 } | 231 } |
225 return true; | 232 return true; |
226 } | 233 } |
227 | 234 |
228 template <typename T> | 235 template <typename T> |
229 bool HookList<T>::Remove(T value_as_t) { | 236 bool HookList<T>::Remove(T value_as_t) { |
230 if (value_as_t == 0) { | 237 if (value_as_t == 0) { |
231 return false; | 238 return false; |
232 } | 239 } |
233 SpinLockHolder l(&hooklist_spinlock); | 240 SpinLockHolder l(&hooklist_spinlock); |
234 AtomicWord hooks_end = base::subtle::Acquire_Load(&priv_end); | 241 AtomicWord hooks_end = base::subtle::Acquire_Load(&priv_end); |
235 int index = 0; | 242 int index = 0; |
236 while (index < hooks_end && value_as_t != bit_cast<T>( | 243 // Note: we need to cast back to T since T may be smaller than AtomicWord. |
| 244 while (index < hooks_end && value_as_t != reinterpret_cast<T>( |
237 base::subtle::Acquire_Load(&priv_data[index]))) { | 245 base::subtle::Acquire_Load(&priv_data[index]))) { |
238 ++index; | 246 ++index; |
239 } | 247 } |
240 if (index == hooks_end) { | 248 if (index == hooks_end) { |
241 return false; | 249 return false; |
242 } | 250 } |
243 base::subtle::Release_Store(&priv_data[index], 0); | 251 base::subtle::Release_Store(&priv_data[index], 0); |
244 if (hooks_end == index + 1) { | 252 if (hooks_end == index + 1) { |
245 // Adjust hooks_end down to the lowest possible value. | 253 // Adjust hooks_end down to the lowest possible value. |
246 hooks_end = index; | 254 hooks_end = index; |
247 while ((hooks_end > 0) && | 255 while ((hooks_end > 0) && |
248 (base::subtle::Acquire_Load(&priv_data[hooks_end - 1]) == 0)) { | 256 (base::subtle::Acquire_Load(&priv_data[hooks_end - 1]) == 0)) { |
249 --hooks_end; | 257 --hooks_end; |
250 } | 258 } |
251 base::subtle::Release_Store(&priv_end, hooks_end); | 259 base::subtle::Release_Store(&priv_end, hooks_end); |
252 } | 260 } |
253 return true; | 261 return true; |
254 } | 262 } |
255 | 263 |
256 template <typename T> | 264 template <typename T> |
257 int HookList<T>::Traverse(T* output_array, int n) const { | 265 int HookList<T>::Traverse(T* output_array, int n) const { |
258 AtomicWord hooks_end = base::subtle::Acquire_Load(&priv_end); | 266 AtomicWord hooks_end = base::subtle::Acquire_Load(&priv_end); |
259 int actual_hooks_end = 0; | 267 int actual_hooks_end = 0; |
260 for (int i = 0; i < hooks_end && n > 0; ++i) { | 268 for (int i = 0; i < hooks_end && n > 0; ++i) { |
261 AtomicWord data = base::subtle::Acquire_Load(&priv_data[i]); | 269 AtomicWord data = base::subtle::Acquire_Load(&priv_data[i]); |
262 if (data != 0) { | 270 if (data != 0) { |
263 *output_array++ = bit_cast<T>(data); | 271 *output_array++ = reinterpret_cast<const T&>(data); |
264 ++actual_hooks_end; | 272 ++actual_hooks_end; |
265 --n; | 273 --n; |
266 } | 274 } |
267 } | 275 } |
268 return actual_hooks_end; | 276 return actual_hooks_end; |
269 } | 277 } |
270 | 278 |
271 // Initialize a HookList (optionally with the given initial_value in index 0). | 279 // Initialize a HookList (optionally with the given initial_value in index 0). |
272 #define INIT_HOOK_LIST { 0 } | 280 #define INIT_HOOK_LIST { 0 } |
273 #define INIT_HOOK_LIST_WITH_VALUE(initial_value) \ | 281 #define INIT_HOOK_LIST_WITH_VALUE(initial_value) \ |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 #endif | 698 #endif |
691 } | 699 } |
692 | 700 |
693 // On systems where we know how, we override mmap/munmap/mremap/sbrk | 701 // On systems where we know how, we override mmap/munmap/mremap/sbrk |
694 // to provide support for calling the related hooks (in addition, | 702 // to provide support for calling the related hooks (in addition, |
695 // of course, to doing what these functions normally do). | 703 // of course, to doing what these functions normally do). |
696 | 704 |
697 #if defined(__linux) | 705 #if defined(__linux) |
698 # include "malloc_hook_mmap_linux.h" | 706 # include "malloc_hook_mmap_linux.h" |
699 | 707 |
700 #elif defined(__FreeBSD__) | 708 // This code doesn't even compile on my freebsd 8.1 (x86_64) system, |
| 709 // so comment it out for now. TODO(csilvers): fix this! |
| 710 #elif 0 && defined(__FreeBSD__) |
701 # include "malloc_hook_mmap_freebsd.h" | 711 # include "malloc_hook_mmap_freebsd.h" |
702 | 712 |
703 #else | 713 #else |
704 | 714 |
705 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, | 715 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, |
706 int flags, int fd, off_t offset) { | 716 int flags, int fd, off_t offset) { |
707 void* result; | 717 void* result; |
708 if (!MallocHook::InvokeMmapReplacement( | 718 if (!MallocHook::InvokeMmapReplacement( |
709 start, length, prot, flags, fd, offset, &result)) { | 719 start, length, prot, flags, fd, offset, &result)) { |
710 result = mmap(start, length, prot, flags, fd, offset); | 720 result = mmap(start, length, prot, flags, fd, offset); |
711 } | 721 } |
712 return result; | 722 return result; |
713 } | 723 } |
714 | 724 |
715 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { | 725 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { |
716 int result; | 726 int result; |
717 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { | 727 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
718 result = munmap(start, length); | 728 result = munmap(start, length); |
719 } | 729 } |
720 return result; | 730 return result; |
721 } | 731 } |
722 | 732 |
723 #endif | 733 #endif |
OLD | NEW |