Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions bitcoin/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,6 @@ u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct bitcoin_address *addr)
return script;
}

u8 *scriptpubkey_opreturn_padded(const tal_t *ctx)
{
u8 *script = tal_arr(ctx, u8, 0);
u8 random[20];
randombytes_buf(random, sizeof(random));

add_op(&script, OP_RETURN);
script_push_bytes(&script, random, sizeof(random));
return script;
}

/* Create an input script which spends p2pkh */
u8 *bitcoin_redeem_p2pkh(const tal_t *ctx, const struct pubkey *pubkey,
const struct bitcoin_signature *sig)
Expand Down
6 changes: 0 additions & 6 deletions bitcoin/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ u8 *scriptpubkey_p2sh_hash(const tal_t *ctx, const struct ripemd160 *redeemhash)
/* Create an output script using p2pkh */
u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct bitcoin_address *addr);

/* Create a prunable output script with 20 random bytes.
* This is needed since a spend from a p2wpkh to an `OP_RETURN` without
* any other outputs would result in a transaction smaller than the
* minimum size. */
u8 *scriptpubkey_opreturn_padded(const tal_t *ctx);

/* Create an input script which spends p2pkh */
u8 *bitcoin_redeem_p2pkh(const tal_t *ctx, const struct pubkey *pubkey,
const struct bitcoin_signature *sig);
Expand Down
16 changes: 8 additions & 8 deletions bitcoin/test/run-secret_eq_consttime.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static struct timerel const_time_test(struct secret *s1,
struct secret *s2,
size_t off)
{
struct timeabs start, end;
struct timemono start, end;
int result = 0;

memset(s1, 0, RUNS * sizeof(*s1));
Expand All @@ -31,16 +31,16 @@ static struct timerel const_time_test(struct secret *s1,
for (size_t i = 0; i < RUNS; i++)
s2[i].data[off] = i;

start = time_now();
start = time_mono();
for (size_t i = 0; i < RUNS; i++)
result += secret_eq_consttime(&s1[i], &s2[i]);
end = time_now();
end = time_mono();

if (result != RUNS / 256)
errx(1, "Expected %u successes at offset %zu, not %u!",
RUNS / 256, off, result);

return time_between(end, start);
return timemono_between(end, start);
}

static inline bool secret_eq_nonconst(const struct secret *a,
Expand All @@ -53,7 +53,7 @@ static struct timerel nonconst_time_test(struct secret *s1,
struct secret *s2,
size_t off)
{
struct timeabs start, end;
struct timemono start, end;
int result = 0;

memset(s1, 0, RUNS * sizeof(*s1));
Expand All @@ -62,16 +62,16 @@ static struct timerel nonconst_time_test(struct secret *s1,
for (size_t i = 0; i < RUNS; i++)
s2[i].data[off] = i;

start = time_now();
start = time_mono();
for (size_t i = 0; i < RUNS; i++)
result += secret_eq_nonconst(&s1[i], &s2[i]);
end = time_now();
end = time_mono();

if (result != RUNS / 256)
errx(1, "Expected %u successes at offset %zu, not %u!",
RUNS / 256, off, result);

return time_between(end, start);
return timemono_between(end, start);
}

static struct secret *s1, *s2;
Expand Down
14 changes: 3 additions & 11 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
/* Needs to be at end, since it doesn't include its own hdrs */
#include "full_channel_error_names_gen.h"

static void memleak_help_htlcmap(struct htable *memtable,
struct htlc_map *htlcs)
{
memleak_scan_htable(memtable, &htlcs->raw);
}

/* This is a dangerous thing! Because we apply HTLCs in many places
* in bulk, we can temporarily go negative. You must check balance_ok()
* at the end! */
Expand Down Expand Up @@ -113,11 +107,9 @@ struct channel *new_full_channel(const tal_t *ctx,
option_wumbo,
opener);

if (channel) {
channel->htlcs = tal(channel, struct htlc_map);
htlc_map_init(channel->htlcs);
memleak_add_helper(channel->htlcs, memleak_help_htlcmap);
}
if (channel)
channel->htlcs = new_htable(channel, htlc_map);

return channel;
}

Expand Down
1 change: 1 addition & 0 deletions cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ LIGHTNING_CLI_COMMON_OBJS := \
common/configdir.o \
common/configvar.o \
common/json_parse_simple.o \
common/memleak.o \
common/status_levels.o \
common/utils.o \
common/version.o
Expand Down
4 changes: 2 additions & 2 deletions common/configdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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>

Expand All @@ -36,8 +37,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)
Expand Down
12 changes: 5 additions & 7 deletions common/memleak.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions common/memleak.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ void memleak_scan_region(struct htable *memtable, const void *p, size_t len);
/* Objects inside this htable (which is opaque to memleak) are not leaks. */
void memleak_scan_htable(struct htable *memtable, const struct htable *ht);

/* Allocate a htable, set up memleak scan automatically (assumes &p->raw == p) */
#define new_htable(ctx, type) \
({ \
const struct htable *raw; \
struct type *p = tal(ctx, struct type); \
type##_init(p); \
raw = &p->raw; \
assert((void *)raw == (void *)p); \
memleak_add_helper(raw, memleak_scan_htable); \
p; \
})

/* Objects inside this uintmap (which is opaque to memleak) are not leaks. */
#define memleak_scan_uintmap(memtable, umap) \
memleak_scan_intmap_(memtable, uintmap_unwrap_(umap))
Expand Down
6 changes: 1 addition & 5 deletions common/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,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");
Expand Down
27 changes: 7 additions & 20 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2021,9 +2021,6 @@ static void dev_connect_memleak(struct daemon *daemon, const u8 *msg)

/* Now delete daemon and those which it has pointers to. */
memleak_scan_obj(memtable, daemon);
memleak_scan_htable(memtable, &daemon->peers->raw);
memleak_scan_htable(memtable, &daemon->scid_htable->raw);
memleak_scan_htable(memtable, &daemon->important_ids->raw);

found_leak = dump_memleak(memtable, memleak_status_broken, NULL);
daemon_conn_send(daemon->master,
Expand Down Expand Up @@ -2437,14 +2434,6 @@ static struct io_plan *recv_gossip(struct io_conn *conn,
return daemon_conn_read_next(conn, daemon->gossipd);
}

/*~ This is a hook used by the memleak code: it can't see pointers
* inside hash tables, so we give it a hint here. */
static void memleak_daemon_cb(struct htable *memtable, struct daemon *daemon)
{
memleak_scan_htable(memtable, &daemon->peers->raw);
memleak_scan_htable(memtable, &daemon->connecting->raw);
}

static void gossipd_failed(struct daemon_conn *gossipd)
{
status_failed(STATUS_FAIL_GOSSIP_IO, "gossipd exited?");
Expand All @@ -2464,14 +2453,13 @@ int main(int argc, char *argv[])
daemon = tal(NULL, struct daemon);
daemon->developer = developer;
daemon->connection_counter = 1;
daemon->peers = tal(daemon, struct peer_htable);
/* htable_new is our helper which allocates a htable, initializes it
* and set up the memleak callback so our memleak code can see objects
* inside it */
daemon->peers = new_htable(daemon, peer_htable);
daemon->listeners = tal_arr(daemon, struct io_listener *, 0);
peer_htable_init(daemon->peers);
memleak_add_helper(daemon, memleak_daemon_cb);
daemon->connecting = tal(daemon, struct connecting_htable);
connecting_htable_init(daemon->connecting);
daemon->important_ids = tal(daemon, struct important_id_htable);
important_id_htable_init(daemon->important_ids);
daemon->connecting = new_htable(daemon, connecting_htable);
daemon->important_ids = new_htable(daemon, important_id_htable);
timers_init(&daemon->timers, time_mono());
daemon->gossmap_raw = NULL;
daemon->shutting_down = false;
Expand All @@ -2481,8 +2469,7 @@ int main(int argc, char *argv[])
daemon->dev_exhausted_fds = false;
/* We generally allow 1MB per second per peer, except for dev testing */
daemon->gossip_stream_limit = 1000000;
daemon->scid_htable = tal(daemon, struct scid_htable);
scid_htable_init(daemon->scid_htable);
daemon->scid_htable = new_htable(daemon, scid_htable);

/* stdin == control */
daemon->master = daemon_conn_new(daemon, STDIN_FILENO, recv_req, NULL,
Expand Down
Loading
Loading