-
Notifications
You must be signed in to change notification settings - Fork 942
fuzz-tests: Add a test for calculate_our_funding()
#8312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
`�1^ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
�1� | ||
0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4������������������������������������������������������������������������������������ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
|
||
���������������������������������������������������������������������������������� |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,166 @@ | ||||||
#include "config.h" | ||||||
#include <ccan/array_size/array_size.h> | ||||||
#include <common/setup.h> | ||||||
#include <wire/wire.h> | ||||||
#include <stdio.h> | ||||||
#include <tests/fuzz/libfuzz.h> | ||||||
|
||||||
#include "../../plugins/funder_policy.c" | ||||||
|
||||||
/* AUTOGENERATED MOCKS START */ | ||||||
/* Generated stub for json_add_string */ | ||||||
void json_add_string(struct json_stream *js UNNEEDED, | ||||||
const char *fieldname UNNEEDED, | ||||||
const char *str TAKES UNNEEDED) | ||||||
{ fprintf(stderr, "json_add_string called!\n"); abort(); } | ||||||
/* AUTOGENERATED MOCKS END */ | ||||||
|
||||||
#define MAX_BTC ((u32)WALLY_SATOSHI_PER_BTC * WALLY_BTC_MAX) | ||||||
|
||||||
struct test_case { | ||||||
struct amount_sat their_funds; | ||||||
struct amount_sat available_funds; | ||||||
struct amount_sat *our_last_funds; | ||||||
struct amount_sat channel_size; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the max channel size allowed.
Suggested change
|
||||||
struct amount_sat lease_request; | ||||||
struct funder_policy policy; | ||||||
struct amount_sat exp_our_funds; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
}; | ||||||
|
||||||
static struct amount_sat fromwire_amount_sat_bounded(const u8 **cursor, size_t *max) | ||||||
{ | ||||||
struct amount_sat amt = fromwire_amount_sat(cursor, max); | ||||||
amt.satoshis %= (MAX_BTC + 1); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: whitespace
Suggested change
|
||||||
return amt; | ||||||
} | ||||||
|
||||||
void init(int *argc, char ***argv) | ||||||
{} | ||||||
|
||||||
void run(const u8 *data, size_t size) | ||||||
{ | ||||||
if (size < 85) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||||||
return; | ||||||
|
||||||
struct test_case tcase; | ||||||
tcase.their_funds = fromwire_amount_sat_bounded(&data, &size); | ||||||
tcase.available_funds = fromwire_amount_sat_bounded(&data, &size); | ||||||
|
||||||
/* Read flag for our_last_funds */ | ||||||
if (size < 1) | ||||||
return; | ||||||
u8 flag = fromwire_u8(&data, &size); | ||||||
|
||||||
/* Handle our_last_funds conditionally */ | ||||||
struct amount_sat our_last_funds_val; | ||||||
if (flag) | ||||||
{ | ||||||
if (size < 8) | ||||||
return; | ||||||
our_last_funds_val = fromwire_amount_sat_bounded(&data, &size); | ||||||
tcase.our_last_funds = &our_last_funds_val; | ||||||
} | ||||||
else | ||||||
tcase.our_last_funds = NULL; | ||||||
|
||||||
tcase.channel_size = fromwire_amount_sat_bounded(&data, &size); | ||||||
tcase.lease_request = fromwire_amount_sat_bounded(&data, &size); | ||||||
|
||||||
if (size < 1) | ||||||
return; | ||||||
tcase.policy.opt = (enum funder_opt)(fromwire_u8(&data, &size) % 3); | ||||||
tcase.policy.mod = fromwire_u64(&data, &size) % (MAX_BTC + 1); | ||||||
tcase.policy.min_their_funding = fromwire_amount_sat_bounded(&data, &size); | ||||||
tcase.policy.max_their_funding = fromwire_amount_sat_bounded(&data, &size); | ||||||
|
||||||
if (amount_sat_greater(tcase.policy.min_their_funding, tcase.policy.max_their_funding)) { | ||||||
struct amount_sat tmp = tcase.policy.min_their_funding; | ||||||
tcase.policy.min_their_funding = tcase.policy.max_their_funding; | ||||||
tcase.policy.max_their_funding = tmp; | ||||||
} | ||||||
|
||||||
tcase.policy.per_channel_max = fromwire_amount_sat_bounded(&data, &size); | ||||||
tcase.policy.per_channel_min = fromwire_amount_sat_bounded(&data, &size); | ||||||
|
||||||
if (amount_sat_greater(tcase.policy.per_channel_min, tcase.policy.per_channel_max)) { | ||||||
struct amount_sat tmp = tcase.policy.per_channel_min; | ||||||
tcase.policy.per_channel_min = tcase.policy.per_channel_max; | ||||||
tcase.policy.per_channel_max = tmp; | ||||||
} | ||||||
|
||||||
if (size < 1) | ||||||
return; | ||||||
|
||||||
tcase.policy.fuzz_factor = fromwire_u8(&data, &size) % 101; | ||||||
tcase.policy.reserve_tank = fromwire_amount_sat_bounded(&data, &size); | ||||||
|
||||||
if (size < 1) | ||||||
return; | ||||||
|
||||||
tcase.policy.fund_probability = fromwire_u8(&data, &size) % 101; | ||||||
|
||||||
if (size < 1) | ||||||
return; | ||||||
|
||||||
tcase.policy.leases_only = fromwire_u8(&data, &size) & 1; | ||||||
|
||||||
struct node_id id; | ||||||
memset(&id, 1, sizeof(id)); | ||||||
const char *err; | ||||||
struct amount_sat our_funds; | ||||||
|
||||||
/* Call the function under test */ | ||||||
err = calculate_our_funding(&tcase.policy, id, | ||||||
tcase.their_funds, | ||||||
tcase.our_last_funds, | ||||||
tcase.available_funds, | ||||||
tcase.channel_size, | ||||||
tcase.lease_request, | ||||||
&our_funds); | ||||||
|
||||||
/* Validate invariants */ | ||||||
if (err) | ||||||
tal_free(err); | ||||||
else { | ||||||
/* Check total doesn't exceed channel_size */ | ||||||
struct amount_sat total; | ||||||
if (!amount_sat_add(&total, tcase.their_funds, our_funds)) { | ||||||
fprintf(stderr, "Overflow in total channel capacity\n"); | ||||||
abort(); | ||||||
} | ||||||
if (amount_sat_greater(total, tcase.channel_size)) { | ||||||
fprintf(stderr, "Total channel capacity %"PRIu64" exceeds size %"PRIu64"\n", | ||||||
total.satoshis, tcase.channel_size.satoshis); | ||||||
abort(); | ||||||
} | ||||||
|
||||||
/* Check our_funds is within per-channel limits */ | ||||||
if (amount_sat_less(our_funds, tcase.policy.per_channel_min) && | ||||||
!amount_sat_is_zero(our_funds)) { | ||||||
fprintf(stderr, "our_funds %"PRIu64" < per_channel_min %"PRIu64"\n", | ||||||
our_funds.satoshis, tcase.policy.per_channel_min.satoshis); | ||||||
abort(); | ||||||
} | ||||||
if (amount_sat_greater(our_funds, tcase.policy.per_channel_max)) { | ||||||
fprintf(stderr, "our_funds %"PRIu64" > per_channel_size %"PRIu64"\n", | ||||||
our_funds.satoshis, tcase.policy.per_channel_max.satoshis); | ||||||
abort(); | ||||||
} | ||||||
} | ||||||
|
||||||
/* Check available funds constraint */ | ||||||
struct amount_sat available_minus_reserve; | ||||||
if (amount_sat_sub(&available_minus_reserve, tcase.available_funds, tcase.policy.reserve_tank)) { | ||||||
if (amount_sat_greater(our_funds, available_minus_reserve)) { | ||||||
fprintf(stderr, "our_funds %"PRIu64" > available %"PRIu64" - reserve %"PRIu64"\n", | ||||||
our_funds.satoshis, tcase.available_funds.satoshis, | ||||||
tcase.policy.reserve_tank.satoshis); | ||||||
abort(); | ||||||
} | ||||||
} else if (!amount_sat_eq(our_funds, AMOUNT_SAT(0))) { | ||||||
fprintf(stderr, "Reserve %"PRIu64" >= available %"PRIu64" but our_funds %"PRIu64" != 0\n", | ||||||
tcase.policy.reserve_tank.satoshis, tcase.available_funds.satoshis, | ||||||
our_funds.satoshis); | ||||||
abort(); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure this multiplication overflows
u32
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this over from
tests/fuzz/fuzz-close_tx.c
:Should I fix it there as well? Possibly by making this small change: