Skip to content
Merged
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
26 changes: 26 additions & 0 deletions openingd/dualopend.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,8 @@ static bool is_dust(struct tx_state *tx_state,
|| amount_sat_less(amount, tx_state->remoteconf.dust_limit);
}

static void handle_dev_memleak(struct state *state, const u8 *msg);

static char *validate_inputs(struct state *state,
struct tx_state *tx_state,
enum tx_role role_to_validate)
Expand All @@ -662,6 +664,10 @@ static char *validate_inputs(struct state *state,
role_to_validate);
wire_sync_write(REQ_FD, take(msg));
msg = wire_sync_read(tmpctx, REQ_FD);
while (state->developer && fromwire_dualopend_dev_memleak(msg)) {
handle_dev_memleak(state, msg);
msg = wire_sync_read(tmpctx, REQ_FD);
}

if (!fromwire_dualopend_validate_inputs_reply(msg)) {
if (!fromwire_dualopend_fail(tmpctx, msg, &err_reason))
Expand Down Expand Up @@ -2392,6 +2398,10 @@ static u8 *accepter_commits(struct state *state,

wire_sync_write(REQ_FD, take(msg));
msg = wire_sync_read(tmpctx, REQ_FD);
while (state->developer && fromwire_dualopend_dev_memleak(msg)) {
handle_dev_memleak(state, msg);
msg = wire_sync_read(tmpctx, REQ_FD);
}

if (fromwire_peektype(msg) != WIRE_DUALOPEND_COMMIT_SEND_ACK)
master_badmsg(WIRE_DUALOPEND_COMMIT_SEND_ACK, msg);
Expand Down Expand Up @@ -2437,6 +2447,10 @@ static u8 *accepter_commits(struct state *state,
tal_free(pbase);
wire_sync_write(REQ_FD, take(msg));
msg = wire_sync_read(tmpctx, REQ_FD);
while (state->developer && fromwire_dualopend_dev_memleak(msg)) {
handle_dev_memleak(state, msg);
msg = wire_sync_read(tmpctx, REQ_FD);
}

if (fromwire_peektype(msg) != WIRE_DUALOPEND_SEND_TX_SIGS)
master_badmsg(WIRE_DUALOPEND_SEND_TX_SIGS, msg);
Expand Down Expand Up @@ -2693,6 +2707,10 @@ static void accepter_start(struct state *state, const u8 *oc2_msg)

wire_sync_write(REQ_FD, take(msg));
msg = wire_sync_read(tmpctx, REQ_FD);
while (state->developer && fromwire_dualopend_dev_memleak(msg)) {
handle_dev_memleak(state, msg);
msg = wire_sync_read(tmpctx, REQ_FD);
}

if ((msg_type = fromwire_peektype(msg)) == WIRE_DUALOPEND_FAIL) {
if (!fromwire_dualopend_fail(msg, msg, &err_reason))
Expand Down Expand Up @@ -3067,6 +3085,10 @@ static u8 *opener_commits(struct state *state,

wire_sync_write(REQ_FD, take(msg));
msg = wire_sync_read(tmpctx, REQ_FD);
while (state->developer && fromwire_dualopend_dev_memleak(msg)) {
handle_dev_memleak(state, msg);
msg = wire_sync_read(tmpctx, REQ_FD);
}

if (fromwire_peektype(msg) != WIRE_DUALOPEND_COMMIT_SEND_ACK)
master_badmsg(WIRE_DUALOPEND_COMMIT_SEND_ACK, msg);
Expand Down Expand Up @@ -3902,6 +3924,10 @@ static void rbf_remote_start(struct state *state, const u8 *rbf_msg)

wire_sync_write(REQ_FD, take(msg));
msg = wire_sync_read(tmpctx, REQ_FD);
while (state->developer && fromwire_dualopend_dev_memleak(msg)) {
handle_dev_memleak(state, msg);
msg = wire_sync_read(tmpctx, REQ_FD);
}

if ((msg_type = fromwire_peektype(msg)) == WIRE_DUALOPEND_FAIL) {
if (!fromwire_dualopend_fail(msg, msg, &err_reason))
Expand Down
6 changes: 6 additions & 0 deletions openingd/openingd.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ struct state {
bool dev_accept_any_channel_type;
};

static void handle_dev_memleak(struct state *state, const u8 *msg);

/*~ If we can't agree on parameters, we fail to open the channel.
* Tell lightningd why. */
static void NORETURN negotiation_aborted(struct state *state, const char *why)
Expand Down Expand Up @@ -1028,6 +1030,10 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
state->channel_type);
wire_sync_write(REQ_FD, take(msg));
msg = wire_sync_read(tmpctx, REQ_FD);
while (state->developer && fromwire_openingd_dev_memleak(msg)) {
handle_dev_memleak(state, msg);
msg = wire_sync_read(tmpctx, REQ_FD);
}

/* We don't allocate off tmpctx, because that's freed inside
* opening_negotiate_msg */
Expand Down
44 changes: 43 additions & 1 deletion tests/test_opening.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
only_one, wait_for, sync_blockheight, first_channel_id, calc_lease_fee, check_coin_moves,
scriptpubkey_addr, TIMEOUT
)
from pyln.testing.utils import FUNDAMOUNT
from pyln.testing.utils import FUNDAMOUNT, VALGRIND
from bitcoin.rpc import JSONRPCError

from pathlib import Path
Expand Down Expand Up @@ -4226,3 +4226,45 @@ def test_duplicate_channel_id_in_db(node_factory, bitcoind):
# And we can still start with both in the db.
l2.restart()
assert [c['channel_id'] for c in l2.rpc.listclosedchannels()['closedchannels']] == [cid, cid]


@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(VALGRIND, "dev-memleak is disabled under VALGRIND")
def test_openingd_dev_memleak_during_hook(node_factory, executor):
release_file = os.path.join(node_factory.directory, 'release_openchannel')
plugin_path = os.path.join(node_factory.directory, 'hold_openchannel.py')
with open(plugin_path, 'w') as f:
f.write(
'#!/usr/bin/env python3\n'
'import os, time\n'
'from pyln.client import Plugin\n'
'plugin = Plugin()\n'
'def _wait():\n'
' plugin.log("openchannel hook waiting")\n'
f' while not os.path.exists({release_file!r}):\n'
' time.sleep(0.05)\n'
' return {"result": "continue"}\n'
'@plugin.hook("openchannel")\n'
'def on_oc(openchannel, **kwargs):\n'
' return _wait()\n'
'@plugin.hook("openchannel2")\n'
'def on_oc2(openchannel2, **kwargs):\n'
' return _wait()\n'
'plugin.run()\n'
)
os.chmod(plugin_path, 0o755)

l1, l2 = node_factory.get_nodes(2, opts=[{}, {'plugin': plugin_path,
'disable-plugin': 'funder'}])
l1.fundwallet(10**6)
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)

fut = executor.submit(l1.rpc.fundchannel, l2.info['id'], 10**5)
try:
l2.daemon.wait_for_log('openchannel hook waiting')
leaks = l2.rpc.dev_memleak()['leaks']
assert leaks == []
finally:
open(release_file, 'w').close()
fut.result()
Loading