Browse Source

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 <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
602117e8bb
  1. 2
      Makefile
  2. 3
      bitcoin/shadouble.c
  3. 7
      bitcoin/tx.c
  4. 18
      bitcoin/valgrind.h

2
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.

3
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));
}

7
bitcoin/tx.c

@ -5,6 +5,7 @@
#include <ccan/tal/grab_file/grab_file.h>
#include <assert.h>
#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)

18
bitcoin/valgrind.h

@ -0,0 +1,18 @@
#ifndef LIGHTNING_VALGRIND_H
#define LIGHTNING_VALGRIND_H
#ifdef VALGRIND_HEADERS
#include <valgrind/memcheck.h>
#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 */
Loading…
Cancel
Save