Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#28470: fuzz: Rework addr fuzzing
Browse files Browse the repository at this point in the history
fad52ba fuzz: Rework addr fuzzing (MarcoFalke)
fa5b6d2 fuzz: Drop unused params from serialize helpers (MarcoFalke)

Pull request description:

  Some minor fixups to addr fuzzing

ACKs for top commit:
  dergoegge:
    utACK fad52ba

Tree-SHA512: 6a2b07fb1a65cf855d5e7c0a52bfcb81d46dbc5d4b3e72cef359987cbd28dbfeb2fc54f210e9737cb131b40ac5f88a90e9af284e441e0b37196121590bbaf015
  • Loading branch information
fanquake committed Sep 20, 2023
2 parents abe4fed + fad52ba commit 1bf915d
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/test/fuzz/deserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ void DeserializeFromFuzzingInput(FuzzBufferType buffer, T&& obj, const P& params
}

template <typename T>
CDataStream Serialize(const T& obj, const int version = INIT_PROTO_VERSION, const int ser_type = SER_NETWORK)
CDataStream Serialize(const T& obj)
{
CDataStream ds(ser_type, version);
CDataStream ds{SER_NETWORK, INIT_PROTO_VERSION};
ds << obj;
return ds;
}
Expand All @@ -107,12 +107,10 @@ T Deserialize(CDataStream ds)
}

template <typename T>
void DeserializeFromFuzzingInput(FuzzBufferType buffer, T&& obj, const std::optional<int> protocol_version = std::nullopt, const int ser_type = SER_NETWORK)
void DeserializeFromFuzzingInput(FuzzBufferType buffer, T&& obj)
{
CDataStream ds(buffer, ser_type, INIT_PROTO_VERSION);
if (protocol_version) {
ds.SetVersion(*protocol_version);
} else {
CDataStream ds{buffer, SER_NETWORK, INIT_PROTO_VERSION};
{
try {
int version;
ds >> version;
Expand All @@ -135,9 +133,9 @@ void AssertEqualAfterSerializeDeserialize(const T& obj, const P& params)
assert(Deserialize<T>(Serialize(obj, params), params) == obj);
}
template <typename T>
void AssertEqualAfterSerializeDeserialize(const T& obj, const int version = INIT_PROTO_VERSION, const int ser_type = SER_NETWORK)
void AssertEqualAfterSerializeDeserialize(const T& obj)
{
assert(Deserialize<T>(Serialize(obj, version, ser_type)) == obj);
assert(Deserialize<T>(Serialize(obj)) == obj);
}

} // namespace
Expand Down Expand Up @@ -254,7 +252,7 @@ FUZZ_TARGET(netaddr_deserialize, .init = initialize_deserialize)
if (!maybe_na) return;
const CNetAddr& na{*maybe_na};
if (na.IsAddrV1Compatible()) {
AssertEqualAfterSerializeDeserialize(na, ConsumeDeserializationParams<CNetAddr::SerParams>(fdp));
AssertEqualAfterSerializeDeserialize(na, CNetAddr::V1);
}
AssertEqualAfterSerializeDeserialize(na, CNetAddr::V2);
}
Expand All @@ -266,7 +264,7 @@ FUZZ_TARGET(service_deserialize, .init = initialize_deserialize)
if (!maybe_s) return;
const CService& s{*maybe_s};
if (s.IsAddrV1Compatible()) {
AssertEqualAfterSerializeDeserialize(s, ConsumeDeserializationParams<CNetAddr::SerParams>(fdp));
AssertEqualAfterSerializeDeserialize(s, CNetAddr::V1);
}
AssertEqualAfterSerializeDeserialize(s, CNetAddr::V2);
if (ser_params.enc == CNetAddr::Encoding::V1) {
Expand All @@ -281,8 +279,8 @@ FUZZ_TARGET_DESERIALIZE(messageheader_deserialize, {
FUZZ_TARGET(address_deserialize, .init = initialize_deserialize)
{
FuzzedDataProvider fdp{buffer.data(), buffer.size()};
const auto ser_enc{ConsumeDeserializationParams<CNetAddr::SerParams>(fdp)};
const auto maybe_a{ConsumeDeserializable<CAddress>(fdp, CAddress::SerParams{{ser_enc}, CAddress::Format::Network})};
const auto ser_enc{ConsumeDeserializationParams<CAddress::SerParams>(fdp)};
const auto maybe_a{ConsumeDeserializable<CAddress>(fdp, ser_enc)};
if (!maybe_a) return;
const CAddress& a{*maybe_a};
// A CAddress in V1 mode will roundtrip
Expand Down

0 comments on commit 1bf915d

Please sign in to comment.