summaryrefslogtreecommitdiff
path: root/base/allocator/allocator_shim_default_dispatch_to_linker_wrapped_symbols.cc
blob: e33754a443feffe5daeba1d7981cb81e89112bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <malloc.h>

#include "base/allocator/allocator_shim.h"
#include "build/build_config.h"

#if defined(OS_ANDROID) && __ANDROID_API__ < 17
#include <dlfcn.h>
#endif

// This translation unit defines a default dispatch for the allocator shim which
// routes allocations to the original libc functions when using the link-time
// -Wl,-wrap,malloc approach (see README.md).
// The __real_X functions here are special symbols that the linker will relocate
// against the real "X" undefined symbol, so that __real_malloc becomes the
// equivalent of what an undefined malloc symbol reference would have been.
// This is the counterpart of allocator_shim_override_linker_wrapped_symbols.h,
// which routes the __wrap_X functions into the shim.

extern "C" {
void* __real_malloc(size_t);
void* __real_calloc(size_t, size_t);
void* __real_realloc(void*, size_t);
void* __real_memalign(size_t, size_t);
void* __real_free(void*);
}  // extern "C"

namespace {

using base::allocator::AllocatorDispatch;

void* RealMalloc(const AllocatorDispatch*, size_t size, void* context) {
  return __real_malloc(size);
}

void* RealCalloc(const AllocatorDispatch*,
                 size_t n,
                 size_t size,
                 void* context) {
  return __real_calloc(n, size);
}

void* RealRealloc(const AllocatorDispatch*,
                  void* address,
                  size_t size,
                  void* context) {
  return __real_realloc(address, size);
}

void* RealMemalign(const AllocatorDispatch*,
                   size_t alignment,
                   size_t size,
                   void* context) {
  return __real_memalign(alignment, size);
}

void RealFree(const AllocatorDispatch*, void* address, void* context) {
  __real_free(address);
}

#if defined(OS_ANDROID) && __ANDROID_API__ < 17
size_t DummyMallocUsableSize(const void*) { return 0; }
#endif

size_t RealSizeEstimate(const AllocatorDispatch*,
                        void* address,
                        void* context) {
#if defined(OS_ANDROID)
#if __ANDROID_API__ < 17
  // malloc_usable_size() is available only starting from API 17.
  // TODO(dskiba): remove once we start building against 17+.
  using MallocUsableSizeFunction = decltype(malloc_usable_size)*;
  static MallocUsableSizeFunction usable_size_function = nullptr;
  if (!usable_size_function) {
    void* function_ptr = dlsym(RTLD_DEFAULT, "malloc_usable_size");
    if (function_ptr) {
      usable_size_function = reinterpret_cast<MallocUsableSizeFunction>(
          function_ptr);
    } else {
      usable_size_function = &DummyMallocUsableSize;
    }
  }
  return usable_size_function(address);
#else
  return malloc_usable_size(address);
#endif
#endif  // OS_ANDROID

  // TODO(primiano): This should be redirected to malloc_usable_size or
  //     the like.
  return 0;
}

}  // namespace

const AllocatorDispatch AllocatorDispatch::default_dispatch = {
    &RealMalloc,       /* alloc_function */
    &RealCalloc,       /* alloc_zero_initialized_function */
    &RealMemalign,     /* alloc_aligned_function */
    &RealRealloc,      /* realloc_function */
    &RealFree,         /* free_function */
    &RealSizeEstimate, /* get_size_estimate_function */
    nullptr,           /* batch_malloc_function */
    nullptr,           /* batch_free_function */
    nullptr,           /* free_definite_size_function */
    nullptr,           /* next */
};