whitelisting localtime_r
[strongswan.git] / src / libstrongswan / utils / leak_detective.c
1 /*
2 * Copyright (C) 2006-2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include <stddef.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <malloc.h>
22 #include <signal.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <unistd.h>
27 #include <syslog.h>
28 #include <pthread.h>
29 #include <netdb.h>
30 #include <printf.h>
31 #include <locale.h>
32
33 #include "leak_detective.h"
34
35 #include <library.h>
36 #include <debug.h>
37 #include <utils/backtrace.h>
38
39 typedef struct private_leak_detective_t private_leak_detective_t;
40
41 /**
42 * private data of leak_detective
43 */
44 struct private_leak_detective_t {
45
46 /**
47 * public functions
48 */
49 leak_detective_t public;
50 };
51
52 /**
53 * Magic value which helps to detect memory corruption. Yummy!
54 */
55 #define MEMORY_HEADER_MAGIC 0x7ac0be11
56
57 /**
58 * Magic written to tail of allocation
59 */
60 #define MEMORY_TAIL_MAGIC 0xcafebabe
61
62 /**
63 * Pattern which is filled in memory before freeing it
64 */
65 #define MEMORY_FREE_PATTERN 0xFF
66
67 /**
68 * Pattern which is filled in newly allocated memory
69 */
70 #define MEMORY_ALLOC_PATTERN 0xEE
71
72
73 static void install_hooks(void);
74 static void uninstall_hooks(void);
75 static void *malloc_hook(size_t, const void *);
76 static void *realloc_hook(void *, size_t, const void *);
77 static void free_hook(void*, const void *);
78
79 void *(*old_malloc_hook)(size_t, const void *);
80 void *(*old_realloc_hook)(void *, size_t, const void *);
81 void (*old_free_hook)(void*, const void *);
82
83 static u_int count_malloc = 0;
84 static u_int count_free = 0;
85 static u_int count_realloc = 0;
86
87 typedef struct memory_header_t memory_header_t;
88 typedef struct memory_tail_t memory_tail_t;
89
90 /**
91 * Header which is prepended to each allocated memory block
92 */
93 struct memory_header_t {
94
95 /**
96 * Number of bytes following after the header
97 */
98 u_int bytes;
99
100 /**
101 * Pointer to previous entry in linked list
102 */
103 memory_header_t *previous;
104
105 /**
106 * Pointer to next entry in linked list
107 */
108 memory_header_t *next;
109
110 /**
111 * backtrace taken during (re-)allocation
112 */
113 backtrace_t *backtrace;
114
115 /**
116 * magic bytes to detect bad free or heap underflow, MEMORY_HEADER_MAGIC
117 */
118 u_int32_t magic;
119
120 }__attribute__((__packed__));
121
122 /**
123 * tail appended to each allocated memory block
124 */
125 struct memory_tail_t {
126
127 /**
128 * Magic bytes to detect heap overflow, MEMORY_TAIL_MAGIC
129 */
130 u_int32_t magic;
131
132 }__attribute__((__packed__));
133
134 /**
135 * first mem header is just a dummy to chain
136 * the others on it...
137 */
138 static memory_header_t first_header = {
139 magic: MEMORY_HEADER_MAGIC,
140 bytes: 0,
141 backtrace: NULL,
142 previous: NULL,
143 next: NULL
144 };
145
146 /**
147 * are the hooks currently installed?
148 */
149 static bool installed = FALSE;
150
151 /**
152 * Leak report white list
153 *
154 * List of functions using static allocation buffers or should be suppressed
155 * otherwise on leak report.
156 */
157 char *whitelist[] = {
158 /* backtraces, including own */
159 "backtrace_create",
160 /* pthread stuff */
161 "pthread_create",
162 "pthread_setspecific",
163 /* glibc functions */
164 "mktime",
165 "__gmtime_r",
166 "localtime_r",
167 "tzset",
168 "inet_ntoa",
169 "strerror",
170 "getprotobynumber",
171 "getservbyport",
172 "getservbyname",
173 "gethostbyname_r",
174 "gethostbyname2_r",
175 "getpwnam_r",
176 "getgrnam_r",
177 "register_printf_function",
178 "syslog",
179 "vsyslog",
180 "getaddrinfo",
181 "setlocale",
182 /* ignore dlopen, as we do not dlclose to get proper leak reports */
183 "dlopen",
184 "dlerror",
185 "dlclose",
186 /* mysql functions */
187 "mysql_init_character_set",
188 "init_client_errs",
189 "my_thread_init",
190 /* fastcgi library */
191 "FCGX_Init",
192 /* libxml */
193 "xmlInitCharEncodingHandlers",
194 "xmlInitParser",
195 "xmlInitParserCtxt",
196 /* ClearSilver */
197 "nerr_init",
198 /* OpenSSL */
199 "RSA_new_method",
200 "DH_new_method",
201 "ENGINE_load_builtin_engines",
202 };
203
204 /**
205 * check if a stack frame contains functions listed above
206 */
207 static bool is_whitelisted(backtrace_t *backtrace)
208 {
209 int i;
210 for (i = 0; i < sizeof(whitelist)/sizeof(char*); i++)
211 {
212 if (backtrace->contains_function(backtrace, whitelist[i]))
213 {
214 return TRUE;
215 }
216 }
217 return FALSE;
218 }
219
220 /**
221 * Report leaks at library destruction
222 */
223 void report_leaks()
224 {
225 memory_header_t *hdr;
226 int leaks = 0, whitelisted = 0;
227
228 for (hdr = first_header.next; hdr != NULL; hdr = hdr->next)
229 {
230 if (is_whitelisted(hdr->backtrace))
231 {
232 whitelisted++;
233 }
234 else
235 {
236 fprintf(stderr, "Leak (%d bytes at %p):\n", hdr->bytes, hdr + 1);
237 /* skip the first frame, contains leak detective logic */
238 hdr->backtrace->log(hdr->backtrace, stderr);
239 leaks++;
240 }
241 }
242
243 switch (leaks)
244 {
245 case 0:
246 fprintf(stderr, "No leaks detected");
247 break;
248 case 1:
249 fprintf(stderr, "One leak detected");
250 break;
251 default:
252 fprintf(stderr, "%d leaks detected", leaks);
253 break;
254 }
255 fprintf(stderr, ", %d suppressed by whitelist\n", whitelisted);
256 }
257
258 /**
259 * Installs the malloc hooks, enables leak detection
260 */
261 static void install_hooks()
262 {
263 if (!installed)
264 {
265 old_malloc_hook = __malloc_hook;
266 old_realloc_hook = __realloc_hook;
267 old_free_hook = __free_hook;
268 __malloc_hook = malloc_hook;
269 __realloc_hook = realloc_hook;
270 __free_hook = free_hook;
271 installed = TRUE;
272 }
273 }
274
275 /**
276 * Uninstalls the malloc hooks, disables leak detection
277 */
278 static void uninstall_hooks()
279 {
280 if (installed)
281 {
282 __malloc_hook = old_malloc_hook;
283 __free_hook = old_free_hook;
284 __realloc_hook = old_realloc_hook;
285 installed = FALSE;
286 }
287 }
288
289 /**
290 * Hook function for malloc()
291 */
292 void *malloc_hook(size_t bytes, const void *caller)
293 {
294 memory_header_t *hdr;
295 memory_tail_t *tail;
296 pthread_t thread_id = pthread_self();
297 int oldpolicy;
298 struct sched_param oldparams, params;
299
300 pthread_getschedparam(thread_id, &oldpolicy, &oldparams);
301
302 params.__sched_priority = sched_get_priority_max(SCHED_FIFO);
303 pthread_setschedparam(thread_id, SCHED_FIFO, &params);
304
305 count_malloc++;
306 uninstall_hooks();
307 hdr = malloc(sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
308 tail = ((void*)hdr) + bytes + sizeof(memory_header_t);
309 /* set to something which causes crashes */
310 memset(hdr, MEMORY_ALLOC_PATTERN,
311 sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
312
313 hdr->magic = MEMORY_HEADER_MAGIC;
314 hdr->bytes = bytes;
315 hdr->backtrace = backtrace_create(3);
316 tail->magic = MEMORY_TAIL_MAGIC;
317 install_hooks();
318
319 /* insert at the beginning of the list */
320 hdr->next = first_header.next;
321 if (hdr->next)
322 {
323 hdr->next->previous = hdr;
324 }
325 hdr->previous = &first_header;
326 first_header.next = hdr;
327
328 pthread_setschedparam(thread_id, oldpolicy, &oldparams);
329
330 return hdr + 1;
331 }
332
333 /**
334 * Hook function for free()
335 */
336 void free_hook(void *ptr, const void *caller)
337 {
338 memory_header_t *hdr;
339 memory_tail_t *tail;
340 backtrace_t *backtrace;
341 pthread_t thread_id = pthread_self();
342 int oldpolicy;
343 struct sched_param oldparams, params;
344
345 /* allow freeing of NULL */
346 if (ptr == NULL)
347 {
348 return;
349 }
350 hdr = ptr - sizeof(memory_header_t);
351 tail = ptr + hdr->bytes;
352
353 pthread_getschedparam(thread_id, &oldpolicy, &oldparams);
354
355 params.__sched_priority = sched_get_priority_max(SCHED_FIFO);
356 pthread_setschedparam(thread_id, SCHED_FIFO, &params);
357
358 count_free++;
359 uninstall_hooks();
360 if (hdr->magic != MEMORY_HEADER_MAGIC ||
361 tail->magic != MEMORY_TAIL_MAGIC)
362 {
363 fprintf(stderr, "freeing invalid memory (%p): "
364 "header magic 0x%x, tail magic 0x%x:\n",
365 ptr, hdr->magic, tail->magic);
366 backtrace = backtrace_create(3);
367 backtrace->log(backtrace, stderr);
368 backtrace->destroy(backtrace);
369 }
370 else
371 {
372 /* remove item from list */
373 if (hdr->next)
374 {
375 hdr->next->previous = hdr->previous;
376 }
377 hdr->previous->next = hdr->next;
378 hdr->backtrace->destroy(hdr->backtrace);
379
380 /* clear MAGIC, set mem to something remarkable */
381 memset(hdr, MEMORY_FREE_PATTERN, hdr->bytes + sizeof(memory_header_t));
382
383 free(hdr);
384 }
385
386 install_hooks();
387 pthread_setschedparam(thread_id, oldpolicy, &oldparams);
388 }
389
390 /**
391 * Hook function for realloc()
392 */
393 void *realloc_hook(void *old, size_t bytes, const void *caller)
394 {
395 memory_header_t *hdr;
396 memory_tail_t *tail;
397 backtrace_t *backtrace;
398 pthread_t thread_id = pthread_self();
399 int oldpolicy;
400 struct sched_param oldparams, params;
401
402 /* allow reallocation of NULL */
403 if (old == NULL)
404 {
405 return malloc_hook(bytes, caller);
406 }
407
408 hdr = old - sizeof(memory_header_t);
409 tail = old + hdr->bytes;
410
411 pthread_getschedparam(thread_id, &oldpolicy, &oldparams);
412
413 params.__sched_priority = sched_get_priority_max(SCHED_FIFO);
414 pthread_setschedparam(thread_id, SCHED_FIFO, &params);
415
416 count_realloc++;
417 uninstall_hooks();
418 if (hdr->magic != MEMORY_HEADER_MAGIC ||
419 tail->magic != MEMORY_TAIL_MAGIC)
420 {
421 fprintf(stderr, "reallocating invalid memory (%p): "
422 "header magic 0x%x, tail magic 0x%x:\n",
423 old, hdr->magic, tail->magic);
424 backtrace = backtrace_create(3);
425 backtrace->log(backtrace, stderr);
426 backtrace->destroy(backtrace);
427 }
428 /* clear tail magic, allocate, set tail magic */
429 memset(&tail->magic, MEMORY_ALLOC_PATTERN, sizeof(tail->magic));
430 hdr = realloc(hdr, sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
431 tail = ((void*)hdr) + bytes + sizeof(memory_header_t);
432 tail->magic = MEMORY_TAIL_MAGIC;
433
434 /* update statistics */
435 hdr->bytes = bytes;
436 hdr->backtrace->destroy(hdr->backtrace);
437 hdr->backtrace = backtrace_create(3);
438
439 /* update header of linked list neighbours */
440 if (hdr->next)
441 {
442 hdr->next->previous = hdr;
443 }
444 hdr->previous->next = hdr;
445 install_hooks();
446 pthread_setschedparam(thread_id, oldpolicy, &oldparams);
447 return hdr + 1;
448 }
449
450 /**
451 * Implementation of leak_detective_t.destroy
452 */
453 static void destroy(private_leak_detective_t *this)
454 {
455 if (installed)
456 {
457 uninstall_hooks();
458 report_leaks();
459 }
460 free(this);
461 }
462
463 /*
464 * see header file
465 */
466 leak_detective_t *leak_detective_create()
467 {
468 private_leak_detective_t *this = malloc_thing(private_leak_detective_t);
469
470 this->public.destroy = (void(*)(leak_detective_t*))destroy;
471
472 if (getenv("LEAK_DETECTIVE_DISABLE") == NULL)
473 {
474 lib->leak_detective = TRUE;
475 install_hooks();
476 }
477 return &this->public;
478 }
479