Repository navigation
tests: Add fuzzing harnesses for functions in script/ - #18994
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsNo conflicts as of last run. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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:
- It found a nullptr dereference: [net processing] Drop unknown types in getdata #18808 (comment)
- It found an unidentified problem: Refactor and slightly stricter p2p message processing #15197 (review)
- Some more that I am missing...?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed. Sounds like we agree on the need for experimentation and measuring before proceeding with this :)
There was a problem hiding this comment.
@practicalswift Are you going to create a pull with the branch or should I do it?
There was a problem hiding this comment.
@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 :)
There was a problem hiding this comment.
Sure, can't hurt to do a benchmark
55778f1 to
e5958d3
Compare
e5958d3 to
94355b1
Compare
|
Needs rebase |
…nation and ConsumeUInt160
…cript/script_error.h and script/standard.h
94355b1 to
f898ef6
Compare
|
ACK f898ef6 🔉 Show signature and timestampSignature: Timestamp of file with hash |
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
Add fuzzing harnesses for functions in
script/:ConsumeDataStreamandConsumeUInt160script/script.h,script/script_error.handscript/standard.hscript/bitcoinconsensus.hscript/descriptor.hscript/interpreter.hscript/sigcache.hscript/sign.hSee
doc/fuzzing.mdfor 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 :)