whitelisted another pthread_setspecific implementation
[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 "__pthread_setspecific",
164 /* glibc functions */
165 "mktime",
166 "__gmtime_r",
167 "localtime_r",
168 "tzset",
169 "inet_ntoa",
170 "strerror",
171 "getprotobynumber",
172 "getservbyport",
173 "getservbyname",
174 "gethostbyname_r",
175 "gethostbyname2_r",
176 "getpwnam_r",
177 "getgrnam_r",
178 "register_printf_function",
179 "syslog",
180 "vsyslog",
181 "getaddrinfo",
182 "setlocale",
183 /* ignore dlopen, as we do not dlclose to get proper leak reports */
184 "dlopen",
185 "dlerror",
186 "dlclose",
187 /* mysql functions */
188 "mysql_init_character_set",
189 "init_client_errs",
190 "my_thread_init",
191 /* fastcgi library */
192 "FCGX_Init",
193 /* libxml */
194 "xmlInitCharEncodingHandlers",
195 "xmlInitParser",
196 "xmlInitParserCtxt",
197 /* ClearSilver */
198 "nerr_init",
199 /* OpenSSL */
200 "RSA_new_method",
201 "DH_new_method",
202 "ENGINE_load_builtin_engines",
203 "OPENSSL_config",
204 };
205
206 /**
207 * check if a stack frame contains functions listed above
208 */
209 static bool is_whitelisted(backtrace_t *backtrace)
210 {
211 int i;
212 for (i = 0; i < sizeof(whitelist)/sizeof(char*); i++)
213 {
214 if (backtrace->contains_function(backtrace, whitelist[i]))
215 {
216 return TRUE;
217 }
218 }
219 return FALSE;
220 }
221
222 /**
223 * Report leaks at library destruction
224 */
225 void report_leaks()
226 {
227 memory_header_t *hdr;
228 int leaks = 0, whitelisted = 0;
229
230 for (hdr = first_header.next; hdr != NULL; hdr = hdr->next)
231 {
232 if (is_whitelisted(hdr->backtrace))
233 {
234 whitelisted++;
235 }
236 else
237 {
238 fprintf(stderr, "Leak (%d bytes at %p):\n", hdr->bytes, hdr + 1);
239 /* skip the first frame, contains leak detective logic */
240 hdr->backtrace->log(hdr->backtrace, stderr);
241 leaks++;
242 }
243 }
244
245 switch (leaks)
246 {
247 case 0:
248 fprintf(stderr, "No leaks detected");
249 break;
250 case 1:
251 fprintf(stderr, "One leak detected");
252 break;
253 default:
254 fprintf(stderr, "%d leaks detected", leaks);
255 break;
256 }
257 fprintf(stderr, ", %d suppressed by whitelist\n", whitelisted);
258 }
259
260 /**
261 * Installs the malloc hooks, enables leak detection
262 */
263 static void install_hooks()
264 {
265 if (!installed)
266 {
267 old_malloc_hook = __malloc_hook;
268 old_realloc_hook = __realloc_hook;
269 old_free_hook = __free_hook;
270 __malloc_hook = malloc_hook;
271 __realloc_hook = realloc_hook;
272 __free_hook = free_hook;
273 installed = TRUE;
274 }
275 }
276
277 /**
278 * Uninstalls the malloc hooks, disables leak detection
279 */
280 static void uninstall_hooks()
281 {
282 if (installed)
283 {
284 __malloc_hook = old_malloc_hook;
285 __free_hook = old_free_hook;
286 __realloc_hook = old_realloc_hook;
287 installed = FALSE;
288 }
289 }
290
291 /**
292 * Hook function for malloc()
293 */
294 void *malloc_hook(size_t bytes, const void *caller)
295 {
296 memory_header_t *hdr;
297 memory_tail_t *tail;
298 pthread_t thread_id = pthread_self();
299 int oldpolicy;
300 struct sched_param oldparams, params;
301
302 pthread_getschedparam(thread_id, &oldpolicy, &oldparams);
303
304 params.__sched_priority = sched_get_priority_max(SCHED_FIFO);
305 pthread_setschedparam(thread_id, SCHED_FIFO, &params);
306
307 count_malloc++;
308 uninstall_hooks();
309 hdr = malloc(sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
310 tail = ((void*)hdr) + bytes + sizeof(memory_header_t);
311 /* set to something which causes crashes */
312 memset(hdr, MEMORY_ALLOC_PATTERN,
313 sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
314
315 hdr->magic = MEMORY_HEADER_MAGIC;
316 hdr->bytes = bytes;
317 hdr->backtrace = backtrace_create(3);
318 tail->magic = MEMORY_TAIL_MAGIC;
319 install_hooks();
320
321 /* insert at the beginning of the list */
322 hdr->next = first_header.next;
323 if (hdr->next)
324 {
325 hdr->next->previous = hdr;
326 }
327 hdr->previous = &first_header;
328 first_header.next = hdr;
329
330 pthread_setschedparam(thread_id, oldpolicy, &oldparams);
331
332 return hdr + 1;
333 }
334
335 /**
336 * Hook function for free()
337 */
338 void free_hook(void *ptr, const void *caller)
339 {
340 memory_header_t *hdr;
341 memory_tail_t *tail;
342 backtrace_t *backtrace;
343 pthread_t thread_id = pthread_self();
344 int oldpolicy;
345 struct sched_param oldparams, params;
346
347 /* allow freeing of NULL */
348 if (ptr == NULL)
349 {
350 return;
351 }
352 hdr = ptr - sizeof(memory_header_t);
353 tail = ptr + hdr->bytes;
354
355 pthread_getschedparam(thread_id, &oldpolicy, &oldparams);
356
357 params.__sched_priority = sched_get_priority_max(SCHED_FIFO);
358 pthread_setschedparam(thread_id, SCHED_FIFO, &params);
359
360 count_free++;
361 uninstall_hooks();
362 if (hdr->magic != MEMORY_HEADER_MAGIC ||
363 tail->magic != MEMORY_TAIL_MAGIC)
364 {
365 fprintf(stderr, "freeing invalid memory (%p): "
366 "header magic 0x%x, tail magic 0x%x:\n",
367 ptr, hdr->magic, tail->magic);
368 backtrace = backtrace_create(3);
369 backtrace->log(backtrace, stderr);
370 backtrace->destroy(backtrace);
371 }
372 else
373 {
374 /* remove item from list */
375 if (hdr->next)
376 {
377 hdr->next->previous = hdr->previous;
378 }
379 hdr->previous->next = hdr->next;
380 hdr->backtrace->destroy(hdr->backtrace);
381
382 /* clear MAGIC, set mem to something remarkable */
383 memset(hdr, MEMORY_FREE_PATTERN, hdr->bytes + sizeof(memory_header_t));
384
385 free(hdr);
386 }
387
388 install_hooks();
389 pthread_setschedparam(thread_id, oldpolicy, &oldparams);
390 }
391
392 /**
393 * Hook function for realloc()
394 */
395 void *realloc_hook(void *old, size_t bytes, const void *caller)
396 {
397 memory_header_t *hdr;
398 memory_tail_t *tail;
399 backtrace_t *backtrace;
400 pthread_t thread_id = pthread_self();
401 int oldpolicy;
402 struct sched_param oldparams, params;
403
404 /* allow reallocation of NULL */
405 if (old == NULL)
406 {
407 return malloc_hook(bytes, caller);
408 }
409
410 hdr = old - sizeof(memory_header_t);
411 tail = old + hdr->bytes;
412
413 pthread_getschedparam(thread_id, &oldpolicy, &oldparams);
414
415 params.__sched_priority = sched_get_priority_max(SCHED_FIFO);
416 pthread_setschedparam(thread_id, SCHED_FIFO, &params);
417
418 count_realloc++;
419 uninstall_hooks();
420 if (hdr->magic != MEMORY_HEADER_MAGIC ||
421 tail->magic != MEMORY_TAIL_MAGIC)
422 {
423 fprintf(stderr, "reallocating invalid memory (%p): "
424 "header magic 0x%x, tail magic 0x%x:\n",
425 old, hdr->magic, tail->magic);
426 backtrace = backtrace_create(3);
427 backtrace->log(backtrace, stderr);
428 backtrace->destroy(backtrace);
429 }
430 /* clear tail magic, allocate, set tail magic */
431 memset(&tail->magic, MEMORY_ALLOC_PATTERN, sizeof(tail->magic));
432 hdr = realloc(hdr, sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
433 tail = ((void*)hdr) + bytes + sizeof(memory_header_t);
434 tail->magic = MEMORY_TAIL_MAGIC;
435
436 /* update statistics */
437 hdr->bytes = bytes;
438 hdr->backtrace->destroy(hdr->backtrace);
439 hdr->backtrace = backtrace_create(3);
440
441 /* update header of linked list neighbours */
442 if (hdr->next)
443 {
444 hdr->next->previous = hdr;
445 }
446 hdr->previous->next = hdr;
447 install_hooks();
448 pthread_setschedparam(thread_id, oldpolicy, &oldparams);
449 return hdr + 1;
450 }
451
452 /**
453 * Implementation of leak_detective_t.destroy
454 */
455 static void destroy(private_leak_detective_t *this)
456 {
457 if (installed)
458 {
459 uninstall_hooks();
460 report_leaks();
461 }
462 free(this);
463 }
464
465 /*
466 * see header file
467 */
468 leak_detective_t *leak_detective_create()
469 {
470 private_leak_detective_t *this = malloc_thing(private_leak_detective_t);
471
472 this->public.destroy = (void(*)(leak_detective_t*))destroy;
473
474 if (getenv("LEAK_DETECTIVE_DISABLE") == NULL)
475 {
476 lib->leak_detective = TRUE;
477 install_hooks();
478 }
479 return &this->public;
480 }
481