From d5eca470dca0090e671e272cdb0b29f45f619280 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 8 Oct 2019 11:42:24 +1030 Subject: [PATCH] utils: add set_softref() / clear_softref(). We often want a pointer which will turn to NULL if the pointed-to thing is freed. This is possible with tal objects, so create it. Signed-off-by: Rusty Russell --- common/test/run-softref.c | 85 +++++++++++++++++++++++++++++++++++++++ common/utils.c | 72 +++++++++++++++++++++++++++++++++ common/utils.h | 14 +++++++ 3 files changed, 171 insertions(+) create mode 100644 common/test/run-softref.c diff --git a/common/test/run-softref.c b/common/test/run-softref.c new file mode 100644 index 000000000..163a5a2c2 --- /dev/null +++ b/common/test/run-softref.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include + +/* AUTOGENERATED MOCKS START */ +/* Generated stub for amount_asset_is_main */ +bool amount_asset_is_main(struct amount_asset *asset UNNEEDED) +{ fprintf(stderr, "amount_asset_is_main called!\n"); abort(); } +/* Generated stub for amount_asset_to_sat */ +struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) +{ fprintf(stderr, "amount_asset_to_sat called!\n"); abort(); } +/* Generated stub for amount_sat_add */ + bool amount_sat_add(struct amount_sat *val UNNEEDED, + struct amount_sat a UNNEEDED, + struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_add called!\n"); abort(); } +/* Generated stub for amount_sat_eq */ +bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); } +/* Generated stub for amount_sat_sub */ + bool amount_sat_sub(struct amount_sat *val UNNEEDED, + struct amount_sat a UNNEEDED, + struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_sub called!\n"); abort(); } +/* Generated stub for fromwire_fail */ +const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_fail called!\n"); abort(); } +/* AUTOGENERATED MOCKS END */ + +struct objtype { + char *c_softref, *c_softref2; +}; + +int main(void) +{ + const void *ctx = tal(NULL, char); + struct objtype *o; + char *c; + + setup_locale(); + + /* Simple test: freeing obj NULLs softref */ + o = tal(ctx, struct objtype); + c = tal(ctx, char); + set_softref(o, &o->c_softref, c); + assert(o->c_softref == c); + + tal_free(c); + assert(o->c_softref == NULL);\ + + /* Duplicate ptrs work */ + o = tal(ctx, struct objtype); + c = tal(ctx, char); + set_softref(o, &o->c_softref, c); + assert(o->c_softref == c); + set_softref(o, &o->c_softref2, c); + assert(o->c_softref2 == c); + + tal_free(c); + assert(o->c_softref == NULL); + assert(o->c_softref2 == NULL); + + /* Cleans up properly if o is freed first. */ + c = tal(ctx, char); + set_softref(o, &o->c_softref, c); + tal_free(o); + tal_free(c); + + /* Setting to NULL works. */ + o = tal(ctx, struct objtype); + c = tal(ctx, char); + set_softref(o, &o->c_softref, c); + assert(o->c_softref == c); + clear_softref(o, &o->c_softref); + assert(o->c_softref == NULL); + + /* Now it's not a softref, won't clear! */ + o->c_softref = c; + tal_free(c); + assert(o->c_softref == c); + tal_free(o); + + tal_free(ctx); +} diff --git a/common/utils.c b/common/utils.c index 959f535c6..7c7f05ecc 100644 --- a/common/utils.c +++ b/common/utils.c @@ -15,6 +15,78 @@ bool is_elements(const struct chainparams *chainparams) return chainparams->is_elements; } +#if DEVELOPER +/* If you've got a softref, we assume no reallocs. */ +static void dont_move_softref(tal_t *ctx, enum tal_notify_type ntype, void *info) +{ + abort(); +} +#endif + +static void softref_nullify(tal_t *obj, void **ptr) +{ + *ptr = NULL; +#if DEVELOPER + tal_del_notifier(obj, dont_move_softref); +#endif +} + +static void softref_cleanup(const tal_t *outer, void **ptr) +{ + if (*ptr) { + tal_del_destructor2(*ptr, softref_nullify, ptr); + } +#if DEVELOPER + tal_del_notifier(outer, dont_move_softref); +#endif +} + +void set_softref_(const tal_t *outer, size_t outersize, void **ptr, tal_t *obj) +{ + /* pointer is inside outer, right? */ + assert((char *)ptr >= (char *)outer); + assert((char *)ptr < (char *)outer + outersize); + + /* This is harmless if there was no prior, otherwise constrains the + * leak: we don't have enough information in softref_nullify to + * clear softref_cleanup */ + tal_del_destructor2(outer, softref_cleanup, ptr); + + if (obj) { + tal_add_destructor2(outer, softref_cleanup, ptr); + tal_add_destructor2(obj, softref_nullify, ptr); +#if DEVELOPER + tal_add_notifier(obj, TAL_NOTIFY_MOVE, dont_move_softref); +#endif + } + +#if DEVELOPER + tal_add_notifier(outer, TAL_NOTIFY_MOVE, dont_move_softref); +#endif + + *ptr = obj; +} + +void clear_softref_(const tal_t *outer, size_t outersize, void **ptr) +{ + assert((char *)ptr >= (char *)outer); + assert((char *)ptr < (char *)outer + outersize); + + if (*ptr) { + tal_del_destructor2(outer, softref_cleanup, ptr); + tal_del_destructor2(*ptr, softref_nullify, ptr); +#if DEVELOPER + tal_del_notifier(*ptr, dont_move_softref); +#endif + } + +#if DEVELOPER + tal_del_notifier(outer, dont_move_softref); +#endif + + *ptr = NULL; +} + char *tal_hexstr(const tal_t *ctx, const void *data, size_t len) { char *str = tal_arr(ctx, char, hex_str_size(len)); diff --git a/common/utils.h b/common/utils.h index 85689e8a2..35e9e1517 100644 --- a/common/utils.h +++ b/common/utils.h @@ -25,6 +25,20 @@ char *tal_hex(const tal_t *ctx, const tal_t *data); /* Allocate and fill a buffer with the data of this hex string. */ u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len); +/* Macro to set memberptr in tal object outer to point to tal object obj, + * if it isn't NULL. + * The 0*sizeof() checks that *memberptr = obj is valid */ +#define set_softref(outer, memberptr, obj) \ + set_softref_((outer), sizeof(*(outer)) + 0*sizeof(*(memberptr) = obj), \ + (void **)(memberptr), (obj)) + +/* Macro to clear a (set) softref ptr to NULL */ +#define clear_softref(outer, memberptr) \ + clear_softref_((outer), sizeof(*(outer)), (void **)(memberptr)) + +void set_softref_(const tal_t *outer, size_t outersize, void **ptr, tal_t *obj); +void clear_softref_(const tal_t *outer, size_t outersize, void **ptr); + /* Note: p is never a complex expression, otherwise this multi-evaluates! */ #define tal_arr_expand(p, s) \ do { \