mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-29 10:19:26 +02:00
Merge bitcoin/bitcoin#22791: init: Fix asmap/addrman initialization order bug
724c497562
[fuzz] Add ConsumeAsmap() function (John Newbery)5840476714
[addrman] Make m_asmap private (John Newbery)f9002cb5db
[net] Rename the copyStats arg from m_asmap to asmap (John Newbery)f572f2b204
[addrman] Set m_asmap in CAddrMan initializer list (John Newbery)593247872d
[net] Remove CConnMan::SetAsmap() (John Newbery)50fd77045e
[init] Read/decode asmap before constructing addrman (John Newbery) Pull request description: Commit181a1207
introduced an initialization order bug: CAddrMan's m_asmap must be set before deserializing peers.dat. The first commit restores the correct initialization order. The remaining commits make `CAddrMan::m_asmap` usage safer: - don't reach into `CAddrMan`'s internal data from `CConnMan` - set `m_asmap` in the initializer list and make it const - make `m_asmap` private, and access it (as a reference to const) from a getter. This ensures that peers.dat deserialization must happen after setting m_asmap, since m_asmap is set during CAddrMan construction. ACKs for top commit: mzumsande: Tested ACK724c497562
amitiuttarwar: code review but utACK724c497562
naumenkogs: utACK724c497562
vasild: ACK724c497562
MarcoFalke: review ACK724c497562
👫 Tree-SHA512: 684a4cf9e3d4496c9997fb2bc4ec874809987055c157ec3fad1d2143b8223df52b5a0af787d028930b27388c8efeba0aeb2446cb35c337a5552ae76112ade726
This commit is contained in:
@ -28,17 +28,11 @@ class CAddrManDeterministic : public CAddrMan
|
||||
public:
|
||||
FuzzedDataProvider& m_fuzzed_data_provider;
|
||||
|
||||
explicit CAddrManDeterministic(FuzzedDataProvider& fuzzed_data_provider)
|
||||
: CAddrMan(/* deterministic */ true, /* consistency_check_ratio */ 0)
|
||||
explicit CAddrManDeterministic(std::vector<bool> asmap, FuzzedDataProvider& fuzzed_data_provider)
|
||||
: CAddrMan(std::move(asmap), /* deterministic */ true, /* consistency_check_ratio */ 0)
|
||||
, m_fuzzed_data_provider(fuzzed_data_provider)
|
||||
{
|
||||
WITH_LOCK(cs, insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
|
||||
if (fuzzed_data_provider.ConsumeBool()) {
|
||||
m_asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
|
||||
if (!SanityCheckASMap(m_asmap)) {
|
||||
m_asmap.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -224,11 +218,19 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] inline std::vector<bool> ConsumeAsmap(FuzzedDataProvider& fuzzed_data_provider) noexcept
|
||||
{
|
||||
std::vector<bool> asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
|
||||
if (!SanityCheckASMap(asmap)) asmap.clear();
|
||||
return asmap;
|
||||
}
|
||||
|
||||
FUZZ_TARGET_INIT(addrman, initialize_addrman)
|
||||
{
|
||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||
SetMockTime(ConsumeTime(fuzzed_data_provider));
|
||||
auto addr_man_ptr = std::make_unique<CAddrManDeterministic>(fuzzed_data_provider);
|
||||
std::vector<bool> asmap = ConsumeAsmap(fuzzed_data_provider);
|
||||
auto addr_man_ptr = std::make_unique<CAddrManDeterministic>(asmap, fuzzed_data_provider);
|
||||
if (fuzzed_data_provider.ConsumeBool()) {
|
||||
const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
|
||||
CDataStream ds(serialized_data, SER_DISK, INIT_PROTO_VERSION);
|
||||
@ -237,7 +239,7 @@ FUZZ_TARGET_INIT(addrman, initialize_addrman)
|
||||
try {
|
||||
ds >> *addr_man_ptr;
|
||||
} catch (const std::ios_base::failure&) {
|
||||
addr_man_ptr = std::make_unique<CAddrManDeterministic>(fuzzed_data_provider);
|
||||
addr_man_ptr = std::make_unique<CAddrManDeterministic>(asmap, fuzzed_data_provider);
|
||||
}
|
||||
}
|
||||
CAddrManDeterministic& addr_man = *addr_man_ptr;
|
||||
@ -306,9 +308,9 @@ FUZZ_TARGET_INIT(addrman_serdeser, initialize_addrman)
|
||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||
SetMockTime(ConsumeTime(fuzzed_data_provider));
|
||||
|
||||
CAddrManDeterministic addr_man1{fuzzed_data_provider};
|
||||
CAddrManDeterministic addr_man2{fuzzed_data_provider};
|
||||
addr_man2.m_asmap = addr_man1.m_asmap;
|
||||
std::vector<bool> asmap = ConsumeAsmap(fuzzed_data_provider);
|
||||
CAddrManDeterministic addr_man1{asmap, fuzzed_data_provider};
|
||||
CAddrManDeterministic addr_man2{asmap, fuzzed_data_provider};
|
||||
|
||||
CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION);
|
||||
|
||||
|
@ -25,7 +25,7 @@ FUZZ_TARGET_INIT(connman, initialize_connman)
|
||||
{
|
||||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
||||
SetMockTime(ConsumeTime(fuzzed_data_provider));
|
||||
CAddrMan addrman(/* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrMan addrman(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CConnman connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>(), addrman, fuzzed_data_provider.ConsumeBool()};
|
||||
CNetAddr random_netaddr;
|
||||
CNode random_node = ConsumeNode(fuzzed_data_provider);
|
||||
@ -103,12 +103,6 @@ FUZZ_TARGET_INIT(connman, initialize_connman)
|
||||
[&] {
|
||||
connman.RemoveAddedNode(random_string);
|
||||
},
|
||||
[&] {
|
||||
const std::vector<bool> asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
|
||||
if (SanityCheckASMap(asmap)) {
|
||||
connman.SetAsmap(asmap);
|
||||
}
|
||||
},
|
||||
[&] {
|
||||
connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
|
||||
},
|
||||
|
@ -21,6 +21,6 @@ FUZZ_TARGET_INIT(data_stream_addr_man, initialize_data_stream_addr_man)
|
||||
{
|
||||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
||||
CDataStream data_stream = ConsumeDataStream(fuzzed_data_provider);
|
||||
CAddrMan addr_man(/* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrMan addr_man(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrDB::Read(addr_man, data_stream);
|
||||
}
|
||||
|
@ -188,7 +188,7 @@ FUZZ_TARGET_DESERIALIZE(blockmerkleroot, {
|
||||
BlockMerkleRoot(block, &mutated);
|
||||
})
|
||||
FUZZ_TARGET_DESERIALIZE(addrman_deserialize, {
|
||||
CAddrMan am(/* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrMan am(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
DeserializeFromFuzzingInput(buffer, am);
|
||||
})
|
||||
FUZZ_TARGET_DESERIALIZE(blockheader_deserialize, {
|
||||
|
@ -43,7 +43,7 @@ FUZZ_TARGET_INIT(net, initialize_net)
|
||||
return;
|
||||
}
|
||||
CNodeStats stats;
|
||||
node.copyStats(stats, asmap);
|
||||
node.CopyStats(stats, asmap);
|
||||
},
|
||||
[&] {
|
||||
const CNode* add_ref_node = node.AddRef();
|
||||
|
Reference in New Issue
Block a user