forked from KDE/heaptrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaptrack_preload.cpp
284 lines (227 loc) · 5.82 KB
/
heaptrack_preload.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright 2014 Milian Wolff <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "libheaptrack.h"
#include <dlfcn.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <type_traits>
#include <atomic>
using namespace std;
#define HAVE_ALIGNED_ALLOC defined(_ISOC11_SOURCE)
namespace {
namespace hooks {
template<typename SignatureT, typename Base>
struct hook
{
using Signature = SignatureT*;
Signature original = nullptr;
void init() noexcept
{
auto ret = dlsym(RTLD_NEXT, Base::identifier);
if (!ret) {
fprintf(stderr, "Could not find original function %s\n", Base::identifier);
abort();
}
original = reinterpret_cast<Signature>(ret);
}
template<typename... Args>
auto operator() (Args... args) const noexcept -> decltype(original(args...))
{
return original(args...);
}
explicit operator bool () const noexcept
{
return original;
}
};
#define HOOK(name) struct name ## _t : public hook<decltype(::name), name ## _t> { \
static constexpr const char* identifier = #name; } name
HOOK(malloc);
HOOK(free);
HOOK(calloc);
HOOK(cfree);
HOOK(realloc);
HOOK(posix_memalign);
HOOK(valloc);
#if HAVE_ALIGNED_ALLOC
HOOK(aligned_alloc);
#endif
HOOK(dlopen);
HOOK(dlclose);
/**
* Dummy implementation, since the call to dlsym from findReal triggers a call to calloc.
*
* This is only called at startup and will eventually be replaced by the "proper" calloc implementation.
*/
void* dummy_calloc(size_t num, size_t size) noexcept
{
const size_t MAX_SIZE = 1024;
static char* buf[MAX_SIZE];
static size_t offset = 0;
if (!offset) {
memset(buf, 0, MAX_SIZE);
}
size_t oldOffset = offset;
offset += num * size;
if (offset >= MAX_SIZE) {
fprintf(stderr, "failed to initialize, dummy calloc buf size exhausted: %zu requested, %zu available\n", offset, MAX_SIZE);
abort();
}
return buf + oldOffset;
}
void init()
{
heaptrack_init(getenv("DUMP_HEAPTRACK_OUTPUT"), [] {
hooks::calloc.original = &dummy_calloc;
hooks::calloc.init();
hooks::dlopen.init();
hooks::dlclose.init();
hooks::malloc.init();
hooks::free.init();
hooks::calloc.init();
hooks::cfree.init();
hooks::realloc.init();
hooks::posix_memalign.init();
hooks::valloc.init();
#if HAVE_ALIGNED_ALLOC
hooks::aligned_alloc.init();
#endif
// cleanup environment to prevent tracing of child apps
unsetenv("LD_PRELOAD");
unsetenv("DUMP_HEAPTRACK_OUTPUT");
}, nullptr, nullptr);
}
}
}
extern "C" {
/// TODO: memalign, pvalloc, ...?
void* malloc(size_t size) noexcept
{
if (!hooks::malloc) {
hooks::init();
}
void* ptr = hooks::malloc(size);
heaptrack_malloc(ptr, size);
return ptr;
}
void free(void* ptr) noexcept
{
if (!hooks::free) {
hooks::init();
}
// call handler before handing over the real free implementation
// to ensure the ptr is not reused in-between and thus the output
// stays consistent
heaptrack_free(ptr);
hooks::free(ptr);
}
void* realloc(void* ptr, size_t size) noexcept
{
if (!hooks::realloc) {
hooks::init();
}
void* ret = hooks::realloc(ptr, size);
if (ret) {
heaptrack_realloc(ptr, size, ret);
}
return ret;
}
void* calloc(size_t num, size_t size) noexcept
{
if (!hooks::calloc) {
hooks::init();
}
void* ret = hooks::calloc(num, size);
if (ret) {
heaptrack_malloc(ret, num * size);
}
return ret;
}
void cfree(void* ptr) noexcept
{
if (!hooks::cfree) {
hooks::init();
}
// call handler before handing over the real free implementation
// to ensure the ptr is not reused in-between and thus the output
// stays consistent
if (ptr) {
heaptrack_free(ptr);
}
hooks::cfree(ptr);
}
int posix_memalign(void **memptr, size_t alignment, size_t size) noexcept
{
if (!hooks::posix_memalign) {
hooks::init();
}
int ret = hooks::posix_memalign(memptr, alignment, size);
if (!ret) {
heaptrack_malloc(*memptr, size);
}
return ret;
}
#if HAVE_ALIGNED_ALLOC
void* aligned_alloc(size_t alignment, size_t size) noexcept
{
if (!hooks::aligned_alloc) {
hooks::init();
}
void* ret = hooks::aligned_alloc(alignment, size);
if (ret) {
heaptrack_malloc(ret, size);
}
return ret;
}
#endif
void* valloc(size_t size) noexcept
{
if (!hooks::valloc) {
hooks::init();
}
void* ret = hooks::valloc(size);
if (ret) {
heaptrack_malloc(ret, size);
}
return ret;
}
void *dlopen(const char *filename, int flag) noexcept
{
if (!hooks::dlopen) {
hooks::init();
}
void* ret = hooks::dlopen(filename, flag);
if (ret) {
heaptrack_invalidate_module_cache();
}
return ret;
}
int dlclose(void *handle) noexcept
{
if (!hooks::dlclose) {
hooks::init();
}
int ret = hooks::dlclose(handle);
if (!ret) {
heaptrack_invalidate_module_cache();
}
return ret;
}
}