Skip to content

tests: Add fuzzing harnesses for functions in script/ - #18994

Merged
maflcko merged 7 commits into
bitcoin:masterfrom
practicalswift:fuzzers-script-slash
May 31, 2020
Merged

maflcko merged 7 commits into
bitcoin:masterfrom
practicalswift:fuzzers-script-slash

Conversation

@practicalswift

Copy link
Copy Markdown
Contributor

Add fuzzing harnesses for functions in script/:

  • Add fuzzing helper functions ConsumeDataStream and ConsumeUInt160
  • Fill fuzzing coverage gaps for functions in script/script.h, script/script_error.h and script/standard.h
  • Add fuzzing harness for functions in script/bitcoinconsensus.h
  • Add fuzzing harness for functions in script/descriptor.h
  • Add fuzzing harness for functions in script/interpreter.h
  • Add fuzzing harness for functions in script/sigcache.h
  • Add fuzzing harness for functions in script/sign.h

See doc/fuzzing.md for information on how to fuzz Bitcoin Core. Don't forget to contribute any coverage increasing inputs you find to the Bitcoin Core fuzzing corpus repo.

Happy fuzzing :)

@fanquake fanquake added the Tests label May 17, 2020
@DrahtBot

DrahtBot commented May 17, 2020 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

@maflcko maflcko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

Comment thread src/test/fuzz/script.cpp Resolved
Comment thread src/test/fuzz/script.cpp Resolved
Comment thread src/test/fuzz/script_sign.cpp Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated, but I was wondering if ConsumeDeserializable<T> could always return type T (if the stream was empty, it would return T{}). Not returning optional would bring this helper in line with all other helpers. For example ConsumeRandomLengthString always return std::string.

@practicalswift practicalswift May 18, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean something along the lines of:

template <typename T>
NODISCARD inline T ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096) noexcept
{
    const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
    CDataStream ds{buffer, SER_NETWORK, INIT_PROTO_VERSION};
    T obj;
    try {
        ds >> obj;
    } catch (const std::ios_base::failure&) {
    }
    return obj;
}

I've thought about that and while it would be better from a developer ergonomics perspective I think it might come with some negative impact on overall fuzzing. One risk I see is that the fuzzer might try to work also on "meaningless" inputs (inputs which cause std::ios_base::failure) which would now become "somewhat meaningful" due to default construction.

Take the extreme example of a huge input containing a few million bytes worth of 0x41 scream (AAAAAAAAAAA…) being deserialized to an object of type CFoo. The current fuzzer would quickly reach std::ios_base::failure, return a nullopt and then give up without further processing (fail early thanks to if (!opt_foo) { return; } or similar). If we were to return a CFoo{} instead then the fuzzer would proceed with processing (fail late) which may slow down the fuzzing.

Does it make sense? :)

tl;dr -- would be worth doing if no negative fuzzing speed impact can be measured :)

Edit: Proof of concept here if someone wants to experiment :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think when it comes to trading off giving the fuzz test more freedom to explore more code branches vs micro-optimizing for performance, we should always pick the option that gives the fuzz engine more freedom. Fuzzing is great to complement traditional tests (e.g. our unit and functional tests). Those traditional tests offer a reasonable line coverage, but fuzz testing can offer great branch coverage. Giving the fuzz engine to explore as much branches as possible is vital. With modern coverage guided fuzz engines, the strength of fuzzing is that even with a large search space, the fuzz engine will always find useful inputs that increase coverage. Additional search space may (or may not!) come with a performance penalty, but I think we shouldn't use performance as a way to measure usefulness of a fuzz tests. The fastest fuzz test is the one that tests nothing. However, it is also the most useless one. Anything that can be solved by letting the fuzz engine run for another hour or by simply adding one more core should not be a reason to cripple the test into one that yields less coverage.

In fact, this has shown to be effective in the past. Take for example the process_message harness vs the process_messages harness. The latter is allowed to send an arbitrary number of messages and start an arbitrary number of peers. The latter (more flexible and broader) test that gives the engine more freedom is also the only test that has found bugs in pull requests that none of the other fuzz test or unit test or even functional tests have found. Specifically:

@practicalswift practicalswift May 18, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree regarding the coverage vs performance trade-off: I'd choose "explore more code branches" over performance any day in the week :)

