From 602117e8bb66893405f029a2559ae0fcd27a5cf4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 24 Jun 2015 16:15:34 +0930 Subject: [PATCH] Add valgrind memcheck helpers. Otherwise valgrind tells you when you test a hash; you want to know if you hash uninitialized memory long before that. Signed-off-by: Rusty Russell --- Makefile | 2 +- bitcoin/shadouble.c | 3 ++- bitcoin/tx.c | 7 ++++--- bitcoin/valgrind.h | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 bitcoin/valgrind.h diff --git a/Makefile b/Makefile index 2f1d1ec75..5b1617ccf 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ CCAN_OBJS := ccan-crypto-sha256.o ccan-crypto-shachain.o ccan-err.o ccan-tal.o c HEADERS := $(wildcard *.h) CCANDIR := ccan/ -CFLAGS := -g -Wall -I $(CCANDIR) +CFLAGS := -g -Wall -I $(CCANDIR) -DVALGRIND_HEADERS=1 LDLIBS := -lcrypto -lprotobuf-c $(PROGRAMS): CFLAGS+=-I. diff --git a/bitcoin/shadouble.c b/bitcoin/shadouble.c index 028ef483d..b8897e58e 100644 --- a/bitcoin/shadouble.c +++ b/bitcoin/shadouble.c @@ -1,8 +1,9 @@ #include "shadouble.h" +#include "valgrind.h" void sha256_double(struct sha256_double *shadouble, const void *p, size_t len) { - sha256(&shadouble->sha, p, len); + sha256(&shadouble->sha, check_mem(p, len), len); sha256(&shadouble->sha, &shadouble->sha, sizeof(shadouble->sha)); } diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 4028a80b4..5e41c80ca 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -5,6 +5,7 @@ #include #include #include "tx.h" +#include "valgrind.h" static void add_varint(varint_t v, void (*add)(const void *, size_t, void *), void *addp) @@ -87,7 +88,7 @@ static void add_tx(const struct bitcoin_tx *tx, static void add_sha(const void *data, size_t len, void *shactx_) { struct sha256_ctx *ctx = shactx_; - sha256_update(ctx, data, len); + sha256_update(ctx, check_mem(data, len), len); } void sha256_tx(struct sha256_ctx *ctx, const struct bitcoin_tx *tx) @@ -101,7 +102,7 @@ static void add_linearize(const void *data, size_t len, void *pptr_) size_t oldsize = tal_count(*pptr); tal_resize(pptr, oldsize + len); - memcpy(*pptr + oldsize, data, len); + memcpy(*pptr + oldsize, check_mem(data, len), len); } u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx) @@ -157,7 +158,7 @@ static const u8 *pull(const u8 **cursor, size_t *max, void *copy, size_t n) *max -= n; if (copy) memcpy(copy, p, n); - return p; + return check_mem(p, n); } static u64 pull_varint(const u8 **cursor, size_t *max) diff --git a/bitcoin/valgrind.h b/bitcoin/valgrind.h new file mode 100644 index 000000000..11fbb1696 --- /dev/null +++ b/bitcoin/valgrind.h @@ -0,0 +1,18 @@ +#ifndef LIGHTNING_VALGRIND_H +#define LIGHTNING_VALGRIND_H + +#ifdef VALGRIND_HEADERS +#include +#elif !defined(VALGRIND_CHECK_MEM_IS_DEFINED) +#define VALGRIND_CHECK_MEM_IS_DEFINED(p, len) +#define RUNNING_ON_VALGRIND 0 +#endif + +/* Useful for hashing: makes sure we're not hashing crap *before* we use + * the hash value for something. */ +static inline void *check_mem(const void *data, size_t len) +{ + VALGRIND_CHECK_MEM_IS_DEFINED(data, len); + return (void *)data; +} +#endif /* LIGHTNING_VALGRIND_H */