forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memleak.c
281 lines (225 loc) · 6.78 KB
/
memleak.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
#include <assert.h>
#include <backtrace.h>
#include <ccan/crypto/siphash24/siphash24.h>
#include <ccan/htable/htable.h>
#include <ccan/intmap/intmap.h>
#include <common/daemon.h>
#include <common/memleak.h>
#include <common/utils.h>
struct backtrace_state *backtrace_state;
#if DEVELOPER
static bool memleak_track;
struct memleak_notleak {
bool plus_children;
};
struct memleak_helper {
void (*cb)(struct htable *memtable, const tal_t *);
};
void *notleak_(const void *ptr, bool plus_children)
{
struct memleak_notleak *notleak;
/* If we're not tracking, don't do anything. */
if (!memleak_track)
return cast_const(void *, ptr);
notleak = tal(ptr, struct memleak_notleak);
notleak->plus_children = plus_children;
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);
}
static void children_into_htable(const void *exclude1, const void *exclude2,
struct htable *memtable, const tal_t *p)
{
const tal_t *i;
for (i = tal_first(p); i; i = tal_next(i)) {
const char *name = tal_name(i);
if (i == exclude1 || i == exclude2)
return;
if (name) {
/* Don't add backtrace objects. */
if (streq(name, "backtrace"))
continue;
/* Don't add our own memleak_helpers or notleak() */
if (strends(name, "struct memleak_helper")
|| strends(name, "struct memleak_notleak"))
continue;
/* Don't add tal_link objects */
if (strends(name, "struct link")
|| strends(name, "struct linkable"))
continue;
/* ccan/io allocates pollfd array, always array. */
if (strends(name, "struct pollfd[]") && !tal_parent(i))
continue;
if (strends(name, "struct io_plan *[]") && !tal_parent(i))
continue;
/* Other notleak internals. */
if (strends(name, "_notleak"))
continue;
/* Don't add tmpctx. */
if (streq(name, "tmpctx"))
continue;
}
htable_add(memtable, hash_ptr(i, NULL), i);
children_into_htable(exclude1, exclude2, memtable, i);
}
}
static void scan_for_pointers(struct htable *memtable,
const tal_t *p, size_t bytelen)
{
size_t i, n;
/* Search for (aligned) pointers. */
n = bytelen / sizeof(void *);
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, tal_bytelen(ptr));
}
}
void memleak_scan_region(struct htable *memtable,
const void *ptr, size_t bytelen)
{
pointer_referenced(memtable, ptr);
scan_for_pointers(memtable, ptr, bytelen);
}
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);
}
void memleak_remove_referenced(struct htable *memtable, const void *root)
{
/* Now delete the ones which are referenced. */
memleak_scan_region(memtable, root, tal_bytelen(root));
/* Remove memtable itself */
pointer_referenced(memtable, memtable);
}
/* memleak can't see inside hash tables, so do them manually */
void memleak_remove_htable(struct htable *memtable, const struct htable *ht)
{
struct htable_iter i;
const void *p;
for (p = htable_first(ht, &i); p; p = htable_next(ht, &i))
memleak_scan_region(memtable, p, tal_bytelen(p));
}
/* FIXME: If uintmap used tal, this wouldn't be necessary! */
void memleak_remove_intmap_(struct htable *memtable, const struct intmap *m)
{
void *p;
intmap_index_t i;
for (p = intmap_first_(m, &i); p; p = intmap_after_(m, &i))
memleak_scan_region(memtable, p, tal_bytelen(p));
}
static bool ptr_match(const void *candidate, void *ptr)
{
return candidate == ptr;
}
const void *memleak_get(struct htable *memtable, const uintptr_t **backtrace)
{
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);
/* 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;
}
return i;
}
static int append_bt(void *data, uintptr_t pc)
{
uintptr_t *bt = data;
if (bt[0] == 32)
return 1;
bt[bt[0]++] = pc;
return 0;
}
static void add_backtrace(tal_t *parent UNUSED, enum tal_notify_type type UNNEEDED,
void *child)
{
uintptr_t *bt = tal_arrz_label(child, uintptr_t, 32, "backtrace");
/* 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);
}
static void add_backtrace_notifiers(const tal_t *root)
{
tal_add_notifier(root, TAL_NOTIFY_ADD_CHILD, add_backtrace);
for (tal_t *i = tal_first(root); i; i = tal_next(i))
add_backtrace_notifiers(i);
}
void memleak_add_helper_(const tal_t *p,
void (*cb)(struct htable *memtable, const tal_t *))
{
struct memleak_helper *mh = tal(p, struct memleak_helper);
mh->cb = cb;
}
/* Handle allocations marked with helpers or notleak() */
static void call_memleak_helpers(struct htable *memtable, const tal_t *p)
{
const tal_t *i;
for (i = tal_first(p); i; i = tal_next(i)) {
const char *name = tal_name(i);
if (name && strends(name, "struct memleak_helper")) {
const struct memleak_helper *mh = i;
mh->cb(memtable, p);
} else if (name && strends(name, "struct memleak_notleak")) {
const struct memleak_notleak *notleak = i;
if (notleak->plus_children)
remove_with_children(memtable, p);
else
pointer_referenced(memtable, p);
memleak_scan_region(memtable, p, tal_bytelen(p));
} else {
call_memleak_helpers(memtable, i);
}
}
}
struct htable *memleak_enter_allocations(const tal_t *ctx,
const void *exclude1,
const void *exclude2)
{
struct htable *memtable = tal(ctx, struct htable);
htable_init(memtable, hash_ptr, NULL);
/* First, add all pointers off NULL to table. */
children_into_htable(exclude1, exclude2, memtable, NULL);
/* Iterate and call helpers to eliminate hard-to-get references. */
call_memleak_helpers(memtable, NULL);
tal_add_destructor(memtable, htable_clear);
return memtable;
}
void memleak_init(void)
{
memleak_track = true;
if (backtrace_state)
add_backtrace_notifiers(NULL);
}
#else /* !DEVELOPER */
void *notleak_(const void *ptr, bool plus_children UNNEEDED)
{
return cast_const(void *, ptr);
}
#endif /* !DEVELOPER */