Do you see a scenario where returning a default constructed object in case of serialisation failure is likely to give more freedom to explore more code branches (in a way that will have a positive impact of coverage) compared to not doing so?

If so we'd have two reasons to do this: developer ergonomics (no more std::optional) and increased coverage :)

My thinking is more along the lines of "how do we guide the coverage-guided fuzzer in a meaningful way" rather than "micro-optimizing for performance". I guess the question boils down to if and to what extent the guidance provided by failing early is meaningful for the fuzzer :)

Code if anyone wants to experiment: master...practicalswift:consume

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you see a scenario where returning a default constructed object in case of serialisation failure is likely to give more freedom to explore more code branches compared to not doing so?

Unclear. This question boils down to whether default initialization leaves members uninitialized or initializes them with values that can not be the result of deserialization.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Sounds like we agree on the need for experimentation and measuring before proceeding with this :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@practicalswift Are you going to create a pull with the branch or should I do it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoFalke I plan to do a FuzzBench style test between the two variations (T ConsumeDeserializable(…) and std::optional<T> ConsumeDeserializable(…)) to see which one that helps us reach the highest coverage after 24 hours of fuzzing using libFuzzer (with each harness given 1/N slice of the runtime) with an empty input corpus. Does that sound like a fair experimental setup that could guide us regarding the trade-offs we're facing here? If so, I'll do it :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can't hurt to do a benchmark

@maflcko

maflcko commented May 29, 2020

Copy link
Copy Markdown
Member

Needs rebase

@practicalswift
practicalswift force-pushed the fuzzers-script-slash branch from 94355b1 to f898ef6 Compare May 30, 2020 10:41
@maflcko

maflcko commented May 31, 2020

Copy link
Copy Markdown
Member

ACK f898ef6 🔉

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK f898ef65c947776750e49d050633f830546bbdc6 🔉
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUgtcwwAk2++KR5/UnaZ6/Gls4rZq7sjVolrOPUw/ADuxRyFu/qM7nmAOuzhX645
/tuQPOszycpK7MuS8INKbw2gV4TTP35W7N9z8GhtbyaSlRXzBGF5vLGk0P1diJrp
GwSiwUle6VU4sRLX44DctSgP9NvNMBkl+zqFEJpjspXkGZuYvtDJrP4Ipm+oSNOw
d5K0zg6VKwz/+23qt9OJErovRltC7QUxIecI+Y0KmJzfkpScf+jGoMTFgo9q0zC4
JDLJ0ZAU8BeiN2J/IK0yyxG1LhZPOK1LikFWqebeD4JEOSA+FnWopXHCUAj+sL7H
hRZmjH1CJ8my2d2nOdqCmOn1R3K+kEuVB0L2cMpPsISRcPjgI8GlF4RcY23PpTBJ
FtjGQdg30fzpNeLW0aPvti4RV7e8eDsi87BOWOVpNKY6AqeKWWcgJwNejrQREfNY
/xHzGR2zvhFYcO7x5V2lafPHaB9CT6lxaRBsNFYfQf7ajaNGeHKXhjHsnQnlW46a
ELWIGbLJ
=55wg
-----END PGP SIGNATURE-----

Timestamp of file with hash 6d0233da06adba4dcb766f9a62ac736cf11c827239891f0b3af49ea1152fcac1 -

@maflcko
maflcko merged commit a65b55f into bitcoin:master May 31, 2020
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 29, 2021
Summary:
```
 - Add fuzzing helper functions ConsumeDataStream and ConsumeUInt160
 - Fill fuzzing coverage gaps for functions in script/script.h,
   script/script_error.h and script/standard.h
 - Add fuzzing harness for functions in script/bitcoinconsensus.h
 - Add fuzzing harness for functions in script/descriptor.h
 - Add fuzzing harness for functions in script/interpreter.h
 - Add fuzzing harness for functions in script/sigcache.h
 - Add fuzzing harness for functions in script/sign.h
```

Backport of core [[bitcoin/bitcoin#18994 | PR18994]].

Test Plan:
  ninja bitcoin-fuzzers
  ./test/fuzz/test_runner.py <path_to_corpus>

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Differential Revision: https://reviews.bitcoinabc.org/D9111
@practicalswift
practicalswift deleted the fuzzers-script-slash branch April 10, 2021 19:41
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants