-
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?
Conversation
Hey @morehouse , this test fails with the error:
but the function under test,
What could be the cause? I tried inspecting the breakage using gdb but the values upon inspection seem to vary from the error output. I've added the breaking input as the corpus, I'd appreciate your thoughts on this. |
When it fails, is The check is skipped when the returned amount is 0. lightning/plugins/funder_policy.c Lines 297 to 299 in ad2dc97
|
01b2f27
to
776eea0
Compare
This test is now ready to review. |
tests/fuzz/fuzz-funder-policy.c
Outdated
{ fprintf(stderr, "json_add_string called!\n"); abort(); } | ||
/* AUTOGENERATED MOCKS END */ | ||
|
||
#define MAX_BTC ((u32)WALLY_SATOSHI_PER_BTC * WALLY_BTC_MAX) |
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
.
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
:
57 max = AMOUNT_SAT((u32)WALLY_SATOSHI_PER_BTC * WALLY_BTC_MAX);
Should I fix it there as well? Possibly by making this small change:
(u64)WALLY_SATOSHI_PER_BTC
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.
Yes, please create a separate PR for that fix and then run the fuzz target to make sure it still works.
tests/fuzz/fuzz-funder-policy.c
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: whitespace
amt.satoshis %= (MAX_BTC + 1); | |
amt.satoshis %= (MAX_BTC + 1); |
tests/fuzz/fuzz-funder-policy.c
Outdated
|
||
void run(const u8 *data, size_t size) | ||
{ | ||
if (size < 85) |
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.
Since data
is only ever read using fromwire*
functions, we probably don't need to check size
at all. If we run out of data
, fromwire
should automatically return 0 values.
tests/fuzz/fuzz-funder-policy.c
Outdated
struct amount_sat channel_size; | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
exp_our_funds
is unused.
tests/fuzz/fuzz-funder-policy.c
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
This is the max channel size allowed.
struct amount_sat channel_size; | |
struct amount_sat max_channel_size; |
Changelog-None: `calculate_our_funding()` in `plugins/funder_policy.c` is responsible for calculating our funding policy. Add a test for it.
Add a minimal input set as a seed corpus for the newly introduced test. This leads to discovery of interesting code paths faster.
Hey @morehouse, I have incorporated the above suggestions. Regarding the |
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.
This target leaks memory:
==280594== ERROR: libFuzzer: out-of-memory (used: 2067Mb; limit: 2048Mb)
To change the out-of-memory limit use -rss_limit_mb=<N>
Live Heap Allocations: 1131892148 bytes in 20471300 chunks; quarantined: 2801018
bytes in 29456 chunks; 1788983 other chunks; total chunks: 22289739; showing to
p 95% (at most 8 unique contexts)
I think clean_tmpctx
should fix it.
struct amount_sat our_last_funds_val; | ||
if (flag) | ||
{ | ||
our_last_funds_val = fromwire_amount_sat_bounded(&data, &size); | ||
tcase.our_last_funds = &our_last_funds_val; | ||
} |
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.
Nit: we can reduce the scope of our_last_funds_val
struct amount_sat our_last_funds_val; | |
if (flag) | |
{ | |
our_last_funds_val = fromwire_amount_sat_bounded(&data, &size); | |
tcase.our_last_funds = &our_last_funds_val; | |
} | |
if (flag) | |
{ | |
struct amount_sat our_last_funds_val = fromwire_amount_sat_bounded(&data, &size); | |
tcase.our_last_funds = &our_last_funds_val; | |
} |
tcase.policy.fuzz_factor = fromwire_u8(&data, &size) % 101; | ||
tcase.policy.reserve_tank = fromwire_amount_sat_bounded(&data, &size); | ||
tcase.policy.fund_probability = fromwire_u8(&data, &size) % 101; | ||
tcase.policy.leases_only = fromwire_u8(&data, &size) & 1; |
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.
Nit: we could improve readability by creating a helper function to populate tcase
for us.
|
||
/* Validate invariants */ | ||
if (err) | ||
tal_free(err); |
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.
It seems this isn't freeing all the memory that calculate_our_funding
allocates. Let's replace this with clean_tmpctx
on every iteration.
Checklist
Before submitting the PR, ensure the following tasks are completed. If an item is not applicable to your PR, please mark it as checked: