1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #include <ccan/tal/tal.h>
3 #include <ccan/compiler/compiler.h>
4 #include <ccan/list/list.h>
5 #include <ccan/alignof/alignof.h>
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stddef.h>
9 #include <string.h>
10 #include <limits.h>
11 #include <stdint.h>
12 #include <errno.h>
13
14 //#define TAL_DEBUG 1
15
16 #define NOTIFY_IS_DESTRUCTOR 512
17 #define NOTIFY_EXTRA_ARG 1024
18
19 /* This makes our parent_child ptr stand out for to_tal_hdr checks */
20 #define TAL_PTR_OBFUSTICATOR ((intptr_t)0x1984200820142016ULL)
21
22 /* 32-bit type field, first byte 0 in either endianness. */
23 enum prop_type {
24 CHILDREN = 0x00c1d500,
25 NAME = 0x00111100,
26 NOTIFIER = 0x00071f00,
27 };
28
29 struct tal_hdr {
30 struct list_node list;
31 struct prop_hdr *prop;
32 /* XOR with TAL_PTR_OBFUSTICATOR */
33 intptr_t parent_child;
34 size_t bytelen;
35 };
36
37 struct prop_hdr {
38 enum prop_type type;
39 struct prop_hdr *next;
40 };
41
42 struct children {
43 struct prop_hdr hdr; /* CHILDREN */
44 struct tal_hdr *parent;
45 struct list_head children; /* Head of siblings. */
46 };
47
48 struct name {
49 struct prop_hdr hdr; /* NAME */
50 char name[];
51 };
52
53 struct notifier {
54 struct prop_hdr hdr; /* NOTIFIER */
55 enum tal_notify_type types;
56 union notifier_cb {
57 void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
58 void (*destroy)(tal_t *); /* If NOTIFY_IS_DESTRUCTOR set */
59 void (*destroy2)(tal_t *, void *); /* If NOTIFY_EXTRA_ARG */
60 } u;
61 };
62
63 /* Extra arg */
64 struct notifier_extra_arg {
65 struct notifier n;
66 void *arg;
67 };
68
69 #define EXTRA_ARG(n) (((struct notifier_extra_arg *)(n))->arg)
70
71 static struct {
72 struct tal_hdr hdr;
73 struct children c;
74 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
75 &null_parent.c.hdr, TAL_PTR_OBFUSTICATOR, 0 },
76 { { CHILDREN, NULL },
77 &null_parent.hdr,
78 { { &null_parent.c.children.n,
79 &null_parent.c.children.n } }
80 }
81 };
82
83
84 static void *(*allocfn)(size_t size) = malloc;
85 static void *(*resizefn)(void *, size_t size) = realloc;
86 static void (*freefn)(void *) = free;
87 static void (*errorfn)(const char *msg) = (void *)abort;
88 /* Count on non-destrutor notifiers; often stays zero. */
89 static size_t notifiers = 0;
90
call_error(const char * msg)91 static inline void COLD call_error(const char *msg)
92 {
93 errorfn(msg);
94 }
95
get_destroying_bit(intptr_t parent_child)96 static bool get_destroying_bit(intptr_t parent_child)
97 {
98 return parent_child & 1;
99 }
100
set_destroying_bit(intptr_t * parent_child)101 static void set_destroying_bit(intptr_t *parent_child)
102 {
103 *parent_child |= 1;
104 }
105
ignore_destroying_bit(intptr_t parent_child)106 static struct children *ignore_destroying_bit(intptr_t parent_child)
107 {
108 return (void *)((parent_child ^ TAL_PTR_OBFUSTICATOR) & ~(intptr_t)1);
109 }
110
111 /* This means valgrind can see leaks. */
tal_cleanup(void)112 void tal_cleanup(void)
113 {
114 struct tal_hdr *i;
115
116 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list))) {
117 list_del(&i->list);
118 memset(i, 0, sizeof(*i));
119 }
120
121 /* Cleanup any taken pointers. */
122 take_cleanup();
123 }
124
125 /* We carefully start all real properties with a zero byte. */
is_literal(const struct prop_hdr * prop)126 static bool is_literal(const struct prop_hdr *prop)
127 {
128 return ((char *)prop)[0] != 0;
129 }
130
131 #ifndef NDEBUG
132 static const void *bounds_start, *bounds_end;
133
update_bounds(const void * new,size_t size)134 static void update_bounds(const void *new, size_t size)
135 {
136 if (unlikely(!bounds_start)) {
137 bounds_start = new;
138 bounds_end = (char *)new + size;
139 } else if (new < bounds_start)
140 bounds_start = new;
141 else if ((char *)new + size > (char *)bounds_end)
142 bounds_end = (char *)new + size;
143 }
144
in_bounds(const void * p)145 static bool in_bounds(const void *p)
146 {
147 return !p
148 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
149 || (p >= bounds_start && p <= bounds_end);
150 }
151 #else
update_bounds(const void * new,size_t size)152 static void update_bounds(const void *new, size_t size)
153 {
154 }
155
in_bounds(const void * p)156 static bool in_bounds(const void *p)
157 {
158 return true;
159 }
160 #endif
161
check_bounds(const void * p)162 static void check_bounds(const void *p)
163 {
164 if (!in_bounds(p))
165 call_error("Not a valid header");
166 }
167
to_tal_hdr(const void * ctx)168 static struct tal_hdr *to_tal_hdr(const void *ctx)
169 {
170 struct tal_hdr *t;
171
172 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
173 check_bounds(t);
174 check_bounds(ignore_destroying_bit(t->parent_child));
175 check_bounds(t->list.next);
176 check_bounds(t->list.prev);
177 if (t->prop && !is_literal(t->prop))
178 check_bounds(t->prop);
179 return t;
180 }
181
to_tal_hdr_or_null(const void * ctx)182 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
183 {
184 if (!ctx)
185 return &null_parent.hdr;
186 return to_tal_hdr(ctx);
187 }
188
from_tal_hdr(const struct tal_hdr * hdr)189 static void *from_tal_hdr(const struct tal_hdr *hdr)
190 {
191 return (void *)(hdr + 1);
192 }
193
from_tal_hdr_or_null(const struct tal_hdr * hdr)194 static void *from_tal_hdr_or_null(const struct tal_hdr *hdr)
195 {
196 if (hdr == &null_parent.hdr)
197 return NULL;
198 return from_tal_hdr(hdr);
199 }
200
201 #ifdef TAL_DEBUG
debug_tal(struct tal_hdr * tal)202 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
203 {
204 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
205 return tal;
206 }
207 #else
debug_tal(struct tal_hdr * tal)208 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
209 {
210 return tal;
211 }
212 #endif
213
notify(const struct tal_hdr * ctx,enum tal_notify_type type,const void * info,int saved_errno)214 static void notify(const struct tal_hdr *ctx,
215 enum tal_notify_type type, const void *info,
216 int saved_errno)
217 {
218 const struct prop_hdr *p;
219
220 for (p = ctx->prop; p; p = p->next) {
221 struct notifier *n;
222
223 if (is_literal(p))
224 break;
225 if (p->type != NOTIFIER)
226 continue;
227 n = (struct notifier *)p;
228 if (n->types & type) {
229 errno = saved_errno;
230 if (n->types & NOTIFY_IS_DESTRUCTOR) {
231 /* Blatt this notifier in case it tries to
232 * tal_del_destructor() from inside */
233 union notifier_cb cb = n->u;
234 /* It's a union, so this NULLs destroy2 too! */
235 n->u.destroy = NULL;
236 if (n->types & NOTIFY_EXTRA_ARG)
237 cb.destroy2(from_tal_hdr(ctx),
238 EXTRA_ARG(n));
239 else
240 cb.destroy(from_tal_hdr(ctx));
241 } else
242 n->u.notifyfn(from_tal_hdr_or_null(ctx), type,
243 (void *)info);
244 }
245 }
246 }
247
allocate(size_t size)248 static void *allocate(size_t size)
249 {
250 void *ret = allocfn(size);
251 if (!ret)
252 call_error("allocation failed");
253 else
254 update_bounds(ret, size);
255 return ret;
256 }
257
find_property_ptr(const struct tal_hdr * t,enum prop_type type)258 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
259 enum prop_type type)
260 {
261 struct prop_hdr **p;
262
263 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
264 if (is_literal(*p)) {
265 if (type == NAME)
266 return p;
267 break;
268 }
269 if ((*p)->type == type)
270 return p;
271 }
272 return NULL;
273 }
274
find_property(const struct tal_hdr * parent,enum prop_type type)275 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
276 {
277 struct prop_hdr **p = find_property_ptr(parent, type);
278
279 if (p)
280 return *p;
281 return NULL;
282 }
283
init_property(struct prop_hdr * hdr,struct tal_hdr * parent,enum prop_type type)284 static void init_property(struct prop_hdr *hdr,
285 struct tal_hdr *parent,
286 enum prop_type type)
287 {
288 hdr->type = type;
289 hdr->next = parent->prop;
290 parent->prop = hdr;
291 }
292
add_notifier_property(struct tal_hdr * t,enum tal_notify_type types,void (* fn)(void *,enum tal_notify_type,void *),void * extra_arg)293 static struct notifier *add_notifier_property(struct tal_hdr *t,
294 enum tal_notify_type types,
295 void (*fn)(void *,
296 enum tal_notify_type,
297 void *),
298 void *extra_arg)
299 {
300 struct notifier *prop;
301
302 if (types & NOTIFY_EXTRA_ARG)
303 prop = allocate(sizeof(struct notifier_extra_arg));
304 else
305 prop = allocate(sizeof(struct notifier));
306
307 if (prop) {
308 init_property(&prop->hdr, t, NOTIFIER);
309 prop->types = types;
310 prop->u.notifyfn = fn;
311 if (types & NOTIFY_EXTRA_ARG)
312 EXTRA_ARG(prop) = extra_arg;
313 }
314 return prop;
315 }
316
del_notifier_property(struct tal_hdr * t,void (* fn)(tal_t *,enum tal_notify_type,void *),bool match_extra_arg,void * extra_arg)317 static enum tal_notify_type del_notifier_property(struct tal_hdr *t,
318 void (*fn)(tal_t *,
319 enum tal_notify_type,
320 void *),
321 bool match_extra_arg,
322 void *extra_arg)
323 {
324 struct prop_hdr **p;
325
326 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
327 struct notifier *n;
328 enum tal_notify_type types;
329
330 if (is_literal(*p))
331 break;
332 if ((*p)->type != NOTIFIER)
333 continue;
334 n = (struct notifier *)*p;
335 if (n->u.notifyfn != fn)
336 continue;
337
338 types = n->types;
339 if ((types & NOTIFY_EXTRA_ARG)
340 && match_extra_arg
341 && extra_arg != EXTRA_ARG(n))
342 continue;
343
344 *p = (*p)->next;
345 freefn(n);
346 return types & ~(NOTIFY_IS_DESTRUCTOR|NOTIFY_EXTRA_ARG);
347 }
348 return 0;
349 }
350
add_name_property(struct tal_hdr * t,const char * name)351 static struct name *add_name_property(struct tal_hdr *t, const char *name)
352 {
353 struct name *prop;
354
355 prop = allocate(sizeof(*prop) + strlen(name) + 1);
356 if (prop) {
357 init_property(&prop->hdr, t, NAME);
358 strcpy(prop->name, name);
359 }
360 return prop;
361 }
362
add_child_property(struct tal_hdr * parent,struct tal_hdr * child UNNEEDED)363 static struct children *add_child_property(struct tal_hdr *parent,
364 struct tal_hdr *child UNNEEDED)
365 {
366 struct children *prop = allocate(sizeof(*prop));
367 if (prop) {
368 init_property(&prop->hdr, parent, CHILDREN);
369 prop->parent = parent;
370 list_head_init(&prop->children);
371 }
372 return prop;
373 }
374
add_child(struct tal_hdr * parent,struct tal_hdr * child)375 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
376 {
377 struct children *children = find_property(parent, CHILDREN);
378
379 if (!children) {
380 children = add_child_property(parent, child);
381 if (!children)
382 return false;
383 }
384 list_add(&children->children, &child->list);
385 child->parent_child = (intptr_t)children ^ TAL_PTR_OBFUSTICATOR;
386 return true;
387 }
388
del_tree(struct tal_hdr * t,const tal_t * orig,int saved_errno)389 static void del_tree(struct tal_hdr *t, const tal_t *orig, int saved_errno)
390 {
391 struct prop_hdr **prop, *p, *next;
392
393 assert(!taken(from_tal_hdr(t)));
394
395 /* Already being destroyed? Don't loop. */
396 if (unlikely(get_destroying_bit(t->parent_child)))
397 return;
398
399 set_destroying_bit(&t->parent_child);
400
401 /* Call free notifiers. */
402 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig, saved_errno);
403
404 /* Now free children and groups. */
405 prop = find_property_ptr(t, CHILDREN);
406 if (prop) {
407 struct tal_hdr *i;
408 struct children *c = (struct children *)*prop;
409
410 while ((i = list_top(&c->children, struct tal_hdr, list))) {
411 list_del(&i->list);
412 del_tree(i, orig, saved_errno);
413 }
414 }
415
416 /* Finally free our properties. */
417 for (p = t->prop; p && !is_literal(p); p = next) {
418 next = p->next;
419 freefn(p);
420 }
421 freefn(t);
422 }
423
tal_alloc_(const tal_t * ctx,size_t size,bool clear,const char * label)424 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
425 {
426 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
427
428 child = allocate(sizeof(struct tal_hdr) + size);
429 if (!child)
430 return NULL;
431 if (clear)
432 memset(from_tal_hdr(child), 0, size);
433 child->prop = (void *)label;
434 child->bytelen = size;
435
436 if (!add_child(parent, child)) {
437 freefn(child);
438 return NULL;
439 }
440 debug_tal(parent);
441 if (notifiers)
442 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child), 0);
443 return from_tal_hdr(debug_tal(child));
444 }
445
adjust_size(size_t * size,size_t count)446 static bool adjust_size(size_t *size, size_t count)
447 {
448 const size_t extra = sizeof(struct tal_hdr);
449
450 /* Multiplication wrap */
451 if (count && unlikely(*size * count / *size != count))
452 goto overflow;
453
454 *size *= count;
455
456 /* Make sure we don't wrap adding header. */
457 if (*size + extra < extra)
458 goto overflow;
459 return true;
460 overflow:
461 call_error("allocation size overflow");
462 return false;
463 }
464
tal_alloc_arr_(const tal_t * ctx,size_t size,size_t count,bool clear,const char * label)465 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
466 const char *label)
467 {
468 if (!adjust_size(&size, count))
469 return NULL;
470
471 return tal_alloc_(ctx, size, clear, label);
472 }
473
tal_free(const tal_t * ctx)474 void *tal_free(const tal_t *ctx)
475 {
476 if (ctx) {
477 struct tal_hdr *t;
478 int saved_errno = errno;
479 t = debug_tal(to_tal_hdr(ctx));
480 if (unlikely(get_destroying_bit(t->parent_child)))
481 return NULL;
482 if (notifiers)
483 notify(ignore_destroying_bit(t->parent_child)->parent,
484 TAL_NOTIFY_DEL_CHILD, ctx, saved_errno);
485 list_del(&t->list);
486 del_tree(t, ctx, saved_errno);
487 errno = saved_errno;
488 }
489 return NULL;
490 }
491
tal_steal_(const tal_t * new_parent,const tal_t * ctx)492 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
493 {
494 if (ctx) {
495 struct tal_hdr *newpar, *t, *old_parent;
496
497 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
498 t = debug_tal(to_tal_hdr(ctx));
499
500 /* Unlink it from old parent. */
501 list_del(&t->list);
502 old_parent = ignore_destroying_bit(t->parent_child)->parent;
503
504 if (unlikely(!add_child(newpar, t))) {
505 /* We can always add to old parent, because it has a
506 * children property already. */
507 if (!add_child(old_parent, t))
508 abort();
509 return NULL;
510 }
511 debug_tal(newpar);
512 if (notifiers)
513 notify(t, TAL_NOTIFY_STEAL, new_parent, 0);
514 }
515 return (void *)ctx;
516 }
517
tal_add_destructor_(const tal_t * ctx,void (* destroy)(void * me))518 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
519 {
520 tal_t *t = debug_tal(to_tal_hdr(ctx));
521 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
522 (void *)destroy, NULL);
523 }
524
tal_add_destructor2_(const tal_t * ctx,void (* destroy)(void * me,void * arg),void * arg)525 bool tal_add_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg),
526 void *arg)
527 {
528 tal_t *t = debug_tal(to_tal_hdr(ctx));
529 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR
530 |NOTIFY_EXTRA_ARG,
531 (void *)destroy, arg);
532 }
533
534 /* We could support notifiers with an extra arg, but we didn't add to API */
tal_add_notifier_(const tal_t * ctx,enum tal_notify_type types,void (* callback)(tal_t *,enum tal_notify_type,void *))535 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
536 void (*callback)(tal_t *, enum tal_notify_type, void *))
537 {
538 struct tal_hdr *t = debug_tal(to_tal_hdr_or_null(ctx));
539 struct notifier *n;
540
541 assert(types);
542 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
543 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
544 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
545 | TAL_NOTIFY_ADD_NOTIFIER
546 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
547
548 /* Don't call notifier about itself: set types after! */
549 n = add_notifier_property(t, 0, callback, NULL);
550 if (unlikely(!n))
551 return false;
552
553 if (notifiers)
554 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback, 0);
555
556 n->types = types;
557 if (types != TAL_NOTIFY_FREE)
558 notifiers++;
559 return true;
560 }
561
tal_del_notifier_(const tal_t * ctx,void (* callback)(tal_t *,enum tal_notify_type,void *),bool match_extra_arg,void * extra_arg)562 bool tal_del_notifier_(const tal_t *ctx,
563 void (*callback)(tal_t *, enum tal_notify_type, void *),
564 bool match_extra_arg, void *extra_arg)
565 {
566 struct tal_hdr *t = debug_tal(to_tal_hdr_or_null(ctx));
567 enum tal_notify_type types;
568
569 types = del_notifier_property(t, callback, match_extra_arg, extra_arg);
570 if (types) {
571 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback, 0);
572 if (types != TAL_NOTIFY_FREE)
573 notifiers--;
574 return true;
575 }
576 return false;
577 }
578
tal_del_destructor_(const tal_t * ctx,void (* destroy)(void * me))579 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
580 {
581 return tal_del_notifier_(ctx, (void *)destroy, false, NULL);
582 }
583
tal_del_destructor2_(const tal_t * ctx,void (* destroy)(void * me,void * arg),void * arg)584 bool tal_del_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg),
585 void *arg)
586 {
587 return tal_del_notifier_(ctx, (void *)destroy, true, arg);
588 }
589
tal_set_name_(tal_t * ctx,const char * name,bool literal)590 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
591 {
592 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
593 struct prop_hdr **prop = find_property_ptr(t, NAME);
594
595 /* Get rid of any old name */
596 if (prop) {
597 struct name *name = (struct name *)*prop;
598 if (is_literal(&name->hdr))
599 *prop = NULL;
600 else {
601 *prop = name->hdr.next;
602 freefn(name);
603 }
604 }
605
606 if (literal && name[0]) {
607 struct prop_hdr **p;
608
609 /* Append literal. */
610 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
611 *p = (struct prop_hdr *)name;
612 } else if (!add_name_property(t, name))
613 return false;
614
615 debug_tal(t);
616 if (notifiers)
617 notify(t, TAL_NOTIFY_RENAME, name, 0);
618 return true;
619 }
620
tal_name(const tal_t * t)621 const char *tal_name(const tal_t *t)
622 {
623 struct name *n;
624
625 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
626 if (!n)
627 return NULL;
628
629 if (is_literal(&n->hdr))
630 return (const char *)n;
631 return n->name;
632 }
633
tal_bytelen(const tal_t * ptr)634 size_t tal_bytelen(const tal_t *ptr)
635 {
636 /* NULL -> null_parent which has bytelen 0 */
637 struct tal_hdr *t = debug_tal(to_tal_hdr_or_null(ptr));
638
639 return t->bytelen;
640 }
641
642 /* Start one past first child: make stopping natural in circ. list. */
first_child(struct tal_hdr * parent)643 static struct tal_hdr *first_child(struct tal_hdr *parent)
644 {
645 struct children *child;
646
647 child = find_property(parent, CHILDREN);
648 if (!child)
649 return NULL;
650
651 return list_top(&child->children, struct tal_hdr, list);
652 }
653
tal_first(const tal_t * root)654 tal_t *tal_first(const tal_t *root)
655 {
656 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
657
658 c = first_child(t);
659 if (!c)
660 return NULL;
661 return from_tal_hdr(c);
662 }
663
tal_next(const tal_t * prev)664 tal_t *tal_next(const tal_t *prev)
665 {
666 struct tal_hdr *next, *prevhdr = debug_tal(to_tal_hdr(prev));
667 struct list_head *head;
668
669 head = &ignore_destroying_bit(prevhdr->parent_child)->children;
670 next = list_next(head, prevhdr, list);
671 if (!next)
672 return NULL;
673 return from_tal_hdr(next);
674 }
675
tal_parent(const tal_t * ctx)676 tal_t *tal_parent(const tal_t *ctx)
677 {
678 struct tal_hdr *t;
679
680 if (!ctx)
681 return NULL;
682
683 t = debug_tal(to_tal_hdr(ctx));
684 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
685 return NULL;
686 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
687 }
688
tal_resize_(tal_t ** ctxp,size_t size,size_t count,bool clear)689 bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear)
690 {
691 struct tal_hdr *old_t, *t;
692 struct children *child;
693
694 old_t = debug_tal(to_tal_hdr(*ctxp));
695
696 if (!adjust_size(&size, count))
697 return false;
698
699 t = resizefn(old_t, sizeof(struct tal_hdr) + size);
700 if (!t) {
701 call_error("Reallocation failure");
702 return false;
703 }
704
705 /* Clear between old end and new end. */
706 if (clear && size > t->bytelen) {
707 char *old_end = (char *)(t + 1) + t->bytelen;
708 memset(old_end, 0, size - t->bytelen);
709 }
710
711 /* Update length. */
712 t->bytelen = size;
713 update_bounds(t, sizeof(struct tal_hdr) + size);
714
715 /* If it didn't move, we're done! */
716 if (t != old_t) {
717 /* Fix up linked list pointers. */
718 t->list.next->prev = t->list.prev->next = &t->list;
719
720 /* Copy take() property. */
721 if (taken(from_tal_hdr(old_t)))
722 take(from_tal_hdr(t));
723
724 /* Fix up child property's parent pointer. */
725 child = find_property(t, CHILDREN);
726 if (child) {
727 assert(child->parent == old_t);
728 child->parent = t;
729 }
730 *ctxp = from_tal_hdr(debug_tal(t));
731 if (notifiers)
732 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t), 0);
733 }
734 if (notifiers)
735 notify(t, TAL_NOTIFY_RESIZE, (void *)size, 0);
736
737 return true;
738 }
739
tal_expand_(tal_t ** ctxp,const void * src,size_t size,size_t count)740 bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count)
741 {
742 size_t old_len;
743 bool ret = false;
744
745 old_len = debug_tal(to_tal_hdr(*ctxp))->bytelen;
746
747 /* Check for additive overflow */
748 if (old_len + count * size < old_len) {
749 call_error("dup size overflow");
750 goto out;
751 }
752
753 /* Don't point src inside thing we're expanding! */
754 assert(src < *ctxp
755 || (char *)src >= (char *)(*ctxp) + old_len);
756
757 if (!tal_resize_(ctxp, size, old_len/size + count, false))
758 goto out;
759
760 memcpy((char *)*ctxp + old_len, src, count * size);
761 ret = true;
762
763 out:
764 if (taken(src))
765 tal_free(src);
766 return ret;
767 }
768
tal_dup_(const tal_t * ctx,const void * p,size_t size,size_t n,size_t extra,const char * label)769 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
770 size_t n, size_t extra, const char *label)
771 {
772 void *ret;
773 size_t nbytes = size;
774
775 if (!adjust_size(&nbytes, n)) {
776 if (taken(p))
777 tal_free(p);
778 return NULL;
779 }
780
781 /* Beware addition overflow! */
782 if (n + extra < n) {
783 call_error("dup size overflow");
784 if (taken(p))
785 tal_free(p);
786 return NULL;
787 }
788
789 if (taken(p)) {
790 if (unlikely(!p))
791 return NULL;
792 if (unlikely(!tal_resize_((void **)&p, size, n + extra, false)))
793 return tal_free(p);
794 if (unlikely(!tal_steal(ctx, p)))
795 return tal_free(p);
796 return (void *)p;
797 }
798
799 ret = tal_alloc_arr_(ctx, size, n + extra, false, label);
800 if (ret)
801 memcpy(ret, p, nbytes);
802 return ret;
803 }
804
tal_set_backend(void * (* alloc_fn)(size_t size),void * (* resize_fn)(void *,size_t size),void (* free_fn)(void *),void (* error_fn)(const char * msg))805 void tal_set_backend(void *(*alloc_fn)(size_t size),
806 void *(*resize_fn)(void *, size_t size),
807 void (*free_fn)(void *),
808 void (*error_fn)(const char *msg))
809 {
810 if (alloc_fn)
811 allocfn = alloc_fn;
812 if (resize_fn)
813 resizefn = resize_fn;
814 if (free_fn)
815 freefn = free_fn;
816 if (error_fn)
817 errorfn = error_fn;
818 }
819
820 #ifdef CCAN_TAL_DEBUG
dump_node(unsigned int indent,const struct tal_hdr * t)821 static void dump_node(unsigned int indent, const struct tal_hdr *t)
822 {
823 unsigned int i;
824 const struct prop_hdr *p;
825
826 for (i = 0; i < indent; i++)
827 fprintf(stderr, " ");
828 fprintf(stderr, "%p len=%zu", t, t->bytelen);
829 for (p = t->prop; p; p = p->next) {
830 struct children *c;
831 struct name *n;
832 struct notifier *no;
833 if (is_literal(p)) {
834 fprintf(stderr, " \"%s\"", (const char *)p);
835 break;
836 }
837 switch (p->type) {
838 case CHILDREN:
839 c = (struct children *)p;
840 fprintf(stderr, " CHILDREN(%p):parent=%p,children={%p,%p}\n",
841 p, c->parent,
842 c->children.n.prev, c->children.n.next);
843 break;
844 case NAME:
845 n = (struct name *)p;
846 fprintf(stderr, " NAME(%p):%s", p, n->name);
847 break;
848 case NOTIFIER:
849 no = (struct notifier *)p;
850 fprintf(stderr, " NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
851 break;
852 default:
853 fprintf(stderr, " **UNKNOWN(%p):%i**", p, p->type);
854 }
855 }
856 fprintf(stderr, "\n");
857 }
858
tal_dump_(unsigned int level,const struct tal_hdr * t)859 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
860 {
861 struct children *children;
862
863 dump_node(level, t);
864
865 children = find_property(t, CHILDREN);
866 if (children) {
867 struct tal_hdr *i;
868
869 list_for_each(&children->children, i, list)
870 tal_dump_(level + 1, i);
871 }
872 }
873
tal_dump(void)874 void tal_dump(void)
875 {
876 tal_dump_(0, &null_parent.hdr);
877 }
878 #endif /* CCAN_TAL_DEBUG */
879
880 #ifndef NDEBUG
check_err(struct tal_hdr * t,const char * errorstr,const char * errmsg)881 static bool check_err(struct tal_hdr *t, const char *errorstr,
882 const char *errmsg)
883 {
884 if (errorstr) {
885 /* Try not to malloc: it may be corrupted. */
886 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
887 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
888 call_error(msg);
889 }
890 return false;
891 }
892
check_node(struct children * parent_child,struct tal_hdr * t,const char * errorstr)893 static bool check_node(struct children *parent_child,
894 struct tal_hdr *t, const char *errorstr)
895 {
896 struct prop_hdr *p;
897 struct name *name = NULL;
898 struct children *children = NULL;
899
900 if (!in_bounds(t))
901 return check_err(t, errorstr, "invalid pointer");
902
903 if (ignore_destroying_bit(t->parent_child) != parent_child)
904 return check_err(t, errorstr, "incorrect parent");
905
906 for (p = t->prop; p; p = p->next) {
907 if (is_literal(p)) {
908 if (name)
909 return check_err(t, errorstr,
910 "has extra literal");
911 break;
912 }
913 if (!in_bounds(p))
914 return check_err(t, errorstr,
915 "has bad property pointer");
916
917 switch (p->type) {
918 case CHILDREN:
919 if (children)
920 return check_err(t, errorstr,
921 "has two child nodes");
922 children = (struct children *)p;
923 break;
924 case NOTIFIER:
925 break;
926 case NAME:
927 if (name)
928 return check_err(t, errorstr,
929 "has two names");
930 name = (struct name *)p;
931 break;
932 default:
933 return check_err(t, errorstr, "has unknown property");
934 }
935 }
936 if (children) {
937 struct tal_hdr *i;
938
939 if (!list_check(&children->children, errorstr))
940 return false;
941 list_for_each(&children->children, i, list) {
942 if (!check_node(children, i, errorstr))
943 return false;
944 }
945 }
946 return true;
947 }
948
tal_check(const tal_t * ctx,const char * errorstr)949 bool tal_check(const tal_t *ctx, const char *errorstr)
950 {
951 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
952
953 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
954 }
955 #else /* NDEBUG */
tal_check(const tal_t * ctx,const char * errorstr)956 bool tal_check(const tal_t *ctx, const char *errorstr)
957 {
958 return true;
959 }
960 #endif
961