Commit 765d9bc7 authored by tzik@chromium.org's avatar tzik@chromium.org

Macro out unused functions in memory_mac.mm

These functions are defined but not used on ASan-enabled build.
That causes build failure on ASan-enabled build without -w flag.

BUG=162783

Review URL: https://codereview.chromium.org/388263002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283766 0039d316-1c4b-4281-b951-d872f2087c98
parent a1e527df
...@@ -4,6 +4,12 @@ ...@@ -4,6 +4,12 @@
#include "base/process/memory.h" #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 <CoreFoundation/CoreFoundation.h>
#include <errno.h> #include <errno.h>
#include <mach/mach.h> #include <mach/mach.h>
...@@ -21,19 +27,19 @@ ...@@ -21,19 +27,19 @@
#include "third_party/apple_apsl/CFBase.h" #include "third_party/apple_apsl/CFBase.h"
#include "third_party/apple_apsl/malloc.h" #include "third_party/apple_apsl/malloc.h"
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
#include <dlfcn.h> #include <dlfcn.h>
#include <mach-o/nlist.h> #include <mach-o/nlist.h>
#include "base/threading/thread_local.h" #include "base/threading/thread_local.h"
#include "third_party/mach_override/mach_override.h" #include "third_party/mach_override/mach_override.h"
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
namespace base { namespace base {
// These are helpers for EnableTerminationOnHeapCorruption, which is a no-op // These are helpers for EnableTerminationOnHeapCorruption, which is a no-op
// on 64 bit Macs. // on 64 bit Macs.
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
namespace { namespace {
// Finds the library path for malloc() and thus the libC part of libSystem, // Finds the library path for malloc() and thus the libC part of libSystem,
...@@ -162,14 +168,10 @@ void CrMallocErrorBreak() { ...@@ -162,14 +168,10 @@ void CrMallocErrorBreak() {
} }
} // namespace } // namespace
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void EnableTerminationOnHeapCorruption() { void EnableTerminationOnHeapCorruption() {
#if defined(ADDRESS_SANITIZER) || ARCH_CPU_64_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
// AddressSanitizer handles heap corruption, and on 64 bit Macs, the malloc
// system automatically abort()s on heap corruption.
return;
#else
// Only override once, otherwise CrMallocErrorBreak() will recurse // Only override once, otherwise CrMallocErrorBreak() will recurse
// to itself. // to itself.
if (g_original_malloc_error_break) if (g_original_malloc_error_break)
...@@ -188,7 +190,7 @@ void EnableTerminationOnHeapCorruption() { ...@@ -188,7 +190,7 @@ void EnableTerminationOnHeapCorruption() {
if (err != err_none) if (err != err_none)
DLOG(WARNING) << "Could not override malloc_error_break; error = " << err; 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 +199,8 @@ namespace { ...@@ -197,6 +199,8 @@ namespace {
bool g_oom_killer_enabled; 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 // 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, // read-only, to prevent them from being overwritten in an attack. However,
// blindly unprotecting and reprotecting the zone allocators fails with // blindly unprotecting and reprotecting the zone allocators fails with
...@@ -289,9 +293,9 @@ memalign_type g_old_memalign_purgeable; ...@@ -289,9 +293,9 @@ memalign_type g_old_memalign_purgeable;
void* oom_killer_malloc(struct _malloc_zone_t* zone, void* oom_killer_malloc(struct _malloc_zone_t* zone,
size_t size) { size_t size) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void* result = g_old_malloc(zone, size); void* result = g_old_malloc(zone, size);
if (!result && size) if (!result && size)
debug::BreakDebugger(); debug::BreakDebugger();
...@@ -301,9 +305,9 @@ void* oom_killer_malloc(struct _malloc_zone_t* zone, ...@@ -301,9 +305,9 @@ void* oom_killer_malloc(struct _malloc_zone_t* zone,
void* oom_killer_calloc(struct _malloc_zone_t* zone, void* oom_killer_calloc(struct _malloc_zone_t* zone,
size_t num_items, size_t num_items,
size_t size) { size_t size) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void* result = g_old_calloc(zone, num_items, size); void* result = g_old_calloc(zone, num_items, size);
if (!result && num_items && size) if (!result && num_items && size)
debug::BreakDebugger(); debug::BreakDebugger();
...@@ -312,9 +316,9 @@ void* oom_killer_calloc(struct _malloc_zone_t* zone, ...@@ -312,9 +316,9 @@ void* oom_killer_calloc(struct _malloc_zone_t* zone,
void* oom_killer_valloc(struct _malloc_zone_t* zone, void* oom_killer_valloc(struct _malloc_zone_t* zone,
size_t size) { size_t size) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void* result = g_old_valloc(zone, size); void* result = g_old_valloc(zone, size);
if (!result && size) if (!result && size)
debug::BreakDebugger(); debug::BreakDebugger();
...@@ -323,18 +327,18 @@ void* oom_killer_valloc(struct _malloc_zone_t* zone, ...@@ -323,18 +327,18 @@ void* oom_killer_valloc(struct _malloc_zone_t* zone,
void oom_killer_free(struct _malloc_zone_t* zone, void oom_killer_free(struct _malloc_zone_t* zone,
void* ptr) { void* ptr) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
g_old_free(zone, ptr); g_old_free(zone, ptr);
} }
void* oom_killer_realloc(struct _malloc_zone_t* zone, void* oom_killer_realloc(struct _malloc_zone_t* zone,
void* ptr, void* ptr,
size_t size) { size_t size) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void* result = g_old_realloc(zone, ptr, size); void* result = g_old_realloc(zone, ptr, size);
if (!result && size) if (!result && size)
debug::BreakDebugger(); debug::BreakDebugger();
...@@ -344,9 +348,9 @@ void* oom_killer_realloc(struct _malloc_zone_t* zone, ...@@ -344,9 +348,9 @@ void* oom_killer_realloc(struct _malloc_zone_t* zone,
void* oom_killer_memalign(struct _malloc_zone_t* zone, void* oom_killer_memalign(struct _malloc_zone_t* zone,
size_t alignment, size_t alignment,
size_t size) { size_t size) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void* result = g_old_memalign(zone, alignment, size); void* result = g_old_memalign(zone, alignment, size);
// Only die if posix_memalign would have returned ENOMEM, since there are // Only die if posix_memalign would have returned ENOMEM, since there are
// other reasons why NULL might be returned (see // other reasons why NULL might be returned (see
...@@ -360,9 +364,9 @@ void* oom_killer_memalign(struct _malloc_zone_t* zone, ...@@ -360,9 +364,9 @@ void* oom_killer_memalign(struct _malloc_zone_t* zone,
void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone, void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone,
size_t size) { size_t size) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void* result = g_old_malloc_purgeable(zone, size); void* result = g_old_malloc_purgeable(zone, size);
if (!result && size) if (!result && size)
debug::BreakDebugger(); debug::BreakDebugger();
...@@ -372,9 +376,9 @@ void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone, ...@@ -372,9 +376,9 @@ void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone,
void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone, void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone,
size_t num_items, size_t num_items,
size_t size) { size_t size) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void* result = g_old_calloc_purgeable(zone, num_items, size); void* result = g_old_calloc_purgeable(zone, num_items, size);
if (!result && num_items && size) if (!result && num_items && size)
debug::BreakDebugger(); debug::BreakDebugger();
...@@ -383,9 +387,9 @@ void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone, ...@@ -383,9 +387,9 @@ void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone,
void* oom_killer_valloc_purgeable(struct _malloc_zone_t* zone, void* oom_killer_valloc_purgeable(struct _malloc_zone_t* zone,
size_t size) { size_t size) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void* result = g_old_valloc_purgeable(zone, size); void* result = g_old_valloc_purgeable(zone, size);
if (!result && size) if (!result && size)
debug::BreakDebugger(); debug::BreakDebugger();
...@@ -394,18 +398,18 @@ void* oom_killer_valloc_purgeable(struct _malloc_zone_t* zone, ...@@ -394,18 +398,18 @@ void* oom_killer_valloc_purgeable(struct _malloc_zone_t* zone,
void oom_killer_free_purgeable(struct _malloc_zone_t* zone, void oom_killer_free_purgeable(struct _malloc_zone_t* zone,
void* ptr) { void* ptr) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
g_old_free_purgeable(zone, ptr); g_old_free_purgeable(zone, ptr);
} }
void* oom_killer_realloc_purgeable(struct _malloc_zone_t* zone, void* oom_killer_realloc_purgeable(struct _malloc_zone_t* zone,
void* ptr, void* ptr,
size_t size) { size_t size) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void* result = g_old_realloc_purgeable(zone, ptr, size); void* result = g_old_realloc_purgeable(zone, ptr, size);
if (!result && size) if (!result && size)
debug::BreakDebugger(); debug::BreakDebugger();
...@@ -415,9 +419,9 @@ void* oom_killer_realloc_purgeable(struct _malloc_zone_t* zone, ...@@ -415,9 +419,9 @@ void* oom_killer_realloc_purgeable(struct _malloc_zone_t* zone,
void* oom_killer_memalign_purgeable(struct _malloc_zone_t* zone, void* oom_killer_memalign_purgeable(struct _malloc_zone_t* zone,
size_t alignment, size_t alignment,
size_t size) { size_t size) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
#endif // ARCH_CPU_32_BITS #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
void* result = g_old_memalign_purgeable(zone, alignment, size); void* result = g_old_memalign_purgeable(zone, alignment, size);
// Only die if posix_memalign would have returned ENOMEM, since there are // Only die if posix_memalign would have returned ENOMEM, since there are
// other reasons why NULL might be returned (see // other reasons why NULL might be returned (see
...@@ -429,12 +433,16 @@ void* oom_killer_memalign_purgeable(struct _malloc_zone_t* zone, ...@@ -429,12 +433,16 @@ void* oom_killer_memalign_purgeable(struct _malloc_zone_t* zone,
return result; return result;
} }
#endif // !defined(ADDRESS_SANITIZER)
// === C++ operator new === // === C++ operator new ===
void oom_killer_new() { void oom_killer_new() {
debug::BreakDebugger(); debug::BreakDebugger();
} }
#if !defined(ADDRESS_SANITIZER)
// === Core Foundation CFAllocators === // === Core Foundation CFAllocators ===
bool CanGetContextForCFAllocator() { bool CanGetContextForCFAllocator() {
...@@ -491,6 +499,8 @@ void* oom_killer_cfallocator_malloc_zone(CFIndex alloc_size, ...@@ -491,6 +499,8 @@ void* oom_killer_cfallocator_malloc_zone(CFIndex alloc_size,
return result; return result;
} }
#endif // !defined(ADDRESS_SANITIZER)
// === Cocoa NSObject allocation === // === Cocoa NSObject allocation ===
typedef id (*allocWithZone_t)(id, SEL, NSZone*); typedef id (*allocWithZone_t)(id, SEL, NSZone*);
...@@ -507,29 +517,37 @@ id oom_killer_allocWithZone(id self, SEL _cmd, NSZone* zone) ...@@ -507,29 +517,37 @@ id oom_killer_allocWithZone(id self, SEL _cmd, NSZone* zone)
} // namespace } // namespace
bool UncheckedMalloc(size_t size, void** result) { bool UncheckedMalloc(size_t size, void** result) {
#if defined(ADDRESS_SANITIZER)
*result = malloc(size);
#else
if (g_old_malloc) { if (g_old_malloc) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); 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); *result = g_old_malloc(malloc_default_zone(), size);
} else { } else {
*result = malloc(size); *result = malloc(size);
} }
#endif // defined(ADDRESS_SANITIZER)
return *result != NULL; return *result != NULL;
} }
bool UncheckedCalloc(size_t num_items, size_t size, void** result) { bool UncheckedCalloc(size_t num_items, size_t size, void** result) {
#if defined(ADDRESS_SANITIZER)
*result = calloc(num_items, size);
#else
if (g_old_calloc) { if (g_old_calloc) {
#if ARCH_CPU_32_BITS #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
ScopedClearErrno clear_errno; ScopedClearErrno clear_errno;
ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); 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); *result = g_old_calloc(malloc_default_zone(), num_items, size);
} else { } else {
*result = calloc(num_items, size); *result = calloc(num_items, size);
} }
#endif // defined(ADDRESS_SANITIZER)
return *result != NULL; return *result != NULL;
} }
...@@ -559,6 +577,10 @@ void EnableTerminationOnOutOfMemory() { ...@@ -559,6 +577,10 @@ void EnableTerminationOnOutOfMemory() {
// Unfortunately, it's the best we can do. Also note that this does not affect // Unfortunately, it's the best we can do. Also note that this does not affect
// allocations from non-default zones. // 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 && CHECK(!g_old_malloc && !g_old_calloc && !g_old_valloc && !g_old_realloc &&
!g_old_memalign) << "Old allocators unexpectedly non-null"; !g_old_memalign) << "Old allocators unexpectedly non-null";
...@@ -566,10 +588,6 @@ void EnableTerminationOnOutOfMemory() { ...@@ -566,10 +588,6 @@ void EnableTerminationOnOutOfMemory() {
!g_old_valloc_purgeable && !g_old_realloc_purgeable && !g_old_valloc_purgeable && !g_old_realloc_purgeable &&
!g_old_memalign_purgeable) << "Old allocators unexpectedly non-null"; !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 = ChromeMallocZone* default_zone =
reinterpret_cast<ChromeMallocZone*>(malloc_default_zone()); reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
ChromeMallocZone* purgeable_zone = ChromeMallocZone* purgeable_zone =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment