forked from archibate/mallocvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc_hook.cpp
444 lines (393 loc) · 11.6 KB
/
malloc_hook.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "plot_actions.hpp"
#include <cerrno>
#include <chrono>
#include <cstdlib>
#include <deque>
#include <mutex>
#include <new>
#if __unix__
# include <unistd.h>
#elif _WIN32
# include <windows.h>
#endif
#include "alloc_action.hpp"
namespace {
uint32_t get_thread_id() {
#if __unix__
return gettid();
#elif _WIN32
return GetCurrentThreadId();
#else
return 0;
#endif
}
struct alignas(64) PerThreadData {
std::recursive_mutex lock;
std::deque<AllocAction> actions;
bool enable = false;
};
struct GlobalData {
std::mutex lock;
static inline size_t const kPerThreadsCount = 32;
PerThreadData per_threads[kPerThreadsCount];
GlobalData() {
for (size_t i = 0; i < kPerThreadsCount; ++i) {
per_threads[i].enable = true;
}
}
~GlobalData() {
for (size_t i = 0; i < kPerThreadsCount; ++i) {
per_threads[i].enable = false;
}
std::deque<AllocAction> actions;
for (size_t i = 0; i < kPerThreadsCount; ++i) {
auto &their_actions = per_threads[i].actions;
actions.insert(actions.end(), their_actions.begin(),
their_actions.end());
}
plot_alloc_actions(actions);
}
} global;
struct EnableGuard {
uint32_t tid;
bool was_enable;
PerThreadData &per_thread;
EnableGuard()
: tid(get_thread_id()),
per_thread(global.per_threads[((size_t)tid * 17) %
global.kPerThreadsCount]) {
per_thread.lock.lock();
was_enable = per_thread.enable;
per_thread.enable = false;
}
explicit operator bool() const {
return was_enable;
}
void on(AllocOp op, void *ptr, size_t size, size_t align,
void *caller) const {
if (ptr) {
auto now = std::chrono::high_resolution_clock::now();
int64_t time =
std::chrono::duration_cast<std::chrono::nanoseconds>(
now.time_since_epoch())
.count();
per_thread.actions.push_back(
AllocAction{op, tid, ptr, size, align, caller, time});
}
}
~EnableGuard() {
per_thread.enable = was_enable;
per_thread.lock.unlock();
}
};
} // namespace
#if __GNUC__
extern "C" void *__libc_malloc(size_t size) noexcept;
extern "C" void __libc_free(void *ptr) noexcept;
extern "C" void *__libc_calloc(size_t nmemb, size_t size) noexcept;
extern "C" void *__libc_realloc(void *ptr, size_t size) noexcept;
extern "C" void *__libc_reallocarray(void *ptr, size_t nmemb,
size_t size) noexcept;
extern "C" void *__libc_valloc(size_t size) noexcept;
extern "C" void *__libc_memalign(size_t align, size_t size) noexcept;
# define REAL_LIBC(name) __libc_##name
# define MAY_OVERRIDE_MALLOC 1
# define MAY_SUPPORT_MEMALIGN 1
# define RETURN_ADDRESS __builtin_return_address(0)
#elif _MSC_VER
static void *msvc_malloc(size_t size) noexcept {
return HeapAlloc(GetProcessHeap(), 0, size);
}
static void *msvc_calloc(size_t nmemb, size_t size) noexcept {
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nmemb * size);
}
static void msvc_free(void *ptr) noexcept {
HeapFree(GetProcessHeap(), 0, ptr);
}
static void *msvc_realloc(void *ptr, size_t size) noexcept {
return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
}
static void *msvc_reallocarray(void *ptr, size_t nmemb, size_t size) noexcept {
return msvc_realloc(ptr, nmemb * size);
}
# define REAL_LIBC(name) msvc_##name
# define MAY_OVERRIDE_MALLOC 1
# define MAY_SUPPORT_MEMALIGN 0
# include <intrin.h>
# pragma intrinsic(_ReturnAddress)
# define RETURN_ADDRESS _ReturnAddress()
#else
# define REAL_LIBC(name) name
# define MAY_OVERRIDE_MALLOC 0
# define MAY_SUPPORT_MEMALIGN 0
# define RETURN_ADDRESS ((void *)1)
#endif
#if MAY_OVERRIDE_MALLOC
extern "C" void *malloc(size_t size) noexcept {
EnableGuard ena;
void *ptr = REAL_LIBC(malloc)(size);
if (ena) {
ena.on(AllocOp::Malloc, ptr, size, kNone, RETURN_ADDRESS);
}
return ptr;
}
extern "C" void free(void *ptr) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::Free, ptr, kNone, kNone, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
extern "C" void *calloc(size_t nmemb, size_t size) noexcept {
EnableGuard ena;
void *ptr = REAL_LIBC(calloc)(nmemb, size);
if (ena) {
ena.on(AllocOp::Malloc, ptr, nmemb * size, kNone, RETURN_ADDRESS);
}
return ptr;
}
extern "C" void *realloc(void *ptr, size_t size) noexcept {
EnableGuard ena;
void *new_ptr = REAL_LIBC(realloc)(ptr, size);
if (ena) {
ena.on(AllocOp::Malloc, new_ptr, size, kNone, RETURN_ADDRESS);
if (new_ptr) {
ena.on(AllocOp::Free, ptr, kNone, kNone, RETURN_ADDRESS);
}
}
return new_ptr;
}
extern "C" void *reallocarray(void *ptr, size_t nmemb, size_t size) noexcept {
EnableGuard ena;
void *new_ptr = REAL_LIBC(reallocarray)(ptr, nmemb, size);
if (ena) {
ena.on(AllocOp::Malloc, new_ptr, nmemb * size, kNone, RETURN_ADDRESS);
if (new_ptr) {
ena.on(AllocOp::Free, ptr, kNone, kNone, RETURN_ADDRESS);
}
}
return new_ptr;
}
# if MAY_SUPPORT_MEMALIGN
extern "C" void *valloc(size_t size) noexcept {
EnableGuard ena;
void *ptr = REAL_LIBC(valloc)(size);
if (ena) {
# if __unix__
size_t pagesize = sysconf(_SC_PAGESIZE);
# elif _WIN32
SYSTEM_INFO info;
info.dwPageSize = kNone;
GetSystemInfo(&info);
size_t pagesize = info.dwPageSize;
# else
size_t pagesize = 0;
# endif
ena.on(AllocOp::Malloc, ptr, size, pagesize, RETURN_ADDRESS);
}
return ptr;
}
extern "C" void *memalign(size_t align, size_t size) noexcept {
EnableGuard ena;
void *ptr = REAL_LIBC(memalign)(align, size);
if (ena) {
ena.on(AllocOp::Malloc, ptr, size, align, RETURN_ADDRESS);
}
return ptr;
}
extern "C" void *aligned_alloc(size_t align, size_t size) noexcept {
EnableGuard ena;
void *ptr = REAL_LIBC(memalign)(align, size);
if (ena) {
ena.on(AllocOp::Malloc, ptr, size, align, RETURN_ADDRESS);
}
return ptr;
}
extern "C" int posix_memalign(void **memptr, size_t align,
size_t size) noexcept {
EnableGuard ena;
void *ptr = REAL_LIBC(memalign)(align, size);
if (ena) {
ena.on(AllocOp::Malloc, *memptr, size, align, RETURN_ADDRESS);
}
int ret = 0;
if (!ptr) {
ret = errno;
} else {
*memptr = ptr;
}
return ret;
}
# endif
#endif
void operator delete(void *ptr) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::Delete, ptr, kNone, kNone, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void operator delete[](void *ptr) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::DeleteArray, ptr, kNone, kNone, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void operator delete(void *ptr, std::nothrow_t const &) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::Delete, ptr, kNone, kNone, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void operator delete[](void *ptr, std::nothrow_t const &) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::DeleteArray, ptr, kNone, kNone, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void *operator new(size_t size) noexcept(false) {
EnableGuard ena;
void *ptr = REAL_LIBC(malloc)(size);
if (ena) {
ena.on(AllocOp::New, ptr, size, kNone, RETURN_ADDRESS);
}
if (ptr == nullptr) {
throw std::bad_alloc();
}
return ptr;
}
void *operator new[](size_t size) noexcept(false) {
EnableGuard ena;
void *ptr = REAL_LIBC(malloc)(size);
if (ena) {
ena.on(AllocOp::NewArray, ptr, size, kNone, RETURN_ADDRESS);
}
if (ptr == nullptr) {
throw std::bad_alloc();
}
return ptr;
}
void *operator new(size_t size, std::nothrow_t const &) noexcept {
EnableGuard ena;
void *ptr = REAL_LIBC(malloc)(size);
if (ena) {
ena.on(AllocOp::New, ptr, size, kNone, RETURN_ADDRESS);
}
return ptr;
}
void *operator new[](size_t size, std::nothrow_t const &) noexcept {
EnableGuard ena;
void *ptr = REAL_LIBC(malloc)(size);
if (ena) {
ena.on(AllocOp::NewArray, ptr, size, kNone, RETURN_ADDRESS);
}
return ptr;
}
#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
void operator delete(void *ptr, size_t size) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::Delete, ptr, size, kNone, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void operator delete[](void *ptr, size_t size) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::DeleteArray, ptr, size, kNone, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
#endif
#if (__cplusplus > 201402L || defined(__cpp_aligned_new))
# if MAY_SUPPORT_MEMALIGN
void operator delete(void *ptr, std::align_val_t align) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::Delete, ptr, kNone, (size_t)align, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void operator delete[](void *ptr, std::align_val_t align) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::DeleteArray, ptr, kNone, (size_t)align, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void operator delete(void *ptr, size_t size, std::align_val_t align) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::Delete, ptr, size, (size_t)align, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void operator delete[](void *ptr, size_t size,
std::align_val_t align) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::DeleteArray, ptr, size, (size_t)align, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void operator delete(void *ptr, std::align_val_t align,
std::nothrow_t const &) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::Delete, ptr, kNone, (size_t)align, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void operator delete[](void *ptr, std::align_val_t align,
std::nothrow_t const &) noexcept {
EnableGuard ena;
if (ena) {
ena.on(AllocOp::DeleteArray, ptr, kNone, (size_t)align, RETURN_ADDRESS);
}
REAL_LIBC(free)(ptr);
}
void *operator new(size_t size, std::align_val_t align) noexcept(false) {
EnableGuard ena;
void *ptr = REAL_LIBC(memalign)((size_t)align, size);
if (ena) {
ena.on(AllocOp::New, ptr, size, (size_t)align, RETURN_ADDRESS);
}
if (ptr == nullptr) {
throw std::bad_alloc();
}
return ptr;
}
void *operator new[](size_t size, std::align_val_t align) noexcept(false) {
EnableGuard ena;
void *ptr = REAL_LIBC(memalign)((size_t)align, size);
if (ena) {
ena.on(AllocOp::NewArray, ptr, size, (size_t)align, RETURN_ADDRESS);
}
if (ptr == nullptr) {
throw std::bad_alloc();
}
return ptr;
}
void *operator new(size_t size, std::align_val_t align,
std::nothrow_t const &) noexcept {
EnableGuard ena;
void *ptr = REAL_LIBC(memalign)((size_t)align, size);
if (ena) {
ena.on(AllocOp::New, ptr, size, (size_t)align, RETURN_ADDRESS);
}
return ptr;
}
void *operator new[](size_t size, std::align_val_t align,
std::nothrow_t const &) noexcept {
EnableGuard ena;
void *ptr = REAL_LIBC(memalign)((size_t)align, size);
if (ena) {
ena.on(AllocOp::NewArray, ptr, size, (size_t)align, RETURN_ADDRESS);
}
return ptr;
}
# endif
#endif