1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <inttypes.h>
18 #include <stdlib.h>
19 
20 #include "perfetto/ext/base/utils.h"
21 #include "perfetto/heap_profile.h"
22 #include "src/profiling/memory/wrap_allocators.h"
23 
24 namespace perfetto {
25 namespace profiling {
26 
27 namespace {
RoundUpToSysPageSize(size_t req_size)28 size_t RoundUpToSysPageSize(size_t req_size) {
29   const size_t page_size = base::GetSysPageSize();
30   return (req_size + page_size - 1) & ~(page_size - 1);
31 }
32 }  // namespace
33 
wrap_malloc(uint32_t heap_id,void * (* fn)(size_t),size_t size)34 void* wrap_malloc(uint32_t heap_id, void* (*fn)(size_t), size_t size) {
35   void* addr = fn(size);
36   AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
37                                 size);
38   return addr;
39 }
40 
wrap_calloc(uint32_t heap_id,void * (* fn)(size_t,size_t),size_t nmemb,size_t size)41 void* wrap_calloc(uint32_t heap_id,
42                   void* (*fn)(size_t, size_t),
43                   size_t nmemb,
44                   size_t size) {
45   void* addr = fn(nmemb, size);
46   AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
47                                 nmemb * size);
48   return addr;
49 }
50 
wrap_memalign(uint32_t heap_id,void * (* fn)(size_t,size_t),size_t alignment,size_t size)51 void* wrap_memalign(uint32_t heap_id,
52                     void* (*fn)(size_t, size_t),
53                     size_t alignment,
54                     size_t size) {
55   void* addr = fn(alignment, size);
56   AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
57                                 size);
58   return addr;
59 }
60 
wrap_posix_memalign(uint32_t heap_id,int (* fn)(void **,size_t,size_t),void ** memptr,size_t alignment,size_t size)61 int wrap_posix_memalign(uint32_t heap_id,
62                         int (*fn)(void**, size_t, size_t),
63                         void** memptr,
64                         size_t alignment,
65                         size_t size) {
66   int res = fn(memptr, alignment, size);
67   if (res != 0)
68     return res;
69 
70   AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(*memptr),
71                                 size);
72   return 0;
73 }
74 
75 // Note: we record the free before calling the backing implementation to make
76 // sure that the address is not reused before we've processed the deallocation
77 // (which includes assigning a sequence id to it).
wrap_free(uint32_t heap_id,void (* fn)(void *),void * pointer)78 void wrap_free(uint32_t heap_id, void (*fn)(void*), void* pointer) {
79   // free on a nullptr is valid but has no effect. Short circuit here, for
80   // various advantages:
81   // * More efficient
82   // * Notably printf calls free(nullptr) even when it is used in a way
83   //   malloc-free way, as it unconditionally frees the pointer even if
84   //   it was never written to.
85   //   Short circuiting here makes it less likely to accidentally build
86   //   infinite recursion.
87   if (pointer == nullptr)
88     return;
89 
90   AHeapProfile_reportFree(heap_id, reinterpret_cast<uint64_t>(pointer));
91   return fn(pointer);
92 }
93 
94 // As with the free, we record the deallocation before calling the backing
95 // implementation to make sure the address is still exclusive while we're
96 // processing it.
wrap_realloc(uint32_t heap_id,void * (* fn)(void *,size_t),void * pointer,size_t size)97 void* wrap_realloc(uint32_t heap_id,
98                    void* (*fn)(void*, size_t),
99                    void* pointer,
100                    size_t size) {
101   if (pointer)
102     AHeapProfile_reportFree(heap_id, reinterpret_cast<uint64_t>(pointer));
103   void* addr = fn(pointer, size);
104   AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
105                                 size);
106   return addr;
107 }
108 
wrap_pvalloc(uint32_t heap_id,void * (* fn)(size_t),size_t size)109 void* wrap_pvalloc(uint32_t heap_id, void* (*fn)(size_t), size_t size) {
110   void* addr = fn(size);
111   AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
112                                 RoundUpToSysPageSize(size));
113   return addr;
114 }
115 
wrap_valloc(uint32_t heap_id,void * (* fn)(size_t),size_t size)116 void* wrap_valloc(uint32_t heap_id, void* (*fn)(size_t), size_t size) {
117   void* addr = fn(size);
118   AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
119                                 size);
120   return addr;
121 }
122 
wrap_reallocarray(uint32_t heap_id,void * (* fn)(void *,size_t,size_t),void * pointer,size_t nmemb,size_t size)123 void* wrap_reallocarray(uint32_t heap_id,
124                         void* (*fn)(void*, size_t, size_t),
125                         void* pointer,
126                         size_t nmemb,
127                         size_t size) {
128   if (pointer)
129     AHeapProfile_reportFree(heap_id, reinterpret_cast<uint64_t>(pointer));
130   void* addr = fn(pointer, nmemb, size);
131   AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
132                                 nmemb * size);
133   return addr;
134 }
135 
136 }  // namespace profiling
137 }  // namespace perfetto
138