botan: Add support for Ed25519 keys
[strongswan.git] / src / libstrongswan / utils / leak_detective.c
1 /*
2 * Copyright (C) 2013-2018 Tobias Brunner
3 * Copyright (C) 2006-2013 Martin Willi
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #define _GNU_SOURCE
18 #include <stddef.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #include <locale.h>
24 #ifdef HAVE_DLADDR
25 #include <dlfcn.h>
26 #endif
27 #include <time.h>
28 #include <errno.h>
29
30 #ifdef __APPLE__
31 #include <sys/mman.h>
32 #include <malloc/malloc.h>
33 /* overload some of our types clashing with mach */
34 #define host_t strongswan_host_t
35 #define processor_t strongswan_processor_t
36 #define thread_t strongswan_thread_t
37 #endif /* __APPLE__ */
38
39 #include "leak_detective.h"
40
41 #include <library.h>
42 #include <utils/utils.h>
43 #include <utils/debug.h>
44 #include <utils/backtrace.h>
45 #include <collections/hashtable.h>
46 #include <threading/thread_value.h>
47 #include <threading/spinlock.h>
48
49 typedef struct private_leak_detective_t private_leak_detective_t;
50
51 /**
52 * private data of leak_detective
53 */
54 struct private_leak_detective_t {
55
56 /**
57 * public functions
58 */
59 leak_detective_t public;
60
61 /**
62 * Registered report() function
63 */
64 leak_detective_report_cb_t report_cb;
65
66 /**
67 * Registered report() summary function
68 */
69 leak_detective_summary_cb_t report_scb;
70
71 /**
72 * Registered user data for callbacks
73 */
74 void *report_data;
75 };
76
77 /**
78 * Magic value which helps to detect memory corruption. Yummy!
79 */
80 #define MEMORY_HEADER_MAGIC 0x7ac0be11
81
82 /**
83 * Magic written to tail of allocation
84 */
85 #define MEMORY_TAIL_MAGIC 0xcafebabe
86
87 /**
88 * Pattern which is filled in memory before freeing it
89 */
90 #define MEMORY_FREE_PATTERN 0xFF
91
92 /**
93 * Pattern which is filled in newly allocated memory
94 */
95 #define MEMORY_ALLOC_PATTERN 0xEE
96
97 typedef struct memory_header_t memory_header_t;
98 typedef struct memory_tail_t memory_tail_t;
99
100 /**
101 * Header which is prepended to each allocated memory block
102 */
103 struct memory_header_t {
104
105 /**
106 * Pointer to previous entry in linked list
107 */
108 memory_header_t *previous;
109
110 /**
111 * Pointer to next entry in linked list
112 */
113 memory_header_t *next;
114
115 /**
116 * backtrace taken during (re-)allocation
117 */
118 backtrace_t *backtrace;
119
120 /**
121 * Padding to make sizeof(memory_header_t) == 32
122 */
123 uint32_t padding[sizeof(void*) == sizeof(uint32_t) ? 3 : 0];
124
125 /**
126 * Number of bytes following after the header
127 */
128 uint32_t bytes;
129
130 /**
131 * magic bytes to detect bad free or heap underflow, MEMORY_HEADER_MAGIC
132 */
133 uint32_t magic;
134
135 }__attribute__((__packed__));
136
137 /**
138 * tail appended to each allocated memory block
139 */
140 struct memory_tail_t {
141
142 /**
143 * Magic bytes to detect heap overflow, MEMORY_TAIL_MAGIC
144 */
145 uint32_t magic;
146
147 }__attribute__((__packed__));
148
149 /**
150 * first mem header is just a dummy to chain
151 * the others on it...
152 */
153 static memory_header_t first_header = {
154 .magic = MEMORY_HEADER_MAGIC,
155 };
156
157 /**
158 * Spinlock to access header linked list
159 */
160 static spinlock_t *lock;
161
162 /**
163 * Is leak detection currently enabled?
164 */
165 static bool enabled;
166
167 /**
168 * Whether to report calls to free() with memory not allocated by us
169 */
170 static bool ignore_unknown;
171
172 /**
173 * Is leak detection disabled for the current thread?
174 */
175 static thread_value_t *thread_disabled;
176
177 /**
178 * Installs the malloc hooks, enables leak detection
179 */
180 static void enable_leak_detective()
181 {
182 enabled = TRUE;
183 }
184
185 /**
186 * Uninstalls the malloc hooks, disables leak detection
187 */
188 static void disable_leak_detective()
189 {
190 enabled = FALSE;
191 }
192
193 /**
194 * Enable/Disable leak detective for the current thread
195 *
196 * @return Previous value
197 */
198 static bool enable_thread(bool enable)
199 {
200 bool before;
201
202 before = thread_disabled->get(thread_disabled) == NULL;
203 thread_disabled->set(thread_disabled, enable ? NULL : (void*)TRUE);
204 return before;
205 }
206
207 /**
208 * Add a header to the beginning of the list
209 */
210 static void add_hdr(memory_header_t *hdr)
211 {
212 lock->lock(lock);
213 hdr->next = first_header.next;
214 if (hdr->next)
215 {
216 hdr->next->previous = hdr;
217 }
218 hdr->previous = &first_header;
219 first_header.next = hdr;
220 lock->unlock(lock);
221 }
222
223 /**
224 * Remove a header from the list
225 */
226 static void remove_hdr(memory_header_t *hdr)
227 {
228 lock->lock(lock);
229 if (hdr->next)
230 {
231 hdr->next->previous = hdr->previous;
232 }
233 hdr->previous->next = hdr->next;
234 lock->unlock(lock);
235 }
236
237 /**
238 * Check if a header is in the list
239 */
240 static bool has_hdr(memory_header_t *hdr)
241 {
242 memory_header_t *current;
243 bool found = FALSE;
244
245 lock->lock(lock);
246 for (current = &first_header; current != NULL; current = current->next)
247 {
248 if (current == hdr)
249 {
250 found = TRUE;
251 break;
252 }
253 }
254 lock->unlock(lock);
255
256 return found;
257 }
258
259 #ifdef __APPLE__
260
261 /**
262 * Copy of original default zone, with functions we call in hooks
263 */
264 static malloc_zone_t original;
265
266 /**
267 * Call original malloc()
268 */
269 static void* real_malloc(size_t size)
270 {
271 return original.malloc(malloc_default_zone(), size);
272 }
273
274 /**
275 * Call original free()
276 */
277 static void real_free(void *ptr)
278 {
279 original.free(malloc_default_zone(), ptr);
280 }
281
282 /**
283 * Call original realloc()
284 */
285 static void* real_realloc(void *ptr, size_t size)
286 {
287 return original.realloc(malloc_default_zone(), ptr, size);
288 }
289
290 /**
291 * Hook definition: static function with _hook suffix, takes additional zone
292 */
293 #define HOOK(ret, name, ...) \
294 static ret name ## _hook(malloc_zone_t *_z, __VA_ARGS__)
295
296 /**
297 * forward declaration of hooks
298 */
299 HOOK(void*, malloc, size_t bytes);
300 HOOK(void*, calloc, size_t nmemb, size_t size);
301 HOOK(void*, valloc, size_t size);
302 HOOK(void, free, void *ptr);
303 HOOK(void*, realloc, void *old, size_t bytes);
304
305 /**
306 * malloc zone size(), must consider the memory header prepended
307 */
308 HOOK(size_t, size, const void *ptr)
309 {
310 bool before;
311 size_t size;
312
313 if (enabled)
314 {
315 before = enable_thread(FALSE);
316 if (before)
317 {
318 ptr -= sizeof(memory_header_t);
319 }
320 }
321 size = original.size(malloc_default_zone(), ptr);
322 if (enabled)
323 {
324 enable_thread(before);
325 }
326 return size;
327 }
328
329 /**
330 * Version of malloc zones we currently support
331 */
332 #define MALLOC_ZONE_VERSION 8 /* Snow Leopard */
333
334 /**
335 * Hook-in our malloc functions into the default zone
336 */
337 static bool register_hooks()
338 {
339 static bool once = FALSE;
340 malloc_zone_t *zone;
341 void *page;
342
343 if (once)
344 {
345 return TRUE;
346 }
347 once = TRUE;
348
349 zone = malloc_default_zone();
350 if (zone->version != MALLOC_ZONE_VERSION)
351 {
352 DBG1(DBG_CFG, "malloc zone version %d unsupported (requiring %d)",
353 zone->version, MALLOC_ZONE_VERSION);
354 return FALSE;
355 }
356
357 original = *zone;
358
359 page = (void*)((uintptr_t)zone / getpagesize() * getpagesize());
360 if (mprotect(page, getpagesize(), PROT_WRITE | PROT_READ) != 0)
361 {
362 DBG1(DBG_CFG, "malloc zone unprotection failed: %s", strerror(errno));
363 return FALSE;
364 }
365
366 zone->size = size_hook;
367 zone->malloc = malloc_hook;
368 zone->calloc = calloc_hook;
369 zone->valloc = valloc_hook;
370 zone->free = free_hook;
371 zone->realloc = realloc_hook;
372
373 /* those other functions can be NULLed out to not use them */
374 zone->batch_malloc = NULL;
375 zone->batch_free = NULL;
376 zone->memalign = NULL;
377 zone->free_definite_size = NULL;
378
379 return TRUE;
380 }
381
382 #else /* !__APPLE__ */
383
384 /**
385 * dlsym() might do a malloc(), but we can't do one before we get the malloc()
386 * function pointer. Use this minimalistic malloc implementation instead.
387 */
388 static void* malloc_for_dlsym(size_t size)
389 {
390 static char buf[1024] = {};
391 static size_t used = 0;
392 char *ptr;
393
394 /* roundup to a multiple of 32 */
395 size = (size - 1) / 32 * 32 + 32;
396
397 if (used + size > sizeof(buf))
398 {
399 return NULL;
400 }
401 ptr = buf + used;
402 used += size;
403 return ptr;
404 }
405
406 /**
407 * Lookup a malloc function, while disabling wrappers
408 */
409 static void* get_malloc_fn(char *name)
410 {
411 bool before = FALSE;
412 void *fn;
413
414 if (enabled)
415 {
416 before = enable_thread(FALSE);
417 }
418 fn = dlsym(RTLD_NEXT, name);
419 if (enabled)
420 {
421 enable_thread(before);
422 }
423 return fn;
424 }
425
426 /**
427 * Call original malloc()
428 */
429 static void* real_malloc(size_t size)
430 {
431 static void* (*fn)(size_t size);
432 static int recursive = 0;
433
434 if (!fn)
435 {
436 /* checking recursiveness should actually be thread-specific. But as
437 * it is very likely that the first allocation is done before we go
438 * multi-threaded, we keep it simple. */
439 if (recursive)
440 {
441 return malloc_for_dlsym(size);
442 }
443 recursive++;
444 fn = get_malloc_fn("malloc");
445 recursive--;
446 }
447 return fn(size);
448 }
449
450 /**
451 * Call original free()
452 */
453 static void real_free(void *ptr)
454 {
455 static void (*fn)(void *ptr);
456
457 if (!fn)
458 {
459 fn = get_malloc_fn("free");
460 }
461 return fn(ptr);
462 }
463
464 /**
465 * Call original realloc()
466 */
467 static void* real_realloc(void *ptr, size_t size)
468 {
469 static void* (*fn)(void *ptr, size_t size);
470
471 if (!fn)
472 {
473 fn = get_malloc_fn("realloc");
474 }
475 return fn(ptr, size);
476 }
477
478 /**
479 * Hook definition: plain function overloading existing malloc calls
480 */
481 #define HOOK(ret, name, ...) ret name(__VA_ARGS__)
482
483 /**
484 * Hook initialization when not using hooks, resolve functions.
485 */
486 static bool register_hooks()
487 {
488 void *buf = real_malloc(8);
489 buf = real_realloc(buf, 16);
490 real_free(buf);
491 return TRUE;
492 }
493
494 #endif /* !__APPLE__ */
495
496 /**
497 * Leak report white list
498 *
499 * List of functions using static allocation buffers or should be suppressed
500 * otherwise on leak report.
501 */
502 static char *whitelist[] = {
503 /* backtraces, including own */
504 "backtrace_create",
505 "strerror_safe",
506 /* pthread stuff */
507 "pthread_create",
508 "pthread_setspecific",
509 "__pthread_setspecific",
510 /* glibc functions */
511 "inet_ntoa",
512 "strerror",
513 "getprotobyname",
514 "getprotobynumber",
515 "getservbyport",
516 "getservbyname",
517 "gethostbyname",
518 "gethostbyname2",
519 "gethostbyname_r",
520 "gethostbyname2_r",
521 "getnetbyname",
522 "getpwnam_r",
523 "getgrnam_r",
524 "register_printf_function",
525 "register_printf_specifier",
526 "syslog",
527 "vsyslog",
528 "__syslog_chk",
529 "__vsyslog_chk",
530 "__fprintf_chk",
531 "getaddrinfo",
532 "setlocale",
533 "getpass",
534 "getpwent_r",
535 "setpwent",
536 "endpwent",
537 "getspnam_r",
538 "getpwuid_r",
539 "initgroups",
540 "tzset",
541 "_IO_file_doallocate",
542 /* ignore dlopen, as we do not dlclose to get proper leak reports */
543 "dlopen",
544 "dlerror",
545 "dlclose",
546 "dlsym",
547 /* mysql functions */
548 "mysql_init_character_set",
549 "init_client_errs",
550 "my_thread_init",
551 /* fastcgi library */
552 "FCGX_Init",
553 /* libxml */
554 "xmlInitCharEncodingHandlers",
555 "xmlInitParser",
556 "xmlInitParserCtxt",
557 /* libcurl */
558 "Curl_client_write",
559 /* libsoup */
560 "soup_message_headers_append",
561 "soup_message_headers_clear",
562 "soup_message_headers_get_list",
563 "soup_message_headers_get_one",
564 "soup_session_abort",
565 "soup_session_get_type",
566 /* libldap */
567 "ldap_int_initialize",
568 /* ClearSilver */
569 "nerr_init",
570 /* libgcrypt */
571 "gcrypt_plugin_create",
572 "gcry_control",
573 "gcry_check_version",
574 "gcry_randomize",
575 "gcry_create_nonce",
576 /* OpenSSL: These are needed for unit-tests only, the openssl plugin
577 * does properly clean up any memory during destroy(). */
578 "ECDSA_do_sign_ex",
579 "ECDSA_verify",
580 "RSA_new_method",
581 /* OpenSSL 1.1.0 does not cleanup anymore until the library is unloaded */
582 "OPENSSL_init_crypto",
583 "CRYPTO_THREAD_lock_new",
584 "ERR_add_error_data",
585 /* OpenSSL libssl */
586 "SSL_COMP_get_compression_methods",
587 /* NSPR */
588 "PR_CallOnce",
589 /* libapr */
590 "apr_pool_create_ex",
591 /* glib */
592 "g_output_stream_write",
593 "g_resolver_lookup_by_name",
594 "g_signal_connect_data",
595 "g_socket_connection_factory_lookup_type",
596 "g_type_init_with_debug_flags",
597 "g_type_register_static",
598 "g_type_class_ref",
599 "g_type_create_instance",
600 "g_type_add_interface_static",
601 "g_type_interface_add_prerequisite",
602 "g_private_set",
603 "g_queue_pop_tail",
604 /* libgpg */
605 "gpg_err_init",
606 /* gnutls */
607 "gnutls_global_init",
608 /* Ada runtime */
609 "system__tasking__initialize",
610 "system__tasking__initialization__abort_defer",
611 "system__tasking__stages__create_task",
612 /* in case external threads call into our code */
613 "thread_current_id",
614 /* FHH IMCs and IMVs */
615 "TNC_IMC_NotifyConnectionChange",
616 "TNC_IMV_NotifyConnectionChange",
617 /* Botan */
618 "botan_public_key_load",
619 "botan_privkey_create_ecdsa",
620 "botan_privkey_create_ecdh",
621 "botan_privkey_load_ecdh",
622 "botan_privkey_load",
623 };
624
625 /**
626 * Some functions are hard to whitelist, as they don't use a symbol directly.
627 * Use some static initialization to suppress them on leak reports
628 */
629 static void init_static_allocations()
630 {
631 struct tm tm;
632 time_t t = 0;
633
634 tzset();
635 gmtime_r(&t, &tm);
636 localtime_r(&t, &tm);
637 }
638
639 /**
640 * Hashtable hash function
641 */
642 static u_int hash(backtrace_t *key)
643 {
644 enumerator_t *enumerator;
645 void *addr;
646 u_int hash = 0;
647
648 enumerator = key->create_frame_enumerator(key);
649 while (enumerator->enumerate(enumerator, &addr))
650 {
651 hash = chunk_hash_inc(chunk_from_thing(addr), hash);
652 }
653 enumerator->destroy(enumerator);
654
655 return hash;
656 }
657
658 /**
659 * Hashtable equals function
660 */
661 static bool equals(backtrace_t *a, backtrace_t *b)
662 {
663 return a->equals(a, b);
664 }
665
666 /**
667 * Summarize and print backtraces
668 */
669 static int print_traces(private_leak_detective_t *this,
670 leak_detective_report_cb_t cb, void *user,
671 int thresh, int thresh_count,
672 bool detailed, int *whitelisted, size_t *sum)
673 {
674 int leaks = 0;
675 memory_header_t *hdr;
676 enumerator_t *enumerator;
677 hashtable_t *entries;
678 struct {
679 /** associated backtrace */
680 backtrace_t *backtrace;
681 /** total size of all allocations */
682 size_t bytes;
683 /** number of allocations */
684 u_int count;
685 } *entry;
686 bool before;
687
688 before = enable_thread(FALSE);
689
690 entries = hashtable_create((hashtable_hash_t)hash,
691 (hashtable_equals_t)equals, 1024);
692 lock->lock(lock);
693 for (hdr = first_header.next; hdr != NULL; hdr = hdr->next)
694 {
695 if (whitelisted &&
696 hdr->backtrace->contains_function(hdr->backtrace,
697 whitelist, countof(whitelist)))
698 {
699 (*whitelisted)++;
700 continue;
701 }
702 entry = entries->get(entries, hdr->backtrace);
703 if (entry)
704 {
705 entry->bytes += hdr->bytes;
706 entry->count++;
707 }
708 else
709 {
710 INIT(entry,
711 .backtrace = hdr->backtrace->clone(hdr->backtrace),
712 .bytes = hdr->bytes,
713 .count = 1,
714 );
715 entries->put(entries, entry->backtrace, entry);
716 }
717 if (sum)
718 {
719 *sum += hdr->bytes;
720 }
721 leaks++;
722 }
723 lock->unlock(lock);
724
725 enumerator = entries->create_enumerator(entries);
726 while (enumerator->enumerate(enumerator, NULL, &entry))
727 {
728 if (cb)
729 {
730 if (!thresh || entry->bytes >= thresh)
731 {
732 if (!thresh_count || entry->count >= thresh_count)
733 {
734 cb(user, entry->count, entry->bytes, entry->backtrace,
735 detailed);
736 }
737 }
738 }
739 entry->backtrace->destroy(entry->backtrace);
740 free(entry);
741 }
742 enumerator->destroy(enumerator);
743 entries->destroy(entries);
744
745 enable_thread(before);
746 return leaks;
747 }
748
749 METHOD(leak_detective_t, report, void,
750 private_leak_detective_t *this, bool detailed)
751 {
752 if (lib->leak_detective)
753 {
754 int leaks, whitelisted = 0;
755 size_t sum = 0;
756
757 leaks = print_traces(this, this->report_cb, this->report_data,
758 0, 0, detailed, &whitelisted, &sum);
759 if (this->report_scb)
760 {
761 this->report_scb(this->report_data, leaks, sum, whitelisted);
762 }
763 }
764 }
765
766 METHOD(leak_detective_t, set_report_cb, void,
767 private_leak_detective_t *this, leak_detective_report_cb_t cb,
768 leak_detective_summary_cb_t scb, void *user)
769 {
770 this->report_cb = cb;
771 this->report_scb = scb;
772 this->report_data = user;
773 }
774
775 METHOD(leak_detective_t, leaks, int,
776 private_leak_detective_t *this)
777 {
778 int whitelisted = 0;
779
780 return print_traces(this, NULL, NULL, 0, 0, FALSE, &whitelisted, NULL);
781 }
782
783 METHOD(leak_detective_t, set_state, bool,
784 private_leak_detective_t *this, bool enable)
785 {
786 return enable_thread(enable);
787 }
788
789 METHOD(leak_detective_t, usage, void,
790 private_leak_detective_t *this, leak_detective_report_cb_t cb,
791 leak_detective_summary_cb_t scb, void *user)
792 {
793 bool detailed;
794 int thresh, thresh_count, leaks, whitelisted = 0;
795 size_t sum = 0;
796
797 thresh = lib->settings->get_int(lib->settings,
798 "%s.leak_detective.usage_threshold", 10240, lib->ns);
799 thresh_count = lib->settings->get_int(lib->settings,
800 "%s.leak_detective.usage_threshold_count", 0, lib->ns);
801 detailed = lib->settings->get_bool(lib->settings,
802 "%s.leak_detective.detailed", TRUE, lib->ns);
803
804 leaks = print_traces(this, cb, user, thresh, thresh_count,
805 detailed, &whitelisted, &sum);
806 if (scb)
807 {
808 scb(user, leaks, sum, whitelisted);
809 }
810 }
811
812 /**
813 * Wrapped malloc() function
814 */
815 HOOK(void*, malloc, size_t bytes)
816 {
817 memory_header_t *hdr;
818 memory_tail_t *tail;
819 bool before;
820
821 if (!enabled || thread_disabled->get(thread_disabled))
822 {
823 return real_malloc(bytes);
824 }
825
826 hdr = real_malloc(sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
827 tail = ((void*)hdr) + bytes + sizeof(memory_header_t);
828 /* set to something which causes crashes */
829 memset(hdr, MEMORY_ALLOC_PATTERN,
830 sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
831
832 before = enable_thread(FALSE);
833 hdr->backtrace = backtrace_create(2);
834 enable_thread(before);
835
836 hdr->magic = MEMORY_HEADER_MAGIC;
837 hdr->bytes = bytes;
838 tail->magic = MEMORY_TAIL_MAGIC;
839
840 add_hdr(hdr);
841
842 return hdr + 1;
843 }
844
845 /**
846 * Wrapped calloc() function
847 */
848 HOOK(void*, calloc, size_t nmemb, size_t size)
849 {
850 void *ptr;
851 volatile size_t total;
852
853 total = nmemb * size;
854 ptr = malloc(total);
855 memset(ptr, 0, total);
856
857 return ptr;
858 }
859
860 /**
861 * Wrapped valloc(), TODO: currently not supported
862 */
863 HOOK(void*, valloc, size_t size)
864 {
865 DBG1(DBG_LIB, "valloc() used, but leak-detective hook missing");
866 return NULL;
867 }
868
869 /**
870 * Wrapped free() function
871 */
872 HOOK(void, free, void *ptr)
873 {
874 memory_header_t *hdr;
875 memory_tail_t *tail;
876 backtrace_t *backtrace;
877 bool before;
878
879 if (!enabled || thread_disabled->get(thread_disabled))
880 {
881 /* after deinitialization we might have to free stuff we allocated
882 * while we were enabled */
883 if (!first_header.magic && ptr)
884 {
885 hdr = ptr - sizeof(memory_header_t);
886 tail = ptr + hdr->bytes;
887 if (hdr->magic == MEMORY_HEADER_MAGIC &&
888 tail->magic == MEMORY_TAIL_MAGIC)
889 {
890 ptr = hdr;
891 }
892 }
893 real_free(ptr);
894 return;
895 }
896 /* allow freeing of NULL */
897 if (!ptr)
898 {
899 return;
900 }
901 hdr = ptr - sizeof(memory_header_t);
902 tail = ptr + hdr->bytes;
903
904 before = enable_thread(FALSE);
905 if (hdr->magic != MEMORY_HEADER_MAGIC ||
906 tail->magic != MEMORY_TAIL_MAGIC)
907 {
908 bool bt = TRUE;
909
910 /* check if memory appears to be allocated by our hooks */
911 if (has_hdr(hdr))
912 {
913 fprintf(stderr, "freeing corrupted memory (%p): "
914 "%u bytes, header magic 0x%x, tail magic 0x%x:\n",
915 ptr, hdr->bytes, hdr->magic, tail->magic);
916 remove_hdr(hdr);
917
918 if (hdr->magic == MEMORY_HEADER_MAGIC)
919 { /* only access the old backtrace if header magic is valid */
920 hdr->backtrace->log(hdr->backtrace, stderr, TRUE);
921 hdr->backtrace->destroy(hdr->backtrace);
922 }
923 else
924 {
925 fprintf(stderr, " header magic invalid, ignore backtrace of "
926 "allocation\n");
927 }
928 }
929 else
930 {
931 /* just free this block of unknown memory */
932 hdr = ptr;
933
934 if (ignore_unknown)
935 {
936 bt = FALSE;
937 }
938 else
939 {
940 fprintf(stderr, "freeing unknown memory (%p):\n", ptr);
941 }
942 }
943 if (bt)
944 {
945 backtrace = backtrace_create(2);
946 backtrace->log(backtrace, stderr, TRUE);
947 backtrace->destroy(backtrace);
948 }
949 }
950 else
951 {
952 remove_hdr(hdr);
953
954 hdr->backtrace->destroy(hdr->backtrace);
955
956 /* set mem to something remarkable */
957 memset(hdr, MEMORY_FREE_PATTERN,
958 sizeof(memory_header_t) + hdr->bytes + sizeof(memory_tail_t));
959 }
960 real_free(hdr);
961 enable_thread(before);
962 }
963
964 /**
965 * Wrapped realloc() function
966 */
967 HOOK(void*, realloc, void *old, size_t bytes)
968 {
969 memory_header_t *hdr;
970 memory_tail_t *tail;
971 backtrace_t *backtrace;
972 bool before, have_backtrace = TRUE;
973
974 if (!enabled || thread_disabled->get(thread_disabled))
975 {
976 return real_realloc(old, bytes);
977 }
978 /* allow reallocation of NULL */
979 if (!old)
980 {
981 return malloc(bytes);
982 }
983 /* handle zero size as a free() */
984 if (!bytes)
985 {
986 free(old);
987 return NULL;
988 }
989
990 hdr = old - sizeof(memory_header_t);
991 tail = old + hdr->bytes;
992
993 before = enable_thread(FALSE);
994 if (hdr->magic != MEMORY_HEADER_MAGIC ||
995 tail->magic != MEMORY_TAIL_MAGIC)
996 {
997 bool bt = TRUE;
998
999 /* check if memory appears to be allocated by our hooks */
1000 if (has_hdr(hdr))
1001 {
1002 fprintf(stderr, "reallocating corrupted memory (%p, %u bytes): "
1003 "%zu bytes, header magic 0x%x, tail magic 0x%x:\n",
1004 old, hdr->bytes, bytes, hdr->magic, tail->magic);
1005 remove_hdr(hdr);
1006
1007 if (hdr->magic == MEMORY_HEADER_MAGIC)
1008 { /* only access header fields (backtrace, bytes) if header magic
1009 * is still valid */
1010 hdr->backtrace->log(hdr->backtrace, stderr, TRUE);
1011 memset(&tail->magic, MEMORY_ALLOC_PATTERN, sizeof(tail->magic));
1012 }
1013 else
1014 {
1015 fprintf(stderr, " header magic invalid, ignore backtrace of "
1016 "allocation\n");
1017 have_backtrace = FALSE;
1018 hdr->magic = MEMORY_HEADER_MAGIC;
1019 }
1020 }
1021 else
1022 {
1023 /* adopt this block of unknown memory */
1024 hdr = old;
1025 have_backtrace = FALSE;
1026
1027 if (ignore_unknown)
1028 {
1029 bt = FALSE;
1030 }
1031 else
1032 {
1033 fprintf(stderr, "reallocating unknown memory (%p): %zu bytes:\n",
1034 old, bytes);
1035 }
1036 }
1037 if (bt)
1038 {
1039 backtrace = backtrace_create(2);
1040 backtrace->log(backtrace, stderr, TRUE);
1041 backtrace->destroy(backtrace);
1042 }
1043 }
1044 else
1045 {
1046 remove_hdr(hdr);
1047 /* clear tail magic, allocate, set tail magic */
1048 memset(&tail->magic, MEMORY_ALLOC_PATTERN, sizeof(tail->magic));
1049 }
1050
1051 hdr = real_realloc(hdr,
1052 sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
1053 tail = ((void*)hdr) + bytes + sizeof(memory_header_t);
1054 tail->magic = MEMORY_TAIL_MAGIC;
1055
1056 /* update statistics */
1057 hdr->bytes = bytes;
1058
1059 if (have_backtrace)
1060 {
1061 hdr->backtrace->destroy(hdr->backtrace);
1062 }
1063 hdr->backtrace = backtrace_create(2);
1064 enable_thread(before);
1065
1066 add_hdr(hdr);
1067
1068 return hdr + 1;
1069 }
1070
1071 METHOD(leak_detective_t, destroy, void,
1072 private_leak_detective_t *this)
1073 {
1074 disable_leak_detective();
1075 lock->destroy(lock);
1076 thread_disabled->destroy(thread_disabled);
1077 free(this);
1078 first_header.magic = 0;
1079 first_header.next = NULL;
1080 }
1081
1082 /*
1083 * see header file
1084 */
1085 leak_detective_t *leak_detective_create()
1086 {
1087 private_leak_detective_t *this;
1088
1089 INIT(this,
1090 .public = {
1091 .report = _report,
1092 .set_report_cb = _set_report_cb,
1093 .usage = _usage,
1094 .leaks = _leaks,
1095 .set_state = _set_state,
1096 .destroy = _destroy,
1097 },
1098 );
1099
1100 if (getenv("LEAK_DETECTIVE_DISABLE") != NULL)
1101 {
1102 free(this);
1103 return NULL;
1104 }
1105 ignore_unknown = getenv("LEAK_DETECTIVE_IGNORE_UNKNOWN") != NULL;
1106
1107 lock = spinlock_create();
1108 thread_disabled = thread_value_create(NULL);
1109
1110 init_static_allocations();
1111
1112 if (register_hooks())
1113 {
1114 enable_leak_detective();
1115 }
1116 return &this->public;
1117 }