1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Its purpose is to preempt the Libc symbols for malloc/new so they call the
6 // shim layer entry points.
7
8 #ifdef BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_ALLOCATOR_SHIM_OVERRIDE_LIBC_SYMBOLS_H_
9 #error This header is meant to be included only once by allocator_shim.cc
10 #endif
11
12 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_ALLOCATOR_SHIM_OVERRIDE_LIBC_SYMBOLS_H_
13 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_ALLOCATOR_SHIM_OVERRIDE_LIBC_SYMBOLS_H_
14
15 #include "build/build_config.h"
16
17 #if BUILDFLAG(IS_APPLE)
18 #include <malloc/malloc.h>
19 #else
20 #include <malloc.h>
21 #endif
22
23 #include "base/allocator/partition_allocator/shim/allocator_shim_internals.h"
24
25 extern "C" {
26
27 // WARNING: Whenever a new function is added there (which, surprisingly enough,
28 // happens. For instance glibc 2.33 introduced mallinfo2(), which we don't
29 // support... yet?), it MUST be added to build/linux/chrome.map.
30 //
31 // Otherwise the new symbol is not exported from Chromium's main binary, which
32 // is necessary to override libc's weak symbol, which in turn is necessary to
33 // intercept calls made by dynamic libraries. See crbug.com/1292206 for such
34 // an example.
35
malloc(size_t size)36 SHIM_ALWAYS_EXPORT void* malloc(size_t size) __THROW {
37 return ShimMalloc(size, nullptr);
38 }
39
free(void * ptr)40 SHIM_ALWAYS_EXPORT void free(void* ptr) __THROW {
41 ShimFree(ptr, nullptr);
42 }
43
realloc(void * ptr,size_t size)44 SHIM_ALWAYS_EXPORT void* realloc(void* ptr, size_t size) __THROW {
45 return ShimRealloc(ptr, size, nullptr);
46 }
47
calloc(size_t n,size_t size)48 SHIM_ALWAYS_EXPORT void* calloc(size_t n, size_t size) __THROW {
49 return ShimCalloc(n, size, nullptr);
50 }
51
cfree(void * ptr)52 SHIM_ALWAYS_EXPORT void cfree(void* ptr) __THROW {
53 ShimFree(ptr, nullptr);
54 }
55
memalign(size_t align,size_t s)56 SHIM_ALWAYS_EXPORT void* memalign(size_t align, size_t s) __THROW {
57 return ShimMemalign(align, s, nullptr);
58 }
59
aligned_alloc(size_t align,size_t s)60 SHIM_ALWAYS_EXPORT void* aligned_alloc(size_t align, size_t s) __THROW {
61 return ShimMemalign(align, s, nullptr);
62 }
63
valloc(size_t size)64 SHIM_ALWAYS_EXPORT void* valloc(size_t size) __THROW {
65 return ShimValloc(size, nullptr);
66 }
67
pvalloc(size_t size)68 SHIM_ALWAYS_EXPORT void* pvalloc(size_t size) __THROW {
69 return ShimPvalloc(size);
70 }
71
posix_memalign(void ** r,size_t a,size_t s)72 SHIM_ALWAYS_EXPORT int posix_memalign(void** r, size_t a, size_t s) __THROW {
73 return ShimPosixMemalign(r, a, s);
74 }
75
malloc_size(const void * address)76 SHIM_ALWAYS_EXPORT size_t malloc_size(const void* address) __THROW {
77 return ShimGetSizeEstimate(address, nullptr);
78 }
79
malloc_usable_size(void * address)80 SHIM_ALWAYS_EXPORT size_t malloc_usable_size(void* address) __THROW {
81 return ShimGetSizeEstimate(address, nullptr);
82 }
83
84 // The default dispatch translation unit has to define also the following
85 // symbols (unless they are ultimately routed to the system symbols):
86 // void malloc_stats(void);
87 // int mallopt(int, int);
88 // struct mallinfo mallinfo(void);
89
90 } // extern "C"
91
92 #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_ALLOCATOR_SHIM_OVERRIDE_LIBC_SYMBOLS_H_
93