2017-12-15 11:18:54 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <backtrace.h>
|
2017-12-15 11:17:54 +01:00
|
|
|
#include <ccan/crypto/siphash24/siphash24.h>
|
|
|
|
#include <ccan/htable/htable.h>
|
|
|
|
#include <common/memleak.h>
|
|
|
|
|
|
|
|
#if DEVELOPER
|
2017-12-15 11:18:54 +01:00
|
|
|
static struct backtrace_state *backtrace_state;
|
2017-12-15 11:17:54 +01:00
|
|
|
static const void **notleaks;
|
2017-12-15 11:22:40 +01:00
|
|
|
static bool *notleak_children;
|
2017-12-15 11:17:54 +01:00
|
|
|
|
2017-12-15 11:21:54 +01:00
|
|
|
static size_t find_notleak(const tal_t *ptr)
|
|
|
|
{
|
|
|
|
size_t i, nleaks = tal_count(notleaks);
|
|
|
|
|
|
|
|
for (i = 0; i < nleaks; i++)
|
|
|
|
if (notleaks[i] == ptr)
|
|
|
|
return i;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void notleak_change(tal_t *ctx,
|
|
|
|
enum tal_notify_type type,
|
|
|
|
void *info)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (type == TAL_NOTIFY_FREE) {
|
|
|
|
i = find_notleak(ctx);
|
|
|
|
memmove(notleaks + i, notleaks + i + 1,
|
|
|
|
sizeof(*notleaks) * (tal_count(notleaks) - i - 1));
|
2017-12-15 11:22:40 +01:00
|
|
|
memmove(notleak_children + i, notleak_children + i + 1,
|
|
|
|
sizeof(*notleak_children)
|
|
|
|
* (tal_count(notleak_children) - i - 1));
|
2017-12-15 11:21:54 +01:00
|
|
|
tal_resize(¬leaks, tal_count(notleaks) - 1);
|
2017-12-15 11:22:40 +01:00
|
|
|
tal_resize(¬leak_children, tal_count(notleak_children) - 1);
|
2017-12-15 11:21:54 +01:00
|
|
|
} else if (type == TAL_NOTIFY_MOVE) {
|
|
|
|
i = find_notleak(info);
|
|
|
|
notleaks[i] = ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:22:40 +01:00
|
|
|
void *notleak_(const void *ptr, bool plus_children)
|
2017-12-15 11:17:54 +01:00
|
|
|
{
|
|
|
|
size_t nleaks;
|
|
|
|
|
|
|
|
/* If we're not tracking, don't do anything. */
|
|
|
|
if (!notleaks)
|
|
|
|
return cast_const(void *, ptr);
|
|
|
|
|
|
|
|
nleaks = tal_count(notleaks);
|
|
|
|
tal_resize(¬leaks, nleaks+1);
|
2017-12-15 11:22:40 +01:00
|
|
|
tal_resize(¬leak_children, nleaks+1);
|
2017-12-15 11:17:54 +01:00
|
|
|
notleaks[nleaks] = ptr;
|
2017-12-15 11:22:40 +01:00
|
|
|
notleak_children[nleaks] = plus_children;
|
2017-12-15 11:21:54 +01:00
|
|
|
|
|
|
|
tal_add_notifier(ptr, TAL_NOTIFY_FREE|TAL_NOTIFY_MOVE, notleak_change);
|
2017-12-15 11:17:54 +01:00
|
|
|
return cast_const(void *, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t hash_ptr(const void *elem, void *unused UNNEEDED)
|
|
|
|
{
|
|
|
|
static struct siphash_seed seed;
|
|
|
|
return siphash24(&seed, &elem, sizeof(elem));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pointer_referenced(struct htable *memtable, const void *p)
|
|
|
|
{
|
|
|
|
return htable_del(memtable, hash_ptr(p, NULL), p);
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:20:54 +01:00
|
|
|
static void children_into_htable(const void *exclude1, const void *exclude2,
|
2017-12-15 11:17:54 +01:00
|
|
|
struct htable *memtable, const tal_t *p)
|
|
|
|
{
|
|
|
|
const tal_t *i;
|
|
|
|
|
|
|
|
for (i = tal_first(p); i; i = tal_next(i)) {
|
2017-12-15 11:19:54 +01:00
|
|
|
const char *name = tal_name(i);
|
|
|
|
|
2017-12-15 11:20:54 +01:00
|
|
|
if (p == exclude1 || p == exclude2)
|
2017-12-15 11:17:54 +01:00
|
|
|
continue;
|
2017-12-15 11:18:54 +01:00
|
|
|
|
2017-12-15 11:19:54 +01:00
|
|
|
if (name) {
|
|
|
|
/* Don't add backtrace objects. */
|
|
|
|
if (streq(name, "backtrace"))
|
|
|
|
continue;
|
2017-12-15 11:18:54 +01:00
|
|
|
|
2018-02-18 13:57:46 +01:00
|
|
|
/* Don't add tal_link objects */
|
|
|
|
if (strends(name, "struct link")
|
|
|
|
|| strends(name, "struct linkable"))
|
|
|
|
continue;
|
|
|
|
|
2017-12-15 11:19:54 +01:00
|
|
|
/* ccan/io allocates pollfd array. */
|
2018-08-02 08:49:55 +02:00
|
|
|
if (strends(name, "struct pollfd[]") && !tal_parent(i))
|
2017-12-15 11:19:54 +01:00
|
|
|
continue;
|
2018-03-15 05:30:38 +01:00
|
|
|
|
|
|
|
/* Don't add tmpctx. */
|
|
|
|
if (streq(name, "tmpctx"))
|
|
|
|
continue;
|
2017-12-15 11:19:54 +01:00
|
|
|
}
|
2017-12-15 11:17:54 +01:00
|
|
|
htable_add(memtable, hash_ptr(i, NULL), i);
|
2017-12-15 11:20:54 +01:00
|
|
|
children_into_htable(exclude1, exclude2, memtable, i);
|
2017-12-15 11:17:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:20:54 +01:00
|
|
|
struct htable *memleak_enter_allocations(const tal_t *ctx,
|
|
|
|
const void *exclude1,
|
|
|
|
const void *exclude2)
|
2017-12-15 11:17:54 +01:00
|
|
|
{
|
|
|
|
struct htable *memtable = tal(ctx, struct htable);
|
|
|
|
htable_init(memtable, hash_ptr, NULL);
|
|
|
|
|
|
|
|
/* First, add all pointers off NULL to table. */
|
2017-12-15 11:20:54 +01:00
|
|
|
children_into_htable(exclude1, exclude2, memtable, NULL);
|
2017-12-15 11:17:54 +01:00
|
|
|
|
|
|
|
tal_add_destructor(memtable, htable_clear);
|
|
|
|
return memtable;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scan_for_pointers(struct htable *memtable, const tal_t *p)
|
|
|
|
{
|
|
|
|
size_t i, n;
|
|
|
|
|
|
|
|
/* Search for (aligned) pointers. */
|
2018-07-28 08:00:16 +02:00
|
|
|
n = tal_bytelen(p) / sizeof(void *);
|
2017-12-15 11:17:54 +01:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
memcpy(&ptr, (char *)p + i * sizeof(void *), sizeof(ptr));
|
|
|
|
if (pointer_referenced(memtable, ptr))
|
|
|
|
scan_for_pointers(memtable, ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void memleak_scan_region(struct htable *memtable, const void *ptr)
|
|
|
|
{
|
|
|
|
pointer_referenced(memtable, ptr);
|
|
|
|
scan_for_pointers(memtable, ptr);
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:22:40 +01:00
|
|
|
static void remove_with_children(struct htable *memtable, const tal_t *p)
|
|
|
|
{
|
|
|
|
const tal_t *i;
|
|
|
|
|
|
|
|
pointer_referenced(memtable, p);
|
|
|
|
for (i = tal_first(p); i; i = tal_next(i))
|
|
|
|
remove_with_children(memtable, i);
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:17:54 +01:00
|
|
|
void memleak_remove_referenced(struct htable *memtable, const void *root)
|
|
|
|
{
|
2017-12-15 11:22:40 +01:00
|
|
|
size_t i;
|
|
|
|
|
2017-12-15 11:17:54 +01:00
|
|
|
/* Now delete the ones which are referenced. */
|
|
|
|
memleak_scan_region(memtable, root);
|
|
|
|
memleak_scan_region(memtable, notleaks);
|
|
|
|
|
2017-12-15 11:22:40 +01:00
|
|
|
/* Those who asked tal children to be removed, do so. */
|
|
|
|
for (i = 0; i < tal_count(notleaks); i++)
|
|
|
|
if (notleak_children[i])
|
|
|
|
remove_with_children(memtable, notleaks[i]);
|
2017-12-15 11:17:54 +01:00
|
|
|
|
2017-12-15 11:22:40 +01:00
|
|
|
/* notleak_children array is not a leak */
|
|
|
|
pointer_referenced(memtable, notleak_children);
|
2017-12-15 11:17:54 +01:00
|
|
|
|
2017-12-15 11:22:40 +01:00
|
|
|
/* Remove memtable itself */
|
|
|
|
pointer_referenced(memtable, memtable);
|
2017-12-15 11:17:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool ptr_match(const void *candidate, void *ptr)
|
|
|
|
{
|
|
|
|
return candidate == ptr;
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:18:54 +01:00
|
|
|
const void *memleak_get(struct htable *memtable, const uintptr_t **backtrace)
|
2017-12-15 11:17:54 +01:00
|
|
|
{
|
|
|
|
struct htable_iter it;
|
|
|
|
const tal_t *i, *p;
|
|
|
|
|
|
|
|
i = htable_first(memtable, &it);
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Delete from table (avoids parenting loops) */
|
|
|
|
htable_delval(memtable, &it);
|
|
|
|
|
|
|
|
/* Find ancestor, which is probably source of leak. */
|
|
|
|
for (p = tal_parent(i);
|
|
|
|
htable_get(memtable, hash_ptr(p, NULL), ptr_match, p);
|
|
|
|
i = p, p = tal_parent(i));
|
|
|
|
|
|
|
|
/* Delete all children */
|
|
|
|
remove_with_children(memtable, i);
|
|
|
|
|
2017-12-15 11:18:54 +01:00
|
|
|
/* Does it have a child called "backtrace"? */
|
|
|
|
for (*backtrace = tal_first(i);
|
|
|
|
*backtrace;
|
|
|
|
*backtrace = tal_next(*backtrace)) {
|
|
|
|
if (tal_name(*backtrace)
|
|
|
|
&& streq(tal_name(*backtrace), "backtrace"))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:17:54 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:18:54 +01:00
|
|
|
static int append_bt(void *data, uintptr_t pc)
|
2017-12-15 11:17:54 +01:00
|
|
|
{
|
2017-12-15 11:18:54 +01:00
|
|
|
uintptr_t *bt = data;
|
|
|
|
|
|
|
|
if (bt[0] == 32)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
bt[bt[0]++] = pc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-21 16:06:07 +01:00
|
|
|
static void add_backtrace(tal_t *parent UNUSED, enum tal_notify_type type UNNEEDED,
|
2017-12-15 11:18:54 +01:00
|
|
|
void *child)
|
|
|
|
{
|
2018-07-28 08:00:20 +02:00
|
|
|
uintptr_t *bt = tal_arrz_label(child, uintptr_t, 32, "backtrace");
|
2017-12-15 11:18:54 +01:00
|
|
|
|
|
|
|
/* First serves as counter. */
|
|
|
|
bt[0] = 1;
|
|
|
|
backtrace_simple(backtrace_state, 2, append_bt, NULL, bt);
|
|
|
|
tal_add_notifier(child, TAL_NOTIFY_ADD_CHILD, add_backtrace);
|
|
|
|
}
|
|
|
|
|
|
|
|
void memleak_init(const tal_t *root, struct backtrace_state *bstate)
|
|
|
|
{
|
|
|
|
assert(!notleaks);
|
|
|
|
backtrace_state = bstate;
|
2017-12-15 11:17:54 +01:00
|
|
|
notleaks = tal_arr(NULL, const void *, 0);
|
2017-12-15 11:22:40 +01:00
|
|
|
notleak_children = tal_arr(notleaks, bool, 0);
|
2017-12-15 11:18:54 +01:00
|
|
|
|
|
|
|
if (backtrace_state)
|
|
|
|
tal_add_notifier(root, TAL_NOTIFY_ADD_CHILD, add_backtrace);
|
2017-12-15 11:17:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void memleak_cleanup(void)
|
|
|
|
{
|
|
|
|
notleaks = tal_free(notleaks);
|
|
|
|
}
|
|
|
|
#endif /* DEVELOPER */
|