memleak: make notleak() work even before memleak is initalized.

It now simply renames tal names, so it's harmless to do even if we're
not going to do memleak detection.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2025-10-23 10:09:12 +10:30
parent 478a0d5792
commit 06f18b1d1d
3 changed files with 9 additions and 14 deletions

View File

@@ -9,6 +9,7 @@
#include <ccan/tal/str/str.h>
#include <common/configdir.h>
#include <common/configvar.h>
#include <common/memleak.h>
#include <common/utils.h>
#include <common/version.h>
@@ -35,8 +36,7 @@ static char *opt_set_abspath(const char *arg, char **p)
/* Tal wrappers for opt. */
static void *opt_allocfn(size_t size)
{
return tal_arr_label(NULL, char, size,
TAL_LABEL(opt_allocfn_notleak, ""));
return notleak(tal_arr(NULL, char, size));
}
static void *tal_reallocfn(void *ptr, size_t size)

View File

@@ -58,9 +58,6 @@ struct tal_backtrace {
void *notleak_(void *ptr, bool plus_children)
{
const char *name;
/* If we're not tracking, don't do anything. */
if (!memleak_track)
return cast_const(void *, ptr);
/* We use special tal names to mark notleak */
name = tal_name(ptr);
@@ -69,12 +66,14 @@ void *notleak_(void *ptr, bool plus_children)
/* Don't mark more than once! */
if (!strstr(name, "**NOTLEAK")) {
/* Don't use tmpctx: it might not be set up yet! */
if (plus_children)
name = tal_fmt(tmpctx, "%s **NOTLEAK_IGNORE_CHILDREN**",
name = tal_fmt(NULL, "%s **NOTLEAK_IGNORE_CHILDREN**",
name);
else
name = tal_fmt(tmpctx, "%s **NOTLEAK**", name);
name = tal_fmt(NULL, "%s **NOTLEAK**", name);
tal_set_name(ptr, name);
tal_free(name);
}
return cast_const(void *, ptr);
@@ -331,8 +330,7 @@ static void call_memleak_helpers(struct htable *memtable, const tal_t *p)
if (strends(name, "struct memleak_helper")) {
const struct memleak_helper *mh = i;
mh->cb(memtable, p);
} else if (strends(name, " **NOTLEAK**")
|| strends(name, "_notleak")) {
} else if (strends(name, " **NOTLEAK**")) {
memleak_ptr(memtable, i);
memleak_scan_obj(memtable, i);
} else if (strends(name,

View File

@@ -5,6 +5,7 @@
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <common/json_stream.h>
#include <common/memleak.h>
#include <common/pseudorand.h>
#include <common/trace.h>
#include <inttypes.h>
@@ -181,15 +182,11 @@ static inline void trace_check_tree(void) {}
static void trace_init(void)
{
const char *dev_trace_file;
const char notleak_name[] = "struct span **NOTLEAK**";
if (active_spans)
return;
active_spans = tal_arrz(NULL, struct span, 1);
/* We're usually too early for memleak to be initialized, so mark
* this notleak manually! */
tal_set_name(active_spans, notleak_name);
active_spans = notleak(tal_arrz(NULL, struct span, 1));
current = NULL;
dev_trace_file = getenv("CLN_DEV_TRACE_FILE");