Home | History | Annotate | Download | only in allocator
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/allocator/allocator_shim.h"
      6 
      7 #include <config.h>
      8 #include "base/allocator/allocator_extension_thunks.h"
      9 #include "base/profiler/alternate_timer.h"
     10 #include "base/sysinfo.h"
     11 #include "jemalloc.h"
     12 
     13 // This shim make it possible to use different allocators via an environment
     14 // variable set before running the program. This may reduce the
     15 // amount of inlining that we get with malloc/free/etc.
     16 
     17 // TODO(mbelshe): Ensure that all calls to tcmalloc have the proper call depth
     18 // from the "user code" so that debugging tools (HeapChecker) can work.
     19 
     20 // __THROW is defined in glibc systems.  It means, counter-intuitively,
     21 // "This function will never throw an exception."  It's an optional
     22 // optimization tool, but we may need to use it to match glibc prototypes.
     23 #ifndef __THROW    // I guess we're not on a glibc system
     24 # define __THROW   // __THROW is just an optimization, so ok to make it ""
     25 #endif
     26 
     27 // new_mode behaves similarly to MSVC's _set_new_mode.
     28 // If flag is 0 (default), calls to malloc will behave normally.
     29 // If flag is 1, calls to malloc will behave like calls to new,
     30 // and the std_new_handler will be invoked on failure.
     31 // Can be set by calling _set_new_mode().
     32 static int new_mode = 0;
     33 
     34 typedef enum {
     35   TCMALLOC,    // TCMalloc is the default allocator.
     36   JEMALLOC,    // JEMalloc.
     37   WINHEAP,  // Windows Heap (standard Windows allocator).
     38   WINLFH,      // Windows LFH Heap.
     39 } Allocator;
     40 
     41 // This is the default allocator. This value can be changed at startup by
     42 // specifying environment variables shown below it.
     43 // See SetupSubprocessAllocator() to specify a default secondary (subprocess)
     44 // allocator.
     45 // TODO(jar): Switch to using TCMALLOC for the renderer as well.
     46 #if (defined(ADDRESS_SANITIZER) && defined(OS_WIN))
     47 // The Windows implementation of Asan requires the use of "WINHEAP".
     48 static Allocator allocator = WINHEAP;
     49 #else
     50 static Allocator allocator = TCMALLOC;
     51 #endif
     52 // The names of the environment variables that can optionally control the
     53 // selection of the allocator.  The primary may be used to control overall
     54 // allocator selection, and the secondary can be used to specify an allocator
     55 // to use in sub-processes.
     56 static const char primary_name[] = "CHROME_ALLOCATOR";
     57 static const char secondary_name[] = "CHROME_ALLOCATOR_2";
     58 
     59 // We include tcmalloc and the win_allocator to get as much inlining as
     60 // possible.
     61 #include "debugallocation_shim.cc"
     62 #include "win_allocator.cc"
     63 
     64 // Forward declarations from jemalloc.
     65 extern "C" {
     66 void* je_malloc(size_t s);
     67 void* je_realloc(void* p, size_t s);
     68 void je_free(void* s);
     69 size_t je_msize(void* p);
     70 bool je_malloc_init_hard();
     71 void* je_memalign(size_t a, size_t s);
     72 }
     73 
     74 // Call the new handler, if one has been set.
     75 // Returns true on successfully calling the handler, false otherwise.
     76 inline bool call_new_handler(bool nothrow) {
     77   // Get the current new handler.  NB: this function is not
     78   // thread-safe.  We make a feeble stab at making it so here, but
     79   // this lock only protects against tcmalloc interfering with
     80   // itself, not with other libraries calling set_new_handler.
     81   std::new_handler nh;
     82   {
     83     SpinLockHolder h(&set_new_handler_lock);
     84     nh = std::set_new_handler(0);
     85     (void) std::set_new_handler(nh);
     86   }
     87 #if (defined(__GNUC__) && !defined(__EXCEPTIONS)) || \
     88     (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS)
     89   if (!nh)
     90     return false;
     91   // Since exceptions are disabled, we don't really know if new_handler
     92   // failed.  Assume it will abort if it fails.
     93   (*nh)();
     94   return false;  // break out of the retry loop.
     95 #else
     96   // If no new_handler is established, the allocation failed.
     97   if (!nh) {
     98     if (nothrow)
     99       return false;
    100     throw std::bad_alloc();
    101   }
    102   // Otherwise, try the new_handler.  If it returns, retry the
    103   // allocation.  If it throws std::bad_alloc, fail the allocation.
    104   // if it throws something else, don't interfere.
    105   try {
    106     (*nh)();
    107   } catch (const std::bad_alloc&) {
    108     if (!nothrow)
    109       throw;
    110     return true;
    111   }
    112 #endif  // (defined(__GNUC__) && !defined(__EXCEPTIONS)) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS)
    113   return false;
    114 }
    115 
    116 extern "C" {
    117 void* malloc(size_t size) __THROW {
    118   void* ptr;
    119   for (;;) {
    120     switch (allocator) {
    121       case JEMALLOC:
    122         ptr = je_malloc(size);
    123         break;
    124       case WINHEAP:
    125       case WINLFH:
    126         ptr = win_heap_malloc(size);
    127         break;
    128       case TCMALLOC:
    129       default:
    130         ptr = do_malloc(size);
    131         break;
    132     }
    133     if (ptr)
    134       return ptr;
    135 
    136     if (!new_mode || !call_new_handler(true))
    137       break;
    138   }
    139   return ptr;
    140 }
    141 
    142 void free(void* p) __THROW {
    143   switch (allocator) {
    144     case JEMALLOC:
    145       je_free(p);
    146       return;
    147     case WINHEAP:
    148     case WINLFH:
    149       win_heap_free(p);
    150       return;
    151     case TCMALLOC:
    152       do_free(p);
    153       return;
    154   }
    155 }
    156 
    157 void* realloc(void* ptr, size_t size) __THROW {
    158   // Webkit is brittle for allocators that return NULL for malloc(0).  The
    159   // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure
    160   // to call malloc for this case.
    161   if (!ptr)
    162     return malloc(size);
    163 
    164   void* new_ptr;
    165   for (;;) {
    166     switch (allocator) {
    167       case JEMALLOC:
    168         new_ptr = je_realloc(ptr, size);
    169         break;
    170       case WINHEAP:
    171       case WINLFH:
    172         new_ptr = win_heap_realloc(ptr, size);
    173         break;
    174       case TCMALLOC:
    175       default:
    176         new_ptr = do_realloc(ptr, size);
    177         break;
    178     }
    179 
    180     // Subtle warning:  NULL return does not alwas indicate out-of-memory.  If
    181     // the requested new size is zero, realloc should free the ptr and return
    182     // NULL.
    183     if (new_ptr || !size)
    184       return new_ptr;
    185     if (!new_mode || !call_new_handler(true))
    186       break;
    187   }
    188   return new_ptr;
    189 }
    190 
    191 // TODO(mbelshe): Implement this for other allocators.
    192 void malloc_stats(void) __THROW {
    193   switch (allocator) {
    194     case JEMALLOC:
    195       // No stats.
    196       return;
    197     case WINHEAP:
    198     case WINLFH:
    199       // No stats.
    200       return;
    201     case TCMALLOC:
    202       tc_malloc_stats();
    203       return;
    204   }
    205 }
    206 
    207 #ifdef WIN32
    208 
    209 extern "C" size_t _msize(void* p) {
    210   switch (allocator) {
    211     case JEMALLOC:
    212       return je_msize(p);
    213     case WINHEAP:
    214     case WINLFH:
    215       return win_heap_msize(p);
    216   }
    217 
    218   // TCMALLOC
    219   return MallocExtension::instance()->GetAllocatedSize(p);
    220 }
    221 
    222 // This is included to resolve references from libcmt.
    223 extern "C" intptr_t _get_heap_handle() {
    224   return 0;
    225 }
    226 
    227 static bool get_allocator_waste_size_thunk(size_t* size) {
    228   switch (allocator) {
    229     case JEMALLOC:
    230     case WINHEAP:
    231     case WINLFH:
    232       // TODO(alexeif): Implement for allocators other than tcmalloc.
    233       return false;
    234   }
    235   size_t heap_size, allocated_bytes, unmapped_bytes;
    236   MallocExtension* ext = MallocExtension::instance();
    237   if (ext->GetNumericProperty("generic.heap_size", &heap_size) &&
    238       ext->GetNumericProperty("generic.current_allocated_bytes",
    239                               &allocated_bytes) &&
    240       ext->GetNumericProperty("tcmalloc.pageheap_unmapped_bytes",
    241                               &unmapped_bytes)) {
    242     *size = heap_size - allocated_bytes - unmapped_bytes;
    243     return true;
    244   }
    245   return false;
    246 }
    247 
    248 static void get_stats_thunk(char* buffer, int buffer_length) {
    249   MallocExtension::instance()->GetStats(buffer, buffer_length);
    250 }
    251 
    252 static void release_free_memory_thunk() {
    253   MallocExtension::instance()->ReleaseFreeMemory();
    254 }
    255 
    256 // The CRT heap initialization stub.
    257 extern "C" int _heap_init() {
    258 // Don't use the environment variable if ADDRESS_SANITIZER is defined on
    259 // Windows, as the implementation requires Winheap to be the allocator.
    260 #if !(defined(ADDRESS_SANITIZER) && defined(OS_WIN))
    261   const char* environment_value = GetenvBeforeMain(primary_name);
    262   if (environment_value) {
    263     if (!stricmp(environment_value, "jemalloc"))
    264       allocator = JEMALLOC;
    265     else if (!stricmp(environment_value, "winheap"))
    266       allocator = WINHEAP;
    267     else if (!stricmp(environment_value, "winlfh"))
    268       allocator = WINLFH;
    269     else if (!stricmp(environment_value, "tcmalloc"))
    270       allocator = TCMALLOC;
    271   }
    272 #endif
    273 
    274   switch (allocator) {
    275     case JEMALLOC:
    276       return je_malloc_init_hard() ? 0 : 1;
    277     case WINHEAP:
    278       return win_heap_init(false) ? 1 : 0;
    279     case WINLFH:
    280       return win_heap_init(true) ? 1 : 0;
    281     case TCMALLOC:
    282     default:
    283       // fall through
    284       break;
    285   }
    286 
    287   // Initializing tcmalloc.
    288   // We intentionally leak this object.  It lasts for the process
    289   // lifetime.  Trying to teardown at _heap_term() is so late that
    290   // you can't do anything useful anyway.
    291   new TCMallocGuard();
    292 
    293   // Provide optional hook for monitoring allocation quantities on a per-thread
    294   // basis.  Only set the hook if the environment indicates this needs to be
    295   // enabled.
    296   const char* profiling =
    297       GetenvBeforeMain(tracked_objects::kAlternateProfilerTime);
    298   if (profiling && *profiling == '1') {
    299     tracked_objects::SetAlternateTimeSource(
    300         tcmalloc::ThreadCache::GetBytesAllocatedOnCurrentThread,
    301         tracked_objects::TIME_SOURCE_TYPE_TCMALLOC);
    302   }
    303 
    304   base::allocator::thunks::SetGetAllocatorWasteSizeFunction(
    305       get_allocator_waste_size_thunk);
    306   base::allocator::thunks::SetGetStatsFunction(get_stats_thunk);
    307   base::allocator::thunks::SetReleaseFreeMemoryFunction(
    308       release_free_memory_thunk);
    309 
    310   return 1;
    311 }
    312 
    313 // The CRT heap cleanup stub.
    314 extern "C" void _heap_term() {}
    315 
    316 // We set this to 1 because part of the CRT uses a check of _crtheap != 0
    317 // to test whether the CRT has been initialized.  Once we've ripped out
    318 // the allocators from libcmt, we need to provide this definition so that
    319 // the rest of the CRT is still usable.
    320 extern "C" void* _crtheap = reinterpret_cast<void*>(1);
    321 
    322 // Provide support for aligned memory through Windows only _aligned_malloc().
    323 void* _aligned_malloc(size_t size, size_t alignment) {
    324   // _aligned_malloc guarantees parameter validation, so do so here.  These
    325   // checks are somewhat stricter than _aligned_malloc() since we're effectively
    326   // using memalign() under the hood.
    327   DCHECK_GT(size, 0U);
    328   DCHECK_EQ(alignment & (alignment - 1), 0U);
    329   DCHECK_EQ(alignment % sizeof(void*), 0U);
    330 
    331   void* ptr;
    332   for (;;) {
    333     switch (allocator) {
    334       case JEMALLOC:
    335         ptr = je_memalign(alignment, size);
    336         break;
    337       case WINHEAP:
    338       case WINLFH:
    339         ptr = win_heap_memalign(alignment, size);
    340         break;
    341       case TCMALLOC:
    342       default:
    343         ptr = tc_memalign(alignment, size);
    344         break;
    345     }
    346 
    347     if (ptr) {
    348       // Sanity check alignment.
    349       DCHECK_EQ(reinterpret_cast<uintptr_t>(ptr) & (alignment - 1), 0U);
    350       return ptr;
    351     }
    352 
    353     if (!new_mode || !call_new_handler(true))
    354       break;
    355   }
    356   return ptr;
    357 }
    358 
    359 void _aligned_free(void* p) {
    360   // Both JEMalloc and TCMalloc return pointers from memalign() that are safe to
    361   // use with free().  Pointers allocated with win_heap_memalign() MUST be freed
    362   // via win_heap_memalign_free() since the aligned pointer is not the real one.
    363   switch (allocator) {
    364     case JEMALLOC:
    365       je_free(p);
    366       return;
    367     case WINHEAP:
    368     case WINLFH:
    369       win_heap_memalign_free(p);
    370       return;
    371     case TCMALLOC:
    372       do_free(p);
    373   }
    374 }
    375 
    376 #endif  // WIN32
    377 
    378 #include "generic_allocators.cc"
    379 
    380 }  // extern C
    381 
    382 namespace base {
    383 namespace allocator {
    384 
    385 void SetupSubprocessAllocator() {
    386   size_t primary_length = 0;
    387   getenv_s(&primary_length, NULL, 0, primary_name);
    388 
    389   size_t secondary_length = 0;
    390   char buffer[20];
    391   getenv_s(&secondary_length, buffer, sizeof(buffer), secondary_name);
    392   DCHECK_GT(sizeof(buffer), secondary_length);
    393   buffer[sizeof(buffer) - 1] = '\0';
    394 
    395   if (secondary_length || !primary_length) {
    396     // Don't use the environment variable if ADDRESS_SANITIZER is defined on
    397     // Windows, as the implementation require Winheap to be the allocator.
    398 #if !(defined(ADDRESS_SANITIZER) && defined(OS_WIN))
    399     const char* secondary_value = secondary_length ? buffer : "TCMALLOC";
    400     // Force renderer (or other subprocesses) to use secondary_value.
    401 #else
    402     const char* secondary_value = "WINHEAP";
    403 #endif
    404     int ret_val = _putenv_s(primary_name, secondary_value);
    405     DCHECK_EQ(0, ret_val);
    406   }
    407 }
    408 
    409 void* TCMallocDoMallocForTest(size_t size) {
    410   return do_malloc(size);
    411 }
    412 
    413 void TCMallocDoFreeForTest(void* ptr) {
    414   do_free(ptr);
    415 }
    416 
    417 size_t ExcludeSpaceForMarkForTest(size_t size) {
    418   return ExcludeSpaceForMark(size);
    419 }
    420 
    421 }  // namespace allocator.
    422 }  // namespace base.
    423