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 <stdlib.h>
18
19 #include <cinttypes>
20
21 #include "perfetto/ext/base/utils.h"
22 #include "perfetto/heap_profile.h"
23 #include "src/profiling/memory/wrap_allocators.h"
24
25 namespace perfetto {
26 namespace profiling {
27
28 namespace {
RoundUpToSysPageSize(size_t req_size)29 size_t RoundUpToSysPageSize(size_t req_size) {
30 const size_t page_size = base::GetSysPageSize();
31 return (req_size + page_size - 1) & ~(page_size - 1);
32 }
33 } // namespace
34
wrap_malloc(uint32_t heap_id,void * (* fn)(size_t),size_t size)35 void* wrap_malloc(uint32_t heap_id, void* (*fn)(size_t), size_t size) {
36 void* addr = fn(size);
37 AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
38 size);
39 return addr;
40 }
41
wrap_calloc(uint32_t heap_id,void * (* fn)(size_t,size_t),size_t nmemb,size_t size)42 void* wrap_calloc(uint32_t heap_id,
43 void* (*fn)(size_t, size_t),
44 size_t nmemb,
45 size_t size) {
46 void* addr = fn(nmemb, size);
47 AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
48 nmemb * size);
49 return addr;
50 }
51
wrap_memalign(uint32_t heap_id,void * (* fn)(size_t,size_t),size_t alignment,size_t size)52 void* wrap_memalign(uint32_t heap_id,
53 void* (*fn)(size_t, size_t),
54 size_t alignment,
55 size_t size) {
56 void* addr = fn(alignment, size);
57 AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
58 size);
59 return addr;
60 }
61
wrap_posix_memalign(uint32_t heap_id,int (* fn)(void **,size_t,size_t),void ** memptr,size_t alignment,size_t size)62 int wrap_posix_memalign(uint32_t heap_id,
63 int (*fn)(void**, size_t, size_t),
64 void** memptr,
65 size_t alignment,
66 size_t size) {
67 int res = fn(memptr, alignment, size);
68 if (res != 0)
69 return res;
70
71 AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(*memptr),
72 size);
73 return 0;
74 }
75
76 // Note: we record the free before calling the backing implementation to make
77 // sure that the address is not reused before we've processed the deallocation
78 // (which includes assigning a sequence id to it).
wrap_free(uint32_t heap_id,void (* fn)(void *),void * pointer)79 void wrap_free(uint32_t heap_id, void (*fn)(void*), void* pointer) {
80 // free on a nullptr is valid but has no effect. Short circuit here, for
81 // various advantages:
82 // * More efficient
83 // * Notably printf calls free(nullptr) even when it is used in a way
84 // malloc-free way, as it unconditionally frees the pointer even if
85 // it was never written to.
86 // Short circuiting here makes it less likely to accidentally build
87 // infinite recursion.
88 if (pointer == nullptr)
89 return;
90
91 AHeapProfile_reportFree(heap_id, reinterpret_cast<uint64_t>(pointer));
92 return fn(pointer);
93 }
94
95 // As with the free, we record the deallocation before calling the backing
96 // implementation to make sure the address is still exclusive while we're
97 // processing it.
wrap_realloc(uint32_t heap_id,void * (* fn)(void *,size_t),void * pointer,size_t size)98 void* wrap_realloc(uint32_t heap_id,
99 void* (*fn)(void*, size_t),
100 void* pointer,
101 size_t size) {
102 if (pointer)
103 AHeapProfile_reportFree(heap_id, reinterpret_cast<uint64_t>(pointer));
104 void* addr = fn(pointer, size);
105 AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
106 size);
107 return addr;
108 }
109
wrap_pvalloc(uint32_t heap_id,void * (* fn)(size_t),size_t size)110 void* wrap_pvalloc(uint32_t heap_id, void* (*fn)(size_t), size_t size) {
111 void* addr = fn(size);
112 AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
113 RoundUpToSysPageSize(size));
114 return addr;
115 }
116
wrap_valloc(uint32_t heap_id,void * (* fn)(size_t),size_t size)117 void* wrap_valloc(uint32_t heap_id, void* (*fn)(size_t), size_t size) {
118 void* addr = fn(size);
119 AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
120 size);
121 return addr;
122 }
123
wrap_reallocarray(uint32_t heap_id,void * (* fn)(void *,size_t,size_t),void * pointer,size_t nmemb,size_t size)124 void* wrap_reallocarray(uint32_t heap_id,
125 void* (*fn)(void*, size_t, size_t),
126 void* pointer,
127 size_t nmemb,
128 size_t size) {
129 if (pointer)
130 AHeapProfile_reportFree(heap_id, reinterpret_cast<uint64_t>(pointer));
131 void* addr = fn(pointer, nmemb, size);
132 AHeapProfile_reportAllocation(heap_id, reinterpret_cast<uint64_t>(addr),
133 nmemb * size);
134 return addr;
135 }
136
137 } // namespace profiling
138 } // namespace perfetto
139