1 // Copyright 2024 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 #include "partition_alloc/shim/allocator_shim_dispatch_to_noop_on_free.h"
6 
7 #include <cstddef>
8 
9 #include "partition_alloc/partition_alloc_check.h"
10 #include "partition_alloc/shim/allocator_dispatch.h"
11 #include "partition_alloc/shim/allocator_shim.h"
12 
13 namespace allocator_shim {
14 namespace {
AllocFn(const AllocatorDispatch * self,size_t size,void * context)15 void* AllocFn(const AllocatorDispatch* self, size_t size, void* context) {
16   return self->next->alloc_function(self->next, size, context);
17 }
18 
AllocUncheckedFn(const AllocatorDispatch * self,size_t size,void * context)19 void* AllocUncheckedFn(const AllocatorDispatch* self,
20                        size_t size,
21                        void* context) {
22   return self->next->alloc_unchecked_function(self->next, size, context);
23 }
24 
AllocZeroInitializedFn(const AllocatorDispatch * self,size_t n,size_t size,void * context)25 void* AllocZeroInitializedFn(const AllocatorDispatch* self,
26                              size_t n,
27                              size_t size,
28                              void* context) {
29   return self->next->alloc_zero_initialized_function(self->next, n, size,
30                                                      context);
31 }
32 
AllocAlignedFn(const AllocatorDispatch * self,size_t alignment,size_t size,void * context)33 void* AllocAlignedFn(const AllocatorDispatch* self,
34                      size_t alignment,
35                      size_t size,
36                      void* context) {
37   return self->next->alloc_aligned_function(self->next, alignment, size,
38                                             context);
39 }
40 
ReallocFn(const AllocatorDispatch * self,void * address,size_t size,void * context)41 void* ReallocFn(const AllocatorDispatch* self,
42                 void* address,
43                 size_t size,
44                 void* context) {
45   return self->next->realloc_function(self->next, address, size, context);
46 }
47 
FreeFn(const AllocatorDispatch * self,void * address,void * context)48 void FreeFn(const AllocatorDispatch* self, void* address, void* context) {}
49 
GetSizeEstimateFn(const AllocatorDispatch * self,void * address,void * context)50 size_t GetSizeEstimateFn(const AllocatorDispatch* self,
51                          void* address,
52                          void* context) {
53   return self->next->get_size_estimate_function(self->next, address, context);
54 }
55 
GoodSizeFn(const AllocatorDispatch * self,size_t size,void * context)56 size_t GoodSizeFn(const AllocatorDispatch* self, size_t size, void* context) {
57   return self->next->good_size_function(self->next, size, context);
58 }
59 
ClaimedAddressFn(const AllocatorDispatch * self,void * address,void * context)60 bool ClaimedAddressFn(const AllocatorDispatch* self,
61                       void* address,
62                       void* context) {
63   return self->next->claimed_address_function(self->next, address, context);
64 }
65 
BatchMallocFn(const AllocatorDispatch * self,size_t size,void ** results,unsigned num_requested,void * context)66 unsigned BatchMallocFn(const AllocatorDispatch* self,
67                        size_t size,
68                        void** results,
69                        unsigned num_requested,
70                        void* context) {
71   return self->next->batch_malloc_function(self->next, size, results,
72                                            num_requested, context);
73 }
74 
BatchFreeFn(const AllocatorDispatch * self,void ** to_be_freed,unsigned num_to_be_freed,void * context)75 void BatchFreeFn(const AllocatorDispatch* self,
76                  void** to_be_freed,
77                  unsigned num_to_be_freed,
78                  void* context) {}
79 
FreeDefiniteSizeFn(const AllocatorDispatch * self,void * address,size_t size,void * context)80 void FreeDefiniteSizeFn(const AllocatorDispatch* self,
81                         void* address,
82                         size_t size,
83                         void* context) {}
84 
TryFreeDefaultFn(const AllocatorDispatch * self,void * address,void * context)85 void TryFreeDefaultFn(const AllocatorDispatch* self,
86                       void* address,
87                       void* context) {}
88 
AlignedMallocFn(const AllocatorDispatch * self,size_t size,size_t alignment,void * context)89 static void* AlignedMallocFn(const AllocatorDispatch* self,
90                              size_t size,
91                              size_t alignment,
92                              void* context) {
93   return self->next->aligned_malloc_function(self->next, size, alignment,
94                                              context);
95 }
96 
AlignedReallocFn(const AllocatorDispatch * self,void * address,size_t size,size_t alignment,void * context)97 static void* AlignedReallocFn(const AllocatorDispatch* self,
98                               void* address,
99                               size_t size,
100                               size_t alignment,
101                               void* context) {
102   return self->next->aligned_realloc_function(self->next, address, size,
103                                               alignment, context);
104 }
105 
AlignedFreeFn(const AllocatorDispatch * self,void * address,void * context)106 static void AlignedFreeFn(const AllocatorDispatch* self,
107                           void* address,
108                           void* context) {}
109 
110 AllocatorDispatch allocator_dispatch = {
111     &AllocFn,
112     &AllocUncheckedFn,
113     &AllocZeroInitializedFn,
114     &AllocAlignedFn,
115     &ReallocFn,
116     &FreeFn,
117     &GetSizeEstimateFn,
118     &GoodSizeFn,
119     &ClaimedAddressFn,
120     &BatchMallocFn,
121     &BatchFreeFn,
122     &FreeDefiniteSizeFn,
123     &TryFreeDefaultFn,
124     &AlignedMallocFn,
125     &AlignedReallocFn,
126     &AlignedFreeFn,
127     nullptr /* next */
128 };
129 }  // namespace
130 
InsertNoOpOnFreeAllocatorShimOnShutDown()131 void InsertNoOpOnFreeAllocatorShimOnShutDown() {
132   static bool called = false;
133   PA_CHECK(!called);
134   called = true;
135   InsertAllocatorDispatch(&allocator_dispatch);
136 }
137 
138 }  // namespace allocator_shim
139