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 PARTITION_ALLOC_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 PARTITION_ALLOC_SHIM_ALLOCATOR_SHIM_OVERRIDE_LIBC_SYMBOLS_H_
13 #define PARTITION_ALLOC_SHIM_ALLOCATOR_SHIM_OVERRIDE_LIBC_SYMBOLS_H_
14 
15 #include "partition_alloc/partition_alloc_buildflags.h"
16 
17 #if BUILDFLAG(USE_ALLOCATOR_SHIM)
18 #include "build/build_config.h"
19 
20 #if BUILDFLAG(IS_APPLE)
21 #include <malloc/malloc.h>
22 #else
23 #include <malloc.h>
24 #endif
25 
26 #include "partition_alloc/shim/allocator_shim_internals.h"
27 
28 extern "C" {
29 
30 // WARNING: Whenever a new function is added there (which, surprisingly enough,
31 // happens. For instance glibc 2.33 introduced mallinfo2(), which we don't
32 // support... yet?), it MUST be added to build/linux/chrome.map.
33 //
34 // Otherwise the new symbol is not exported from Chromium's main binary, which
35 // is necessary to override libc's weak symbol, which in turn is necessary to
36 // intercept calls made by dynamic libraries. See crbug.com/1292206 for such
37 // an example.
38 
malloc(size_t size)39 SHIM_ALWAYS_EXPORT void* malloc(size_t size) __THROW {
40   return ShimMalloc(size, nullptr);
41 }
42 
free(void * ptr)43 SHIM_ALWAYS_EXPORT void free(void* ptr) __THROW {
44   ShimFree(ptr, nullptr);
45 }
46 
realloc(void * ptr,size_t size)47 SHIM_ALWAYS_EXPORT void* realloc(void* ptr, size_t size) __THROW {
48   return ShimRealloc(ptr, size, nullptr);
49 }
50 
calloc(size_t n,size_t size)51 SHIM_ALWAYS_EXPORT void* calloc(size_t n, size_t size) __THROW {
52   return ShimCalloc(n, size, nullptr);
53 }
54 
cfree(void * ptr)55 SHIM_ALWAYS_EXPORT void cfree(void* ptr) __THROW {
56   ShimFree(ptr, nullptr);
57 }
58 
memalign(size_t align,size_t s)59 SHIM_ALWAYS_EXPORT void* memalign(size_t align, size_t s) __THROW {
60   return ShimMemalign(align, s, nullptr);
61 }
62 
aligned_alloc(size_t align,size_t s)63 SHIM_ALWAYS_EXPORT void* aligned_alloc(size_t align, size_t s) __THROW {
64   return ShimMemalign(align, s, nullptr);
65 }
66 
valloc(size_t size)67 SHIM_ALWAYS_EXPORT void* valloc(size_t size) __THROW {
68   return ShimValloc(size, nullptr);
69 }
70 
pvalloc(size_t size)71 SHIM_ALWAYS_EXPORT void* pvalloc(size_t size) __THROW {
72   return ShimPvalloc(size);
73 }
74 
posix_memalign(void ** r,size_t a,size_t s)75 SHIM_ALWAYS_EXPORT int posix_memalign(void** r, size_t a, size_t s) __THROW {
76   return ShimPosixMemalign(r, a, s);
77 }
78 
malloc_size(const void * address)79 SHIM_ALWAYS_EXPORT size_t malloc_size(const void* address) __THROW {
80   return ShimGetSizeEstimate(address, nullptr);
81 }
82 
malloc_usable_size(void * address)83 SHIM_ALWAYS_EXPORT size_t malloc_usable_size(void* address) __THROW {
84   return ShimGetSizeEstimate(address, nullptr);
85 }
86 
87 // The default dispatch translation unit has to define also the following
88 // symbols (unless they are ultimately routed to the system symbols):
89 //   void malloc_stats(void);
90 //   int mallopt(int, int);
91 //   struct mallinfo mallinfo(void);
92 
93 }  // extern "C"
94 
95 #endif  // BUILDFLAG(USE_ALLOCATOR_SHIM)
96 
97 #endif  // PARTITION_ALLOC_SHIM_ALLOCATOR_SHIM_OVERRIDE_LIBC_SYMBOLS_H_
98