-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc_wrappers.c
More file actions
213 lines (167 loc) · 4.17 KB
/
malloc_wrappers.c
File metadata and controls
213 lines (167 loc) · 4.17 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
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
/*
*
* malloc_wrappers.c
*
* Author: Markku Rossi <[email protected]>
*
* Copyright (c) 2005-2016 Markku Rossi.
*
* See the LICENSE file for the details on licensing.
*
* Wrappers for system's memory allocation routines.
*
*/
#include "piincludes.h"
#include "pimalloc.h"
#include <dlfcn.h>
#include <signal.h>
/***************************** Global variables *****************************/
/* Allocation and free functions. These point to some functions that
are capable to allocate and release memory (malloc and free are
nice). */
extern void *(*pi_malloc_ptr)(size_t);
extern void (*pi_free_ptr)(void *);
/********************** Bootstrap allocation routines ***********************/
static unsigned char bootstrap_heap[100 * 1024];
static size_t bootstrap_allocated = 0;
/* Align `number' to `align'. The `number' and `align' must be
integer numbers. */
#define PI_ALIGN(number, align) \
((((number) + ((align) - 1)) / (align)) * (align))
static void *
bootstrap_malloc(size_t size)
{
void *ptr;
size = PI_ALIGN(size, 8);
if (bootstrap_allocated + size > sizeof(bootstrap_heap))
{
fprintf(stderr, "pimalloc: bootstrap heap out of space: size=%zu\n",
size);
return NULL;
}
ptr = bootstrap_heap + bootstrap_allocated;
bootstrap_allocated += size;
fprintf(stderr, "pimalloc: bootstrap_malloc(%zu) => %p\n", size, ptr);
return ptr;
}
static void
bootstrap_free(void *ptr)
{
fprintf(stderr, "pimalloc: bootstrap_free(%p)\n", ptr);
}
/************************** Interface to the libc ***************************/
static int initialized = 0;
static void *libc;
/*************** Wrapping system's memory allocation routines ***************/
static void init(void);
void *
malloc(size_t size)
{
if (!initialized)
init();
return pi_malloc(size);
}
void
free(void *ptr)
{
if (!initialized)
init();
if (ptr == (void *) 1)
{
pi_malloc_dump_statistics();
pi_malloc_dump_blocks();
return;
}
pi_free(ptr);
}
void *
realloc(void *ptr, size_t size)
{
if (!initialized)
init();
return pi_realloc(ptr, 0, size);
}
void *
calloc(size_t number, size_t size)
{
if (!initialized)
init();
return pi_calloc(number, size);
}
static void
dump_blocks(int sig)
{
pi_malloc_dump_statistics();
fprintf(stderr, "pimalloc: dumping blocks...\n");
pi_malloc_dump_blocks();
fprintf(stderr, "pimalloc: done\n");
}
static void
init(void)
{
char buf[256];
ssize_t ret;
const char *signal_path = "/tmp/mallocdebug";
int sig = -1;
int i;
int at_exit = 1;
char *endp;
initialized = 1;
pi_malloc_ptr = bootstrap_malloc;
pi_free_ptr = bootstrap_free;
fprintf(stderr, "pimalloc: init...\n");
libc = dlopen("libc.so.6", RTLD_LAZY | RTLD_GLOBAL);
if (libc == NULL)
libc = dlopen("libc.so", RTLD_LAZY | RTLD_GLOBAL);
if (libc == NULL)
{
perror("Could not open libc.so");
exit(1);
}
pi_malloc_ptr = dlsym(libc, "malloc");
if (pi_malloc_ptr == NULL)
{
perror("Could not fetch malloc");
exit(1);
}
pi_free_ptr = dlsym(libc, "free");
if (pi_free_ptr == NULL)
{
perror("Could not fetch free");
exit(1);
}
ret = readlink(signal_path, buf, sizeof(buf));
if (ret != -1)
{
for (i = 0; buf[i]; i++)
{
switch (buf[i])
{
case 'e':
at_exit = 0;
break;
default:
sig = strtol(buf + i, &endp, 10);
i = endp - buf - 1;
break;
}
}
if (sig >= 0 && signal(sig, dump_blocks) == SIG_ERR)
sig = -2;
fprintf(stderr,
"pimalloc: options: signal=%d, pid=%d, atexit=%s\n",
sig, getpid(), at_exit ? "true" : "false");
}
else
{
fprintf(stderr,
"pimalloc: no options defined (ln -s FLAGSSIGNAL %s)\n"
"pimalloc: FLAGS: e=no atexit dump\n"
"pimalloc: SIGNAL: dump signal (SIGUSR1=%d, SIGUSR2=%d)\n",
signal_path, SIGUSR1, SIGUSR2);
}
fprintf(stderr, "pimalloc: malloc=%p[%p], free=%p[%p]\n",
pi_malloc_ptr, malloc, pi_free_ptr, free);
if (at_exit)
atexit(pi_malloc_dump_blocks);
}