1 // Copyright 2016 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 #include "base/compiler_specific.h"
7 
8 #include <dlfcn.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #ifndef __DragonFly__
12 #include <malloc_np.h>
13 #endif
14 
15 // This translation unit defines a default dispatch for the allocator shim which
16 // routes allocations to libc functions.
17 // The code here is strongly inspired from tcmalloc's override_glibc.h.
18 
19 extern "C" {
20 void* __malloc(size_t size);
21 void* __calloc(size_t n, size_t size);
22 void* __realloc(void* address, size_t len);
23 #ifdef __DragonFly__
24 int __posix_memalign(void **ptr, size_t alignment, size_t size);
25 #endif
__memalign(size_t alignment,size_t size)26 void* __memalign(size_t alignment, size_t size) {
27   void *ret;
28   if (__posix_memalign(&ret, alignment, size) != 0) {
29     return nullptr;
30   } else {
31     return ret;
32   }
33 }
34 int __posix_memalign(void **ptr, size_t alignment, size_t size);
35 void __free(void* ptr);
36 }  // extern "C"
37 
38 namespace {
39 
40 using base::allocator::AllocatorDispatch;
41 
GlibcMalloc(const AllocatorDispatch *,size_t size,void * context)42 void* GlibcMalloc(const AllocatorDispatch*, size_t size, void* context) {
43   return __malloc(size);
44 }
45 
GlibcCalloc(const AllocatorDispatch *,size_t n,size_t size,void * context)46 void* GlibcCalloc(const AllocatorDispatch*,
47                   size_t n,
48                   size_t size,
49                   void* context) {
50   return __calloc(n, size);
51 }
52 
GlibcRealloc(const AllocatorDispatch *,void * address,size_t size,void * context)53 void* GlibcRealloc(const AllocatorDispatch*,
54                    void* address,
55                    size_t size,
56                    void* context) {
57   return __realloc(address, size);
58 }
59 
GlibcMemalign(const AllocatorDispatch *,size_t alignment,size_t size,void * context)60 void* GlibcMemalign(const AllocatorDispatch*,
61                     size_t alignment,
62                     size_t size,
63                     void* context) {
64   return __memalign(alignment, size);
65 }
66 
GlibcFree(const AllocatorDispatch *,void * address,void * context)67 void GlibcFree(const AllocatorDispatch*, void* address, void* context) {
68   __free(address);
69 }
70 
71 NO_SANITIZE("cfi-icall")
GlibcGetSizeEstimate(const AllocatorDispatch *,void * address,void * context)72 size_t GlibcGetSizeEstimate(const AllocatorDispatch*,
73                             void* address,
74                             void* context) {
75   // glibc does not expose an alias to resolve malloc_usable_size. Dynamically
76   // resolve it instead. This should be safe because glibc (and hence dlfcn)
77   // does not use malloc_size internally and so there should not be a risk of
78   // recursion.
79   using MallocUsableSizeFunction = decltype(malloc_usable_size)*;
80   static MallocUsableSizeFunction fn_ptr =
81       reinterpret_cast<MallocUsableSizeFunction>(
82           dlsym(RTLD_NEXT, "malloc_usable_size"));
83 
84   return fn_ptr(address);
85 }
86 
87 }  // namespace
88 
89 const AllocatorDispatch AllocatorDispatch::default_dispatch = {
90     &GlibcMalloc,          /* alloc_function */
91     &GlibcCalloc,          /* alloc_zero_initialized_function */
92     &GlibcMemalign,        /* alloc_aligned_function */
93     &GlibcRealloc,         /* realloc_function */
94     &GlibcFree,            /* free_function */
95     &GlibcGetSizeEstimate, /* get_size_estimate_function */
96     nullptr,               /* batch_malloc_function */
97     nullptr,               /* batch_free_function */
98     nullptr,               /* free_definite_size_function */
99     nullptr,               /* aligned_malloc_function */
100     nullptr,               /* aligned_realloc_function */
101     nullptr,               /* aligned_free_function */
102     nullptr,               /* next */
103 };
104