forked from sorokin/malloc-intercept
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc-intercept.cpp
More file actions
123 lines (91 loc) · 2.43 KB
/
malloc-intercept.cpp
File metadata and controls
123 lines (91 loc) · 2.43 KB
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
// compile (debug): g++ --shared -std=c++11 -fPIC -g -o malloc-intercept.so malloc-intercept.cpp internals.cpp tracing.cpp
// compile (release): g++ --shared -std=c++11 -fPIC -O2 -o malloc-intercept.so malloc-intercept.cpp internals.cpp tracing.cpp
// run (trace): LD_PRELOAD=./malloc-intercept.so kreversi
// run (no trace): LD_PRELOAD=./malloc-intercept.so MALLOC_INTERCEPT_NO_TRACE=1 kreversi
// view symbols: objdump -T --demangle malloc-intercept.so
#include <cerrno>
#include "internals.h"
#include "tracing.h"
using namespace malloc_intercept;
namespace
{
__thread bool inside_malloc = false;
struct recuirsion_guard
{
recuirsion_guard()
{
if (inside_malloc)
{
print("recursive call\n");
std::abort();
}
inside_malloc = true;
}
~recuirsion_guard()
{
inside_malloc = false;
}
private:
recuirsion_guard(recuirsion_guard const&);
recuirsion_guard& operator=(recuirsion_guard const&);
};
}
extern "C"
void* malloc(size_t size)
{
recuirsion_guard rg;
void *p = internal_alloc(size, DEFAULT_ALIGNMENT);
trace("malloc ", size, " ", p, "\n");
return p;
}
extern "C"
void* calloc(size_t n, size_t size)
{
recuirsion_guard rg;
void* p = internal_alloc(n * size, DEFAULT_ALIGNMENT);
trace("calloc ", n, " ", size, " ", p, "\n");
return p;
}
extern "C"
void free(void *ptr)
{
recuirsion_guard rg;
internal_free(ptr);
trace("free ", ptr, "\n");
}
extern "C"
void* realloc(void *ptr, size_t size)
{
recuirsion_guard rg;
void* p = internal_realloc(ptr, size);
trace("realloc ", ptr, " ", size, " ", p, "\n");
return p;
}
extern "C"
int posix_memalign(void** memptr, size_t alignment, size_t size)
{
recuirsion_guard rg;
*memptr = 0;
if (!is_valid_alignment(alignment))
return EINVAL;
void* p = internal_alloc(size, alignment);
trace("posix_memalign ", alignment, " ", size, " ", p, "\n");
if (p == 0)
return ENOMEM;
*memptr = p;
return 0;
}
extern "C"
void *valloc(size_t size)
{
recuirsion_guard rg;
print("deprecated function valloc is not supported\n");
std::abort();
}
extern "C"
void *memalign(size_t boundary, size_t size)
{
recuirsion_guard rg;
print("deprecated function memalign is not supported\n");
std::abort();
}