Chromium Code Reviews| Index: base/process/memory_mac.mm |
| diff --git a/base/process/memory_mac.mm b/base/process/memory_mac.mm |
| index 575e886a175adacd6fe15105fa35e47b06336c32..c3bf321ed8428548090fc2e533226801a62eb574 100644 |
| --- a/base/process/memory_mac.mm |
| +++ b/base/process/memory_mac.mm |
| @@ -4,6 +4,12 @@ |
| #include "base/process/memory.h" |
| +// AddressSanitizer handles heap corruption, and on 64 bit Macs, the malloc |
| +// system automatically abort()s on heap corruption. |
| +#if !defined(ADDRESS_SANITIZER) && ARCH_CPU_32_BITS |
| +#define HANDLE_MEMORY_CORRUPTION_MANUALLY |
| +#endif |
| + |
| #include <CoreFoundation/CoreFoundation.h> |
| #include <errno.h> |
| #include <mach/mach.h> |
| @@ -21,19 +27,19 @@ |
| #include "third_party/apple_apsl/CFBase.h" |
| #include "third_party/apple_apsl/malloc.h" |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| #include <dlfcn.h> |
| #include <mach-o/nlist.h> |
| #include "base/threading/thread_local.h" |
| #include "third_party/mach_override/mach_override.h" |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| namespace base { |
| // These are helpers for EnableTerminationOnHeapCorruption, which is a no-op |
| // on 64 bit Macs. |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| namespace { |
| // Finds the library path for malloc() and thus the libC part of libSystem, |
| @@ -162,12 +168,10 @@ void CrMallocErrorBreak() { |
| } |
| } // namespace |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void EnableTerminationOnHeapCorruption() { |
| -#if defined(ADDRESS_SANITIZER) || ARCH_CPU_64_BITS |
| - // AddressSanitizer handles heap corruption, and on 64 bit Macs, the malloc |
| - // system automatically abort()s on heap corruption. |
| +#if !defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
|
Nico
2014/07/16 19:34:24
Since the comment that used to be here is now at t
tzik
2014/07/17 08:17:27
Done.
|
| return; |
| #else |
| // Only override once, otherwise CrMallocErrorBreak() will recurse |
| @@ -188,7 +192,7 @@ void EnableTerminationOnHeapCorruption() { |
| if (err != err_none) |
| DLOG(WARNING) << "Could not override malloc_error_break; error = " << err; |
| -#endif // defined(ADDRESS_SANITIZER) || ARCH_CPU_64_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| } |
| // ------------------------------------------------------------------------ |
| @@ -197,6 +201,8 @@ namespace { |
| bool g_oom_killer_enabled; |
| +#if !defined(ADDRESS_SANITIZER) |
| + |
| // Starting with Mac OS X 10.7, the zone allocators set up by the system are |
| // read-only, to prevent them from being overwritten in an attack. However, |
| // blindly unprotecting and reprotecting the zone allocators fails with |
| @@ -289,9 +295,9 @@ memalign_type g_old_memalign_purgeable; |
| void* oom_killer_malloc(struct _malloc_zone_t* zone, |
| size_t size) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void* result = g_old_malloc(zone, size); |
| if (!result && size) |
| debug::BreakDebugger(); |
| @@ -301,9 +307,9 @@ void* oom_killer_malloc(struct _malloc_zone_t* zone, |
| void* oom_killer_calloc(struct _malloc_zone_t* zone, |
| size_t num_items, |
| size_t size) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void* result = g_old_calloc(zone, num_items, size); |
| if (!result && num_items && size) |
| debug::BreakDebugger(); |
| @@ -312,9 +318,9 @@ void* oom_killer_calloc(struct _malloc_zone_t* zone, |
| void* oom_killer_valloc(struct _malloc_zone_t* zone, |
| size_t size) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void* result = g_old_valloc(zone, size); |
| if (!result && size) |
| debug::BreakDebugger(); |
| @@ -323,18 +329,18 @@ void* oom_killer_valloc(struct _malloc_zone_t* zone, |
| void oom_killer_free(struct _malloc_zone_t* zone, |
| void* ptr) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| g_old_free(zone, ptr); |
| } |
| void* oom_killer_realloc(struct _malloc_zone_t* zone, |
| void* ptr, |
| size_t size) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void* result = g_old_realloc(zone, ptr, size); |
| if (!result && size) |
| debug::BreakDebugger(); |
| @@ -344,9 +350,9 @@ void* oom_killer_realloc(struct _malloc_zone_t* zone, |
| void* oom_killer_memalign(struct _malloc_zone_t* zone, |
| size_t alignment, |
| size_t size) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void* result = g_old_memalign(zone, alignment, size); |
| // Only die if posix_memalign would have returned ENOMEM, since there are |
| // other reasons why NULL might be returned (see |
| @@ -360,9 +366,9 @@ void* oom_killer_memalign(struct _malloc_zone_t* zone, |
| void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone, |
| size_t size) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void* result = g_old_malloc_purgeable(zone, size); |
| if (!result && size) |
| debug::BreakDebugger(); |
| @@ -372,9 +378,9 @@ void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone, |
| void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone, |
| size_t num_items, |
| size_t size) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void* result = g_old_calloc_purgeable(zone, num_items, size); |
| if (!result && num_items && size) |
| debug::BreakDebugger(); |
| @@ -383,9 +389,9 @@ void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone, |
| void* oom_killer_valloc_purgeable(struct _malloc_zone_t* zone, |
| size_t size) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void* result = g_old_valloc_purgeable(zone, size); |
| if (!result && size) |
| debug::BreakDebugger(); |
| @@ -394,18 +400,18 @@ void* oom_killer_valloc_purgeable(struct _malloc_zone_t* zone, |
| void oom_killer_free_purgeable(struct _malloc_zone_t* zone, |
| void* ptr) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| g_old_free_purgeable(zone, ptr); |
| } |
| void* oom_killer_realloc_purgeable(struct _malloc_zone_t* zone, |
| void* ptr, |
| size_t size) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void* result = g_old_realloc_purgeable(zone, ptr, size); |
| if (!result && size) |
| debug::BreakDebugger(); |
| @@ -415,9 +421,9 @@ void* oom_killer_realloc_purgeable(struct _malloc_zone_t* zone, |
| void* oom_killer_memalign_purgeable(struct _malloc_zone_t* zone, |
| size_t alignment, |
| size_t size) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| void* result = g_old_memalign_purgeable(zone, alignment, size); |
| // Only die if posix_memalign would have returned ENOMEM, since there are |
| // other reasons why NULL might be returned (see |
| @@ -429,12 +435,16 @@ void* oom_killer_memalign_purgeable(struct _malloc_zone_t* zone, |
| return result; |
| } |
| +#endif // !defined(ADDRESS_SANITIZER) |
| + |
| // === C++ operator new === |
| void oom_killer_new() { |
| debug::BreakDebugger(); |
| } |
| +#if !defined(ADDRESS_SANITIZER) |
| + |
| // === Core Foundation CFAllocators === |
| bool CanGetContextForCFAllocator() { |
| @@ -491,6 +501,8 @@ void* oom_killer_cfallocator_malloc_zone(CFIndex alloc_size, |
| return result; |
| } |
| +#endif // !defined(ADDRESS_SANITIZER) |
| + |
| // === Cocoa NSObject allocation === |
| typedef id (*allocWithZone_t)(id, SEL, NSZone*); |
| @@ -507,25 +519,30 @@ id oom_killer_allocWithZone(id self, SEL _cmd, NSZone* zone) |
| } // namespace |
| bool UncheckedMalloc(size_t size, void** result) { |
| + |
| +#if !defined(ADDRESS_SANITIZER) |
|
Scott Hess - ex-Googler
2014/07/16 19:53:47
I'd invert this and put the ASAN case closer to th
tzik
2014/07/17 08:17:27
Done.
|
| if (g_old_malloc) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| *result = g_old_malloc(malloc_default_zone(), size); |
| } else { |
| *result = malloc(size); |
| } |
| +#else |
| + *result = malloc(size); |
| +#endif // !defined(ADDRESS_SANITIZER) |
| return *result != NULL; |
| } |
| bool UncheckedCalloc(size_t num_items, size_t size, void** result) { |
| if (g_old_calloc) { |
| -#if ARCH_CPU_32_BITS |
| +#if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| ScopedClearErrno clear_errno; |
| ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); |
| -#endif // ARCH_CPU_32_BITS |
| +#endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) |
| *result = g_old_calloc(malloc_default_zone(), num_items, size); |
| } else { |
| *result = calloc(num_items, size); |
| @@ -559,6 +576,10 @@ void EnableTerminationOnOutOfMemory() { |
| // Unfortunately, it's the best we can do. Also note that this does not affect |
| // allocations from non-default zones. |
| +#if !defined(ADDRESS_SANITIZER) |
| + // Don't do anything special on OOM for the malloc zones replaced by |
| + // AddressSanitizer, as modifying or protecting them may not work correctly. |
| + |
| CHECK(!g_old_malloc && !g_old_calloc && !g_old_valloc && !g_old_realloc && |
| !g_old_memalign) << "Old allocators unexpectedly non-null"; |
| @@ -566,10 +587,6 @@ void EnableTerminationOnOutOfMemory() { |
| !g_old_valloc_purgeable && !g_old_realloc_purgeable && |
| !g_old_memalign_purgeable) << "Old allocators unexpectedly non-null"; |
| -#if !defined(ADDRESS_SANITIZER) |
| - // Don't do anything special on OOM for the malloc zones replaced by |
| - // AddressSanitizer, as modifying or protecting them may not work correctly. |
| - |
| ChromeMallocZone* default_zone = |
| reinterpret_cast<ChromeMallocZone*>(malloc_default_zone()); |
| ChromeMallocZone* purgeable_zone = |