From 68043c2e8ccf00e2f7a48f62d32d2c4d041cfd01 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 15 Nov 2021 04:21:46 +1030 Subject: [PATCH] common: clean up autodata in common_shutdown(). valgrind locally complains about the allocations in autodata leaking: ``` ==138200== 16 bytes in 1 blocks are still reachable in loss record 1 of 2 ==138200== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==138200== by 0x10D41A: autodata_register_ (autodata.c:20) ==138200== by 0x10E7B8: register_autotype_type_to_string (type_to_string.h:79) ==138200== by 0x10F5CA: register_one_type_to_string0 (block.c:259) ==138200== by 0x19734C: __libc_csu_init (in /home/rusty/devel/cvs/lightning/common/test/run-route-specific) ==138200== by 0x4A3D03F: (below main) (libc-start.c:264) ==138200== ==138200== 176 bytes in 1 blocks are still reachable in loss record 2 of 2 ==138200== at 0x483DFAF: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==138200== by 0x10D472: autodata_register_ (autodata.c:26) ==138200== by 0x122D37: register_autotype_type_to_string (type_to_string.h:79) ==138200== by 0x122F1F: register_one_type_to_string0 (node_id.c:50) ==138200== by 0x19734C: __libc_csu_init (in /home/rusty/devel/cvs/lightning/common/test/run-route-specific) ==138200== by 0x4A3D03F: (below main) (libc-start.c:264) ==138200== make: *** [Makefile:638: unittest/common/test/run-route-specific] Error 7 ``` Signed-off-by: Rusty Russell --- common/autodata.c | 14 ++++++++++++++ common/autodata.h | 3 +++ common/setup.c | 2 ++ connectd/Makefile | 2 +- tests/plugins/Makefile | 2 +- 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/common/autodata.c b/common/autodata.c index 4273a9b0b..ddeab76fd 100644 --- a/common/autodata.c +++ b/common/autodata.c @@ -38,3 +38,17 @@ void *autodata_get_(const char *typename, size_t *nump) *nump = t->num; return t->ptrs; } + +static bool free_one(const char *member, + struct typereg *t, void *unused) +{ + free(t->ptrs); + free(t); + return true; +} + +void autodata_cleanup(void) +{ + strmap_iterate(&typemap, free_one, NULL); + strmap_clear(&typemap); +} diff --git a/common/autodata.h b/common/autodata.h index 7d05217b4..60c300bab 100644 --- a/common/autodata.h +++ b/common/autodata.h @@ -3,6 +3,7 @@ #include "config.h" #include #include +#include #define AUTODATA_TYPE(name, type) \ static inline void register_autotype_##name(const type *t) { \ @@ -23,4 +24,6 @@ void autodata_register_(const char *typename, const void *ptr); void *autodata_get_(const char *typename, size_t *nump); +/* Call on shutdown to keep valgrind leak detection happy. */ +void autodata_cleanup(void); #endif /* LIGHTNING_COMMON_AUTODATA_H */ diff --git a/common/setup.c b/common/setup.c index 6f4c4182a..46879e6e1 100644 --- a/common/setup.c +++ b/common/setup.c @@ -1,6 +1,7 @@ #include "config.h" #include #include +#include #include #include #include @@ -58,4 +59,5 @@ void common_shutdown(void) tal_free(tmpctx); wally_cleanup(0); tal_free(wally_tal_ctx); + autodata_cleanup(); } diff --git a/connectd/Makefile b/connectd/Makefile index 8d30894c8..8e17d4c17 100644 --- a/connectd/Makefile +++ b/connectd/Makefile @@ -76,6 +76,6 @@ CONNECTD_COMMON_OBJS := \ lightningd/lightning_connectd: $(CONNECTD_OBJS) $(CONNECTD_COMMON_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(HSMD_CLIENT_OBJS) -lightningd/lightning_websocketd: $(WEBSOCKETD_OBJS) common/setup.o common/utils.o +lightningd/lightning_websocketd: $(WEBSOCKETD_OBJS) common/setup.o common/utils.o common/autodata.o include connectd/test/Makefile diff --git a/tests/plugins/Makefile b/tests/plugins/Makefile index 94ba95762..3f2e6e3aa 100644 --- a/tests/plugins/Makefile +++ b/tests/plugins/Makefile @@ -8,7 +8,7 @@ $(PLUGIN_TESTLIBPLUGIN_OBJS): $(PLUGIN_LIB_HEADER) PLUGIN_TESTSELFDISABLE_AFTER_GETMANIFEST_SRC := tests/plugins/test_selfdisable_after_getmanifest.c PLUGIN_TESTSELFDISABLE_AFTER_GETMANIFEST_OBJS := $(PLUGIN_TESTSELFDISABLE_AFTER_GETMANIFEST_SRC:.c=.o) -tests/plugins/test_selfdisable_after_getmanifest: bitcoin/chainparams.o $(PLUGIN_TESTSELFDISABLE_AFTER_GETMANIFEST_OBJS) common/json.o common/json_stream.o common/setup.o common/utils.o $(JSMN_OBJS) $(CCAN_OBJS) +tests/plugins/test_selfdisable_after_getmanifest: bitcoin/chainparams.o $(PLUGIN_TESTSELFDISABLE_AFTER_GETMANIFEST_OBJS) common/autodata.o common/json.o common/json_stream.o common/setup.o common/utils.o $(JSMN_OBJS) $(CCAN_OBJS) # Make sure these depend on everything. ALL_TEST_PROGRAMS += tests/plugins/test_libplugin tests/plugins/test_selfdisable_after_getmanifest