1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 // Copyright (c) 2005, Google Inc.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // ---
32 // Author: Sanjay Ghemawat
33 
34 #include <config.h>
35 #include <errno.h>                      // for EAGAIN, errno
36 #include <fcntl.h>                      // for open, O_RDWR
37 #include <stddef.h>                     // for size_t, NULL, ptrdiff_t
38 #if defined HAVE_STDINT_H
39 #include <stdint.h>                     // for uintptr_t, intptr_t
40 #elif defined HAVE_INTTYPES_H
41 #include <inttypes.h>
42 #else
43 #include <sys/types.h>
44 #endif
45 #ifdef HAVE_MMAP
46 #include <sys/mman.h>                   // for munmap, mmap, MADV_DONTNEED, etc
47 #endif
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>                     // for sbrk, getpagesize, off_t
50 #endif
51 #include <new>                          // for operator new
52 #include <gperftools/malloc_extension.h>
53 #include "base/basictypes.h"
54 #include "base/commandlineflags.h"
55 #include "base/spinlock.h"              // for SpinLockHolder, SpinLock, etc
56 #include "common.h"
57 #include "internal_logging.h"
58 
59 // On systems (like freebsd) that don't define MAP_ANONYMOUS, use the old
60 // form of the name instead.
61 #ifndef MAP_ANONYMOUS
62 # define MAP_ANONYMOUS MAP_ANON
63 #endif
64 
65 // Linux added support for MADV_FREE in 4.5 but we aren't ready to use it
66 // yet. Among other things, using compile-time detection leads to poor
67 // results when compiling on a system with MADV_FREE and running on a
68 // system without it. See https://github.com/gperftools/gperftools/issues/780.
69 #if defined(__linux__) && defined(MADV_FREE) && !defined(TCMALLOC_USE_MADV_FREE)
70 # undef MADV_FREE
71 #endif
72 
73 // MADV_FREE is specifically designed for use by malloc(), but only
74 // FreeBSD supports it; in linux we fall back to the somewhat inferior
75 // MADV_DONTNEED.
76 #if !defined(MADV_FREE) && defined(MADV_DONTNEED)
77 # define MADV_FREE  MADV_DONTNEED
78 #endif
79 
80 // Solaris has a bug where it doesn't declare madvise() for C++.
81 //    http://www.opensolaris.org/jive/thread.jspa?threadID=21035&tstart=0
82 #if defined(__sun) && defined(__SVR4)
83 # include <sys/types.h>    // for caddr_t
84   extern "C" { extern int madvise(caddr_t, size_t, int); }
85 #endif
86 
87 // Set kDebugMode mode so that we can have use C++ conditionals
88 // instead of preprocessor conditionals.
89 #ifdef NDEBUG
90 static const bool kDebugMode = false;
91 #else
92 static const bool kDebugMode = true;
93 #endif
94 
95 // TODO(sanjay): Move the code below into the tcmalloc namespace
96 using tcmalloc::kLog;
97 using tcmalloc::Log;
98 
99 // Anonymous namespace to avoid name conflicts on "CheckAddressBits".
100 namespace {
101 
102 // Check that no bit is set at position ADDRESS_BITS or higher.
CheckAddressBits(uintptr_t ptr)103 template <int ADDRESS_BITS> bool CheckAddressBits(uintptr_t ptr) {
104   return (ptr >> ADDRESS_BITS) == 0;
105 }
106 
107 // Specialize for the bit width of a pointer to avoid undefined shift.
CheckAddressBits(uintptr_t ptr)108 template <> bool CheckAddressBits<8 * sizeof(void*)>(uintptr_t ptr) {
109   return true;
110 }
111 
112 }  // Anonymous namespace to avoid name conflicts on "CheckAddressBits".
113 
114 COMPILE_ASSERT(kAddressBits <= 8 * sizeof(void*),
115                address_bits_larger_than_pointer_size);
116 
117 static SpinLock spinlock(SpinLock::LINKER_INITIALIZED);
118 
119 #if defined(HAVE_MMAP) || defined(MADV_FREE)
120 // Page size is initialized on demand (only needed for mmap-based allocators)
121 static size_t pagesize = 0;
122 #endif
123 
124 // The current system allocator
125 SysAllocator* sys_alloc = NULL;
126 
127 // Number of bytes taken from system.
128 size_t TCMalloc_SystemTaken = 0;
129 
130 // Configuration parameters.
131 DEFINE_int32(malloc_devmem_start,
132              EnvToInt("TCMALLOC_DEVMEM_START", 0),
133              "Physical memory starting location in MB for /dev/mem allocation."
134              "  Setting this to 0 disables /dev/mem allocation");
135 DEFINE_int32(malloc_devmem_limit,
136              EnvToInt("TCMALLOC_DEVMEM_LIMIT", 0),
137              "Physical memory limit location in MB for /dev/mem allocation."
138              "  Setting this to 0 means no limit.");
139 DEFINE_bool(malloc_skip_sbrk,
140             EnvToBool("TCMALLOC_SKIP_SBRK", false),
141             "Whether sbrk can be used to obtain memory.");
142 DEFINE_bool(malloc_skip_mmap,
143             EnvToBool("TCMALLOC_SKIP_MMAP", false),
144             "Whether mmap can be used to obtain memory.");
145 DEFINE_bool(malloc_disable_memory_release,
146             EnvToBool("TCMALLOC_DISABLE_MEMORY_RELEASE", false),
147             "Whether MADV_FREE/MADV_DONTNEED should be used"
148             " to return unused memory to the system.");
149 
150 // static allocators
151 class SbrkSysAllocator : public SysAllocator {
152 public:
SbrkSysAllocator()153   SbrkSysAllocator() : SysAllocator() {
154   }
155   void* Alloc(size_t size, size_t *actual_size, size_t alignment);
156 };
157 static union {
158   char buf[sizeof(SbrkSysAllocator)];
159   void *ptr;
160 } sbrk_space;
161 
162 class MmapSysAllocator : public SysAllocator {
163 public:
MmapSysAllocator()164   MmapSysAllocator() : SysAllocator() {
165   }
166   void* Alloc(size_t size, size_t *actual_size, size_t alignment);
167 };
168 static union {
169   char buf[sizeof(MmapSysAllocator)];
170   void *ptr;
171 } mmap_space;
172 
173 class DevMemSysAllocator : public SysAllocator {
174 public:
DevMemSysAllocator()175   DevMemSysAllocator() : SysAllocator() {
176   }
177   void* Alloc(size_t size, size_t *actual_size, size_t alignment);
178 };
179 
180 class DefaultSysAllocator : public SysAllocator {
181  public:
DefaultSysAllocator()182   DefaultSysAllocator() : SysAllocator() {
183     for (int i = 0; i < kMaxAllocators; i++) {
184       failed_[i] = true;
185       allocs_[i] = NULL;
186       names_[i] = NULL;
187     }
188   }
SetChildAllocator(SysAllocator * alloc,unsigned int index,const char * name)189   void SetChildAllocator(SysAllocator* alloc, unsigned int index,
190                          const char* name) {
191     if (index < kMaxAllocators && alloc != NULL) {
192       allocs_[index] = alloc;
193       failed_[index] = false;
194       names_[index] = name;
195     }
196   }
197   void* Alloc(size_t size, size_t *actual_size, size_t alignment);
198 
199  private:
200   static const int kMaxAllocators = 2;
201   bool failed_[kMaxAllocators];
202   SysAllocator* allocs_[kMaxAllocators];
203   const char* names_[kMaxAllocators];
204 };
205 static union {
206   char buf[sizeof(DefaultSysAllocator)];
207   void *ptr;
208 } default_space;
209 static const char sbrk_name[] = "SbrkSysAllocator";
210 static const char mmap_name[] = "MmapSysAllocator";
211 
212 
Alloc(size_t size,size_t * actual_size,size_t alignment)213 void* SbrkSysAllocator::Alloc(size_t size, size_t *actual_size,
214                               size_t alignment) {
215 #if !defined(HAVE_SBRK) || defined(__UCLIBC__)
216   return NULL;
217 #else
218   // Check if we should use sbrk allocation.
219   // FLAGS_malloc_skip_sbrk starts out as false (its uninitialized
220   // state) and eventually gets initialized to the specified value.  Note
221   // that this code runs for a while before the flags are initialized.
222   // That means that even if this flag is set to true, some (initial)
223   // memory will be allocated with sbrk before the flag takes effect.
224   if (FLAGS_malloc_skip_sbrk) {
225     return NULL;
226   }
227 
228   // sbrk will release memory if passed a negative number, so we do
229   // a strict check here
230   if (static_cast<ptrdiff_t>(size + alignment) < 0) return NULL;
231 
232   // This doesn't overflow because TCMalloc_SystemAlloc has already
233   // tested for overflow at the alignment boundary.
234   size = ((size + alignment - 1) / alignment) * alignment;
235 
236   // "actual_size" indicates that the bytes from the returned pointer
237   // p up to and including (p + actual_size - 1) have been allocated.
238   if (actual_size) {
239     *actual_size = size;
240   }
241 
242   // Check that we we're not asking for so much more memory that we'd
243   // wrap around the end of the virtual address space.  (This seems
244   // like something sbrk() should check for us, and indeed opensolaris
245   // does, but glibc does not:
246   //    http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/sys/sbrk.c?a=true
247   //    http://sourceware.org/cgi-bin/cvsweb.cgi/~checkout~/libc/misc/sbrk.c?rev=1.1.2.1&content-type=text/plain&cvsroot=glibc
248   // Without this check, sbrk may succeed when it ought to fail.)
249   if (reinterpret_cast<intptr_t>(sbrk(0)) + size < size) {
250     return NULL;
251   }
252 
253   void* result = sbrk(size);
254   if (result == reinterpret_cast<void*>(-1)) {
255     return NULL;
256   }
257 
258   // Is it aligned?
259   uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
260   if ((ptr & (alignment-1)) == 0)  return result;
261 
262   // Try to get more memory for alignment
263   size_t extra = alignment - (ptr & (alignment-1));
264   void* r2 = sbrk(extra);
265   if (reinterpret_cast<uintptr_t>(r2) == (ptr + size)) {
266     // Contiguous with previous result
267     return reinterpret_cast<void*>(ptr + extra);
268   }
269 
270   // Give up and ask for "size + alignment - 1" bytes so
271   // that we can find an aligned region within it.
272   result = sbrk(size + alignment - 1);
273   if (result == reinterpret_cast<void*>(-1)) {
274     return NULL;
275   }
276   ptr = reinterpret_cast<uintptr_t>(result);
277   if ((ptr & (alignment-1)) != 0) {
278     ptr += alignment - (ptr & (alignment-1));
279   }
280   return reinterpret_cast<void*>(ptr);
281 #endif  // HAVE_SBRK
282 }
283 
Alloc(size_t size,size_t * actual_size,size_t alignment)284 void* MmapSysAllocator::Alloc(size_t size, size_t *actual_size,
285                               size_t alignment) {
286 #ifndef HAVE_MMAP
287   return NULL;
288 #else
289   // Check if we should use mmap allocation.
290   // FLAGS_malloc_skip_mmap starts out as false (its uninitialized
291   // state) and eventually gets initialized to the specified value.  Note
292   // that this code runs for a while before the flags are initialized.
293   // Chances are we never get here before the flags are initialized since
294   // sbrk is used until the heap is exhausted (before mmap is used).
295   if (FLAGS_malloc_skip_mmap) {
296     return NULL;
297   }
298 
299   // Enforce page alignment
300   if (pagesize == 0) pagesize = getpagesize();
301   if (alignment < pagesize) alignment = pagesize;
302   size_t aligned_size = ((size + alignment - 1) / alignment) * alignment;
303   if (aligned_size < size) {
304     return NULL;
305   }
306   size = aligned_size;
307 
308   // "actual_size" indicates that the bytes from the returned pointer
309   // p up to and including (p + actual_size - 1) have been allocated.
310   if (actual_size) {
311     *actual_size = size;
312   }
313 
314   // Ask for extra memory if alignment > pagesize
315   size_t extra = 0;
316   if (alignment > pagesize) {
317     extra = alignment - pagesize;
318   }
319 
320   // Note: size + extra does not overflow since:
321   //            size + alignment < (1<<NBITS).
322   // and        extra <= alignment
323   // therefore  size + extra < (1<<NBITS)
324   void* result = mmap(NULL, size + extra,
325                       PROT_READ|PROT_WRITE,
326                       MAP_PRIVATE|MAP_ANONYMOUS,
327                       -1, 0);
328   if (result == reinterpret_cast<void*>(MAP_FAILED)) {
329     return NULL;
330   }
331 
332   // Adjust the return memory so it is aligned
333   uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
334   size_t adjust = 0;
335   if ((ptr & (alignment - 1)) != 0) {
336     adjust = alignment - (ptr & (alignment - 1));
337   }
338 
339   // Return the unused memory to the system
340   if (adjust > 0) {
341     munmap(reinterpret_cast<void*>(ptr), adjust);
342   }
343   if (adjust < extra) {
344     munmap(reinterpret_cast<void*>(ptr + adjust + size), extra - adjust);
345   }
346 
347   ptr += adjust;
348   return reinterpret_cast<void*>(ptr);
349 #endif  // HAVE_MMAP
350 }
351 
Alloc(size_t size,size_t * actual_size,size_t alignment)352 void* DevMemSysAllocator::Alloc(size_t size, size_t *actual_size,
353                                 size_t alignment) {
354 #ifndef HAVE_MMAP
355   return NULL;
356 #else
357   static bool initialized = false;
358   static off_t physmem_base;  // next physical memory address to allocate
359   static off_t physmem_limit; // maximum physical address allowed
360   static int physmem_fd;      // file descriptor for /dev/mem
361 
362   // Check if we should use /dev/mem allocation.  Note that it may take
363   // a while to get this flag initialized, so meanwhile we fall back to
364   // the next allocator.  (It looks like 7MB gets allocated before
365   // this flag gets initialized -khr.)
366   if (FLAGS_malloc_devmem_start == 0) {
367     // NOTE: not a devmem_failure - we'd like TCMalloc_SystemAlloc to
368     // try us again next time.
369     return NULL;
370   }
371 
372   if (!initialized) {
373     physmem_fd = open("/dev/mem", O_RDWR);
374     if (physmem_fd < 0) {
375       return NULL;
376     }
377     physmem_base = FLAGS_malloc_devmem_start*1024LL*1024LL;
378     physmem_limit = FLAGS_malloc_devmem_limit*1024LL*1024LL;
379     initialized = true;
380   }
381 
382   // Enforce page alignment
383   if (pagesize == 0) pagesize = getpagesize();
384   if (alignment < pagesize) alignment = pagesize;
385   size_t aligned_size = ((size + alignment - 1) / alignment) * alignment;
386   if (aligned_size < size) {
387     return NULL;
388   }
389   size = aligned_size;
390 
391   // "actual_size" indicates that the bytes from the returned pointer
392   // p up to and including (p + actual_size - 1) have been allocated.
393   if (actual_size) {
394     *actual_size = size;
395   }
396 
397   // Ask for extra memory if alignment > pagesize
398   size_t extra = 0;
399   if (alignment > pagesize) {
400     extra = alignment - pagesize;
401   }
402 
403   // check to see if we have any memory left
404   if (physmem_limit != 0 &&
405       ((size + extra) > (physmem_limit - physmem_base))) {
406     return NULL;
407   }
408 
409   // Note: size + extra does not overflow since:
410   //            size + alignment < (1<<NBITS).
411   // and        extra <= alignment
412   // therefore  size + extra < (1<<NBITS)
413   void *result = mmap(0, size + extra, PROT_WRITE|PROT_READ,
414                       MAP_SHARED, physmem_fd, physmem_base);
415   if (result == reinterpret_cast<void*>(MAP_FAILED)) {
416     return NULL;
417   }
418   uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
419 
420   // Adjust the return memory so it is aligned
421   size_t adjust = 0;
422   if ((ptr & (alignment - 1)) != 0) {
423     adjust = alignment - (ptr & (alignment - 1));
424   }
425 
426   // Return the unused virtual memory to the system
427   if (adjust > 0) {
428     munmap(reinterpret_cast<void*>(ptr), adjust);
429   }
430   if (adjust < extra) {
431     munmap(reinterpret_cast<void*>(ptr + adjust + size), extra - adjust);
432   }
433 
434   ptr += adjust;
435   physmem_base += adjust + size;
436 
437   return reinterpret_cast<void*>(ptr);
438 #endif  // HAVE_MMAP
439 }
440 
Alloc(size_t size,size_t * actual_size,size_t alignment)441 void* DefaultSysAllocator::Alloc(size_t size, size_t *actual_size,
442                                  size_t alignment) {
443   for (int i = 0; i < kMaxAllocators; i++) {
444     if (!failed_[i] && allocs_[i] != NULL) {
445       void* result = allocs_[i]->Alloc(size, actual_size, alignment);
446       if (result != NULL) {
447         return result;
448       }
449       failed_[i] = true;
450     }
451   }
452   // After both failed, reset "failed_" to false so that a single failed
453   // allocation won't make the allocator never work again.
454   for (int i = 0; i < kMaxAllocators; i++) {
455     failed_[i] = false;
456   }
457   return NULL;
458 }
459 
460 ATTRIBUTE_WEAK ATTRIBUTE_NOINLINE
tc_get_sysalloc_override(SysAllocator * def)461 SysAllocator *tc_get_sysalloc_override(SysAllocator *def)
462 {
463   return def;
464 }
465 
466 static bool system_alloc_inited = false;
InitSystemAllocators(void)467 void InitSystemAllocators(void) {
468   MmapSysAllocator *mmap = new (mmap_space.buf) MmapSysAllocator();
469   SbrkSysAllocator *sbrk = new (sbrk_space.buf) SbrkSysAllocator();
470 
471   // In 64-bit debug mode, place the mmap allocator first since it
472   // allocates pointers that do not fit in 32 bits and therefore gives
473   // us better testing of code's 64-bit correctness.  It also leads to
474   // less false negatives in heap-checking code.  (Numbers are less
475   // likely to look like pointers and therefore the conservative gc in
476   // the heap-checker is less likely to misinterpret a number as a
477   // pointer).
478   DefaultSysAllocator *sdef = new (default_space.buf) DefaultSysAllocator();
479   if (kDebugMode && sizeof(void*) > 4) {
480     sdef->SetChildAllocator(mmap, 0, mmap_name);
481     sdef->SetChildAllocator(sbrk, 1, sbrk_name);
482   } else {
483     sdef->SetChildAllocator(sbrk, 0, sbrk_name);
484     sdef->SetChildAllocator(mmap, 1, mmap_name);
485   }
486 
487   sys_alloc = tc_get_sysalloc_override(sdef);
488 }
489 
TCMalloc_SystemAlloc(size_t size,size_t * actual_size,size_t alignment)490 void* TCMalloc_SystemAlloc(size_t size, size_t *actual_size,
491                            size_t alignment) {
492   // Discard requests that overflow
493   if (size + alignment < size) return NULL;
494 
495   SpinLockHolder lock_holder(&spinlock);
496 
497   if (!system_alloc_inited) {
498     InitSystemAllocators();
499     system_alloc_inited = true;
500   }
501 
502   // Enforce minimum alignment
503   if (alignment < sizeof(MemoryAligner)) alignment = sizeof(MemoryAligner);
504 
505   size_t actual_size_storage;
506   if (actual_size == NULL) {
507     actual_size = &actual_size_storage;
508   }
509 
510   void* result = sys_alloc->Alloc(size, actual_size, alignment);
511   if (result != NULL) {
512     CHECK_CONDITION(
513       CheckAddressBits<kAddressBits>(
514         reinterpret_cast<uintptr_t>(result) + *actual_size - 1));
515     TCMalloc_SystemTaken += *actual_size;
516   }
517   return result;
518 }
519 
TCMalloc_SystemRelease(void * start,size_t length)520 bool TCMalloc_SystemRelease(void* start, size_t length) {
521 #ifdef MADV_FREE
522   if (FLAGS_malloc_devmem_start) {
523     // It's not safe to use MADV_FREE/MADV_DONTNEED if we've been
524     // mapping /dev/mem for heap memory.
525     return false;
526   }
527   if (FLAGS_malloc_disable_memory_release) return false;
528   if (pagesize == 0) pagesize = getpagesize();
529   const size_t pagemask = pagesize - 1;
530 
531   size_t new_start = reinterpret_cast<size_t>(start);
532   size_t end = new_start + length;
533   size_t new_end = end;
534 
535   // Round up the starting address and round down the ending address
536   // to be page aligned:
537   new_start = (new_start + pagesize - 1) & ~pagemask;
538   new_end = new_end & ~pagemask;
539 
540   ASSERT((new_start & pagemask) == 0);
541   ASSERT((new_end & pagemask) == 0);
542   ASSERT(new_start >= reinterpret_cast<size_t>(start));
543   ASSERT(new_end <= end);
544 
545   if (new_end > new_start) {
546     int result;
547     do {
548       result = madvise(reinterpret_cast<char*>(new_start),
549           new_end - new_start, MADV_FREE);
550     } while (result == -1 && errno == EAGAIN);
551 
552     return result != -1;
553   }
554 #endif
555   return false;
556 }
557 
TCMalloc_SystemCommit(void * start,size_t length)558 void TCMalloc_SystemCommit(void* start, size_t length) {
559   // Nothing to do here.  TCMalloc_SystemRelease does not alter pages
560   // such that they need to be re-committed before they can be used by the
561   // application.
562 }
563