-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc_detective.c
281 lines (239 loc) · 8.39 KB
/
malloc_detective.c
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
/*
* Malloc Detective: Core
*
* ---------------------------------------------------------------------------
* Copyright (c) 2015 Ayumu Koujiya
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <execinfo.h>
#include <errno.h>
static void* (*origin_malloc)(size_t);
static void (*origin_free)(void*);
static void* (*origin_libc_malloc)(size_t);
static void (*origin_libc_free)(void*);
static int malloc_wapper_logfd = -1;
static int is_out_free_bt = 0;
/* depth: Flag of reentrace */
static __thread int depth;
static const char* INNER_FLAG_LOGFD = "__LOGFD_FLAG_MALLOC_DETECTIVE";
static const char* MALLOC_DETECTIVE_OUTPUT = "MALLOC_DETECTIVE_OUTPUT";
static const char* MALLOC_DETECTIVE_CHILD = "MALLOC_DETECTIVE_CHILD";
static const char* MALLOC_DETECTIVE_FREE = "MALLOC_DETECTIVE_FREE";
#define MAX_BACKTRACE_SIZE 256
#define MAX_MESSAGE_SIZE PIPE_BUF
/* ----------------------------
* Initialize
* ---------------------------- */
static void load_malloc()
{
origin_malloc = (void*(*)(size_t)) dlsym(RTLD_NEXT, "malloc");
}
static void load_free()
{
origin_free = (void(*)(void*)) dlsym(RTLD_NEXT, "free");
}
static void load_libc_malloc()
{
origin_libc_malloc = (void*(*)(size_t)) dlsym(RTLD_NEXT, "__libc_malloc");
}
static void load_libc_free()
{
origin_libc_free = (void(*)(void*)) dlsym(RTLD_NEXT, "__libc_free");
}
/* Initializer of each process */
void __attribute__((constructor)) init_malloc_wrapper()
{
const char* malloc_detective_free = getenv(MALLOC_DETECTIVE_FREE);
is_out_free_bt = (malloc_detective_free && atoi(malloc_detective_free) == 1);
/* INNER_FLAG_UP: Logging file descriptor for child-processes. */
const char* inner_flag_logfd = getenv(INNER_FLAG_LOGFD);
char* output_name = NULL;
/* MALLOC_DETECTIVE_CHILD: Reuse flag of log stream by children. */
const char* malloc_detective_child = getenv(MALLOC_DETECTIVE_CHILD);
const int is_reuse = (malloc_detective_child && atoi(malloc_detective_child) == 1);
/* Get file discriptor for output log */
malloc_wapper_logfd = STDERR_FILENO;
if (inner_flag_logfd) {
/* Reuse the file descriptor that parent has generated.
* (or -1. Output suppressed by parents) */
malloc_wapper_logfd = atoi(inner_flag_logfd);
} else if ((output_name = getenv(MALLOC_DETECTIVE_OUTPUT)) != NULL) {
/* Deciding output stream, stderr or fifo.
* Using fifo if defined env-value MALLOC_DETECTIVE_OUTPUT. */
struct stat filestat;
if (stat(output_name, &filestat) == -1) {
/* Creating fifo, if not exists */
if (mkfifo(output_name, 0600) == -1) {
const char* errstr = strerror(errno);
fprintf(stderr, "Error: Can't create fifo(%s). mkfifo:%s\n", output_name, errstr);
_exit(2);
}
} else if (!S_ISFIFO(filestat.st_mode)) {
fprintf(stderr, "Error: %s is not fifo.\n", output_name);
_exit(3);
}
const mode_t openflag = O_WRONLY || (is_reuse ? 0 : O_CLOEXEC);
malloc_wapper_logfd = open(output_name, openflag);
if (malloc_wapper_logfd < 0) {
const char* errstr = strerror(errno);
fprintf(stderr, "Error: Can't open fifo(%s). open:%s\n", output_name, errstr);
_exit(1);
}
/* Save output stream if defined MALLOC_DETECTIVE_CHILD environment as 1.
* Otherwise save -1 to suppress output by children. */
if (is_reuse) {
char str_logfd[32] = {0};
sprintf(str_logfd, "%d", malloc_wapper_logfd);
setenv(INNER_FLAG_LOGFD, str_logfd, 1);
} else {
setenv(INNER_FLAG_LOGFD, "-1", 1);
}
}
/* load libc's malloc/free */
load_malloc();
load_free();
load_libc_malloc();
load_libc_free();
}
/* ----------------------------
* Output log string functions
* ---------------------------- */
static void log_output(
int is_trace,
int (*head_build)(char*,size_t,const void*,size_t),
void* addr, size_t size)
{
char msg[MAX_MESSAGE_SIZE];
struct timeval tv;
struct tm tm;
gettimeofday(&tv, NULL);
int msgsize = strftime(msg, sizeof(msg), "%Y-%m-%dT%H:%M:%S", localtime_r(&tv.tv_sec, &tm));
msgsize += sprintf(&msg[msgsize], ".%06d\t", (int)tv.tv_usec);
msgsize += head_build(&msg[msgsize], sizeof(msg) - msgsize, addr, size);
if (is_trace) {
void* trace[MAX_BACKTRACE_SIZE];
memset(trace, 0, sizeof(trace));
const int num_bt = backtrace(trace, MAX_BACKTRACE_SIZE);
char** const trace_str = backtrace_symbols(trace, MAX_BACKTRACE_SIZE);
int i;
for (i = 2/*remove this module*/; i < num_bt; ++i) {
const size_t btstrsize = strlen(trace_str[i]);
if (MAX_MESSAGE_SIZE < (btstrsize + msgsize)) {
break;
}
strncat(msg, "\t", 1);
strncat(msg, trace_str[i], btstrsize);
msgsize += btstrsize + 1;
}
if (trace_str) {
origin_free(trace_str);
}
}
strncat(msg, "\n", 1);
++msgsize;
write(malloc_wapper_logfd, msg, msgsize);
}
static int loghead_build_malloc(char* str, size_t str_size, const void* addr, size_t size)
{
return snprintf(str, str_size, "%d\tmalloc\t0x%08lx\t%zu", getpid(), (unsigned long)addr, size);
}
static int loghead_build_free(char* str, size_t str_size, const void* addr, size_t size)
{
return snprintf(str, str_size, "%d\tfree\t0x%08lx\t0", getpid(), (unsigned long)addr);
}
static int loghead_build_libc_malloc(char* str, size_t str_size, const void* addr, size_t size)
{
return snprintf(str, str_size, "%d\tmalloc\t0x%08lx\t%zu", getpid(), (unsigned long)addr, size);
}
static int loghead_build_libc_free(char* str, size_t str_size, const void* addr, size_t size)
{
return snprintf(str, str_size, "%d\tfree\t0x%08lx\t0", getpid(), (unsigned long)addr);
}
/* ----------------------------
* Wrappers
* ---------------------------- */
void* malloc(size_t size)
{
if (!origin_malloc) {
load_malloc();
}
void* result = origin_malloc(size);
if (depth > 0|| malloc_wapper_logfd == -1) {
return result;
}
++depth;
log_output(1, &loghead_build_malloc, result, size);
--depth;
return result;
}
void free(void* p)
{
if (!origin_free) {
load_free();
}
origin_free(p);
if (depth > 0|| malloc_wapper_logfd == -1) {
return;
}
++depth;
log_output(is_out_free_bt, &loghead_build_free, p, 0);
--depth;
}
void* __libc_malloc(size_t size)
{
if (!origin_libc_malloc) {
load_libc_malloc();
}
void* result = origin_libc_malloc(size);
if (depth > 0|| malloc_wapper_logfd == -1) {
return result;
}
++depth;
log_output(1, &loghead_build_libc_malloc, result, size);
--depth;
return result;
}
void __libc_free(void* p)
{
if (!origin_libc_free) {
load_libc_free();
}
origin_libc_free(p);
if (depth > 0|| malloc_wapper_logfd == -1) {
return;
}
++depth;
log_output(is_out_free_bt, &loghead_build_libc_free, p, 0);
--depth;
}
/* vim: ts=4 sts=4 sw=4 expandtab :
*/