This commit is contained in:
Fabian Jahr 2025-04-29 12:06:39 +02:00 committed by GitHub
commit 25f49ba8e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 297 additions and 186 deletions

View file

@ -137,6 +137,8 @@ if(WITH_ZMQ)
find_package(ZeroMQ 4.0.0 MODULE REQUIRED) find_package(ZeroMQ 4.0.0 MODULE REQUIRED)
endif() endif()
option(WITH_EMBEDDED_ASMAP "Embed default ASMap data." ON)
option(WITH_USDT "Enable tracepoints for Userspace, Statically Defined Tracing." OFF) option(WITH_USDT "Enable tracepoints for Userspace, Statically Defined Tracing." OFF)
if(WITH_USDT) if(WITH_USDT)
find_package(USDT MODULE REQUIRED) find_package(USDT MODULE REQUIRED)
@ -224,6 +226,7 @@ if(BUILD_FOR_FUZZING)
set(BUILD_GUI OFF) set(BUILD_GUI OFF)
set(ENABLE_EXTERNAL_SIGNER OFF) set(ENABLE_EXTERNAL_SIGNER OFF)
set(WITH_ZMQ OFF) set(WITH_ZMQ OFF)
set(WITH_EMBEDDED_ASMAP OFF)
set(BUILD_TESTS OFF) set(BUILD_TESTS OFF)
set(BUILD_GUI_TESTS OFF) set(BUILD_GUI_TESTS OFF)
set(BUILD_BENCH OFF) set(BUILD_BENCH OFF)
@ -692,6 +695,7 @@ else()
set(ipc_status OFF) set(ipc_status OFF)
endif() endif()
message(" IPC ................................. ${ipc_status}") message(" IPC ................................. ${ipc_status}")
message(" Embedded ASMap ...................... ${WITH_EMBEDDED_ASMAP}")
message(" USDT tracing ........................ ${WITH_USDT}") message(" USDT tracing ........................ ${WITH_USDT}")
message(" QR code (GUI) ....................... ${WITH_QRENCODE}") message(" QR code (GUI) ....................... ${WITH_QRENCODE}")
message(" DBus (GUI, Linux only) .............. ${WITH_DBUS}") message(" DBus (GUI, Linux only) .............. ${WITH_DBUS}")

View file

@ -12,4 +12,4 @@ export CI_IMAGE_NAME_TAG="mirror.gcr.io/debian:bookworm"
export PACKAGES="python3-zmq clang-16 llvm-16 libc++abi-16-dev libc++-16-dev" export PACKAGES="python3-zmq clang-16 llvm-16 libc++abi-16-dev libc++-16-dev"
export DEP_OPTS="NO_WALLET=1 CC=clang-16 CXX='clang++-16 -stdlib=libc++'" export DEP_OPTS="NO_WALLET=1 CC=clang-16 CXX='clang++-16 -stdlib=libc++'"
export GOAL="install" export GOAL="install"
export BITCOIN_CONFIG="-DREDUCE_EXPORTS=ON -DBUILD_UTIL_CHAINSTATE=ON -DBUILD_KERNEL_LIB=ON -DBUILD_SHARED_LIBS=ON" export BITCOIN_CONFIG="-DREDUCE_EXPORTS=ON -DBUILD_UTIL_CHAINSTATE=ON -DBUILD_KERNEL_LIB=ON -DBUILD_SHARED_LIBS=ON -DWITH_EMBEDDED_ASMAP=NO"

View file

@ -18,6 +18,5 @@ ${formatted_bytes}
}; };
inline constexpr std::span ${raw_source_basename}{detail_${raw_source_basename}_raw}; inline constexpr std::span ${raw_source_basename}{detail_${raw_source_basename}_raw};
} }")
")
file(WRITE ${HEADER_PATH} "${header_content}") file(WRITE ${HEADER_PATH} "${header_content}")

View file

@ -18,6 +18,9 @@ A Linux bash script that will set up traffic control (tc) to limit the outgoing
### [Seeds](/contrib/seeds) ### ### [Seeds](/contrib/seeds) ###
Utility to generate the pnSeed[] array that is compiled into the client. Utility to generate the pnSeed[] array that is compiled into the client.
### [ASMap](/contrib/asmap) ###
Utilities to analyze and process asmap files.
Build Tools and Keys Build Tools and Keys
--------------------- ---------------------

View file

@ -330,6 +330,13 @@ target_link_libraries(bitcoin_node
$<TARGET_NAME_IF_EXISTS:libevent::pthreads> $<TARGET_NAME_IF_EXISTS:libevent::pthreads>
$<TARGET_NAME_IF_EXISTS:USDT::headers> $<TARGET_NAME_IF_EXISTS:USDT::headers>
) )
if (WITH_EMBEDDED_ASMAP)
target_compile_definitions(bitcoin_node PRIVATE ENABLE_EMBEDDED_ASMAP=1)
include(TargetDataSources)
target_raw_data_sources(bitcoin_node NAMESPACE node::data
node/data/ip_asn.dat
)
endif()
# Bitcoin Core bitcoind. # Bitcoin Core bitcoind.

View file

@ -156,7 +156,7 @@ void AddrManImpl::Serialize(Stream& s_) const
* * for each new bucket: * * for each new bucket:
* * number of elements * * number of elements
* * for each element: index in the serialized "all new addresses" * * for each element: index in the serialized "all new addresses"
* * asmap checksum * * asmap version
* *
* 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it * 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it
* as incompatible. This is necessary because it did not check the version number on * as incompatible. This is necessary because it did not check the version number on
@ -222,9 +222,9 @@ void AddrManImpl::Serialize(Stream& s_) const
} }
} }
} }
// Store asmap checksum after bucket entries so that it // Store asmap version after bucket entries so that it
// can be ignored by older clients for backward compatibility. // can be ignored by older clients for backward compatibility.
s << m_netgroupman.GetAsmapChecksum(); s << m_netgroupman.GetAsmapVersion();
} }
template <typename Stream> template <typename Stream>
@ -330,16 +330,16 @@ void AddrManImpl::Unserialize(Stream& s_)
} }
} }
// If the bucket count and asmap checksum haven't changed, then attempt // If the bucket count and asmap version haven't changed, then attempt
// to restore the entries to the buckets/positions they were in before // to restore the entries to the buckets/positions they were in before
// serialization. // serialization.
uint256 supplied_asmap_checksum{m_netgroupman.GetAsmapChecksum()}; uint256 supplied_asmap_version{m_netgroupman.GetAsmapVersion()};
uint256 serialized_asmap_checksum; uint256 serialized_asmap_version;
if (format >= Format::V2_ASMAP) { if (format >= Format::V2_ASMAP) {
s >> serialized_asmap_checksum; s >> serialized_asmap_version;
} }
const bool restore_bucketing{nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && const bool restore_bucketing{nUBuckets == ADDRMAN_NEW_BUCKET_COUNT &&
serialized_asmap_checksum == supplied_asmap_checksum}; serialized_asmap_version == supplied_asmap_version};
if (!restore_bucketing) { if (!restore_bucketing) {
LogDebug(BCLog::ADDRMAN, "Bucketing method was updated, re-bucketing addrman entries from disk\n"); LogDebug(BCLog::ADDRMAN, "Bucketing method was updated, re-bucketing addrman entries from disk\n");

View file

@ -24,7 +24,7 @@
static constexpr size_t NUM_SOURCES = 64; static constexpr size_t NUM_SOURCES = 64;
static constexpr size_t NUM_ADDRESSES_PER_SOURCE = 256; static constexpr size_t NUM_ADDRESSES_PER_SOURCE = 256;
static NetGroupManager EMPTY_NETGROUPMAN{std::vector<bool>()}; static NetGroupManager EMPTY_NETGROUPMAN{NetGroupManager::NoAsmap()};
static constexpr uint32_t ADDRMAN_CONSISTENCY_CHECK_RATIO{0}; static constexpr uint32_t ADDRMAN_CONSISTENCY_CHECK_RATIO{0};
static std::vector<CAddress> g_sources; static std::vector<CAddress> g_sources;

View file

@ -273,7 +273,7 @@ fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value)
{ {
if (IsArgNegated(arg)) return fs::path{}; if (IsArgNegated(arg)) return fs::path{};
std::string path_str = GetArg(arg, ""); std::string path_str = GetArg(arg, "");
if (path_str.empty()) return default_value; if (path_str.empty() || path_str == "1") return default_value;
fs::path result = fs::PathFromString(path_str).lexically_normal(); fs::path result = fs::PathFromString(path_str).lexically_normal();
// Remove trailing slash, if present. // Remove trailing slash, if present.
return result.has_filename() ? result : result.parent_path(); return result.has_filename() ? result : result.parent_path();

View file

@ -94,6 +94,7 @@
#include <algorithm> #include <algorithm>
#include <condition_variable> #include <condition_variable>
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <fstream> #include <fstream>
@ -117,6 +118,10 @@
#include <zmq/zmqrpc.h> #include <zmq/zmqrpc.h>
#endif #endif
#ifdef ENABLE_EMBEDDED_ASMAP
#include <node/data/ip_asn.dat.h>
#endif
using common::AmountErrMsg; using common::AmountErrMsg;
using common::InvalidPortErrMsg; using common::InvalidPortErrMsg;
using common::ResolveErrMsg; using common::ResolveErrMsg;
@ -522,7 +527,14 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-addnode=<ip>", strprintf("Add a node to connect to and attempt to keep the connection open (see the addnode RPC help for more info). This option can be specified multiple times to add multiple nodes; connections are limited to %u at a time and are counted separately from the -maxconnections limit.", MAX_ADDNODE_CONNECTIONS), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION); argsman.AddArg("-addnode=<ip>", strprintf("Add a node to connect to and attempt to keep the connection open (see the addnode RPC help for more info). This option can be specified multiple times to add multiple nodes; connections are limited to %u at a time and are counted separately from the -maxconnections limit.", MAX_ADDNODE_CONNECTIONS), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
argsman.AddArg("-asmap=<file>", strprintf("Specify asn mapping used for bucketing of the peers (default: %s). Relative paths will be prefixed by the net-specific datadir location.", DEFAULT_ASMAP_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-asmap=<file>", strprintf("Use IP to ASN mapping for bucketing of the peers. If a file argument is not given (-asmap or -asmap=1), a file in the default location (%s) will be used.%s Relative paths will be prefixed by the net-specific datadir location.",
DEFAULT_ASMAP_FILENAME,
#ifdef ENABLE_EMBEDDED_ASMAP
" If no file is found there, the embedded mapping data in the binary will be used as a fallback."
#else
""
#endif
), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-bantime=<n>", strprintf("Default duration (in seconds) of manually configured bans (default: %u)", DEFAULT_MISBEHAVING_BANTIME), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-bantime=<n>", strprintf("Default duration (in seconds) of manually configured bans (default: %u)", DEFAULT_MISBEHAVING_BANTIME), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-bind=<addr>[:<port>][=onion]", strprintf("Bind to given address and always listen on it (default: 0.0.0.0). Use [host]:port notation for IPv6. Append =onion to tag any incoming connections to that address and port as incoming Tor connections (default: 127.0.0.1:%u=onion, testnet3: 127.0.0.1:%u=onion, testnet4: 127.0.0.1:%u=onion, signet: 127.0.0.1:%u=onion, regtest: 127.0.0.1:%u=onion)", defaultChainParams->GetDefaultPort() + 1, testnetChainParams->GetDefaultPort() + 1, testnet4ChainParams->GetDefaultPort() + 1, signetChainParams->GetDefaultPort() + 1, regtestChainParams->GetDefaultPort() + 1), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION); argsman.AddArg("-bind=<addr>[:<port>][=onion]", strprintf("Bind to given address and always listen on it (default: 0.0.0.0). Use [host]:port notation for IPv6. Append =onion to tag any incoming connections to that address and port as incoming Tor connections (default: 127.0.0.1:%u=onion, testnet3: 127.0.0.1:%u=onion, testnet4: 127.0.0.1:%u=onion, signet: 127.0.0.1:%u=onion, regtest: 127.0.0.1:%u=onion)", defaultChainParams->GetDefaultPort() + 1, testnetChainParams->GetDefaultPort() + 1, testnet4ChainParams->GetDefaultPort() + 1, signetChainParams->GetDefaultPort() + 1, regtestChainParams->GetDefaultPort() + 1), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
argsman.AddArg("-cjdnsreachable", "If set, then this host is configured for CJDNS (connecting to fc00::/8 addresses would lead us to the CJDNS network, see doc/cjdns.md) (default: 0)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-cjdnsreachable", "If set, then this host is configured for CJDNS (connecting to fc00::/8 addresses would lead us to the CJDNS network, see doc/cjdns.md) (default: 0)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@ -1454,33 +1466,65 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
ApplyArgsManOptions(args, peerman_opts); ApplyArgsManOptions(args, peerman_opts);
{ {
// Read asmap file or embedded data if configured and initialize
// Read asmap file if configured // Netgroupman with or without it
std::vector<bool> asmap; assert(!node.netgroupman);
uint256 asmap_version;
if (args.IsArgSet("-asmap") && !args.IsArgNegated("-asmap")) { if (args.IsArgSet("-asmap") && !args.IsArgNegated("-asmap")) {
const bool asmap_file_set{args.GetPathArg("-asmap") != ""};
fs::path asmap_path = args.GetPathArg("-asmap", DEFAULT_ASMAP_FILENAME); fs::path asmap_path = args.GetPathArg("-asmap", DEFAULT_ASMAP_FILENAME);
if (!asmap_path.is_absolute()) { if (!asmap_path.is_absolute()) {
asmap_path = args.GetDataDirNet() / asmap_path; asmap_path = args.GetDataDirNet() / asmap_path;
} }
if (!fs::exists(asmap_path)) {
// If a specific path was passed with the asmap argument check if
// the file actually exists in that location
if (!fs::exists(asmap_path) && asmap_file_set) {
InitError(strprintf(_("Could not find asmap file %s"), fs::quoted(fs::PathToString(asmap_path)))); InitError(strprintf(_("Could not find asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
return false; return false;
} }
asmap = DecodeAsmap(asmap_path);
if (asmap.size() == 0) { if (fs::exists(asmap_path)) {
// If a file exists at the path (could be passed or the default
// location), try to read the file
std::vector<std::byte> asmap{DecodeAsmap(asmap_path)};
if (asmap.empty()) {
// If the file could not be read, print the error depending
// on if it was passed or the default location
if (asmap_file_set) {
InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path)))); InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
} else {
InitError(strprintf(_("Could not parse asmap file in default location %s"), fs::quoted(fs::PathToString(asmap_path))));
}
return false; return false;
} }
const uint256 asmap_version = (HashWriter{} << asmap).GetHash(); node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::WithLoadedAsmap(asmap));
asmap_version = AsmapVersion(asmap);
} else {
#ifdef ENABLE_EMBEDDED_ASMAP
// If the file doesn't exist, try to use the embedded data
std::span<const std::byte> asmap{CheckAsmap(node::data::ip_asn)};
if (asmap.empty()) {
InitError(strprintf(_("Could not read embedded asmap data")));
return false;
}
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::WithEmbeddedAsmap(asmap));
asmap_version = AsmapVersion(asmap);
LogInfo("Opened asmap data (%zu bytes) from embedded byte array\n", asmap.size());
#else
// If there is no embedded data, fail and report the default
// file as missing since we only end up here if the no
// specific path was passed as an argument
InitError(strprintf(_("Could not find asmap file in default location %s"), fs::quoted(fs::PathToString(asmap_path))));
return false;
#endif
}
LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString()); LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
} else { } else {
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::NoAsmap());
LogPrintf("Using /16 prefix for IP bucketing\n"); LogPrintf("Using /16 prefix for IP bucketing\n");
} }
// Initialize netgroup manager
assert(!node.netgroupman);
node.netgroupman = std::make_unique<NetGroupManager>(std::move(asmap));
// Initialize addrman // Initialize addrman
assert(!node.addrman); assert(!node.addrman);
uiInterface.InitMessage(_("Loading P2P addresses…")); uiInterface.InitMessage(_("Loading P2P addresses…"));

View file

@ -6,13 +6,15 @@
#include <hash.h> #include <hash.h>
#include <logging.h> #include <logging.h>
#include <uint256.h>
#include <util/asmap.h> #include <util/asmap.h>
uint256 NetGroupManager::GetAsmapChecksum() const #include <cstddef>
uint256 NetGroupManager::GetAsmapVersion() const
{ {
if (!m_asmap.size()) return {}; if (!m_asmap.size()) return {};
return AsmapVersion(m_asmap);
return (HashWriter{} << m_asmap).GetHash();
} }
std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
@ -81,30 +83,25 @@ std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) co
uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
{ {
uint32_t net_class = address.GetNetClass(); uint32_t net_class = address.GetNetClass();
if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) { if (m_asmap.empty() || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
return 0; // Indicates not found, safe because AS0 is reserved per RFC7607. return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
} }
std::vector<bool> ip_bits(128); std::vector<std::byte> ip_bits(16);
if (address.HasLinkedIPv4()) { if (address.HasLinkedIPv4()) {
// For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits) // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
for (int8_t byte_i = 0; byte_i < 12; ++byte_i) { for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) { ip_bits[byte_i] = static_cast<std::byte>(IPV4_IN_IPV6_PREFIX[byte_i]);
ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
}
} }
uint32_t ipv4 = address.GetLinkedIPv4(); uint32_t ipv4 = address.GetLinkedIPv4();
for (int i = 0; i < 32; ++i) { for (int i = 0; i < 4; ++i) {
ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1; ip_bits[12 + i] = static_cast<std::byte>((ipv4 >> (24 - i * 8)) & 0xFF);
} }
} else { } else {
// Use all 128 bits of the IPv6 address otherwise // Use all 128 bits of the IPv6 address otherwise
assert(address.IsIPv6()); assert(address.IsIPv6());
auto addr_bytes = address.GetAddrBytes(); auto addr_bytes = address.GetAddrBytes();
for (int8_t byte_i = 0; byte_i < 16; ++byte_i) { for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
uint8_t cur_byte = addr_bytes[byte_i]; ip_bits[byte_i] = static_cast<std::byte>(addr_bytes[byte_i]);
for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
}
} }
} }
uint32_t mapped_as = Interpret(m_asmap, ip_bits); uint32_t mapped_as = Interpret(m_asmap, ip_bits);

View file

@ -8,6 +8,7 @@
#include <netaddress.h> #include <netaddress.h>
#include <uint256.h> #include <uint256.h>
#include <cstddef>
#include <vector> #include <vector>
/** /**
@ -15,12 +16,21 @@
*/ */
class NetGroupManager { class NetGroupManager {
public: public:
explicit NetGroupManager(std::vector<bool> asmap) static NetGroupManager WithEmbeddedAsmap(std::span<const std::byte> asmap) {
: m_asmap{std::move(asmap)} return NetGroupManager(asmap, {});
{} }
/** Get a checksum identifying the asmap being used. */ static NetGroupManager WithLoadedAsmap(std::vector<std::byte> loaded_asmap) {
uint256 GetAsmapChecksum() const; std::span<const std::byte> asmap_span(loaded_asmap);
return NetGroupManager(asmap_span, std::move(loaded_asmap));
}
static NetGroupManager NoAsmap() {
return NetGroupManager({}, {});
}
/** Get the asmap version, a checksum identifying the asmap being used. */
uint256 GetAsmapVersion() const;
/** /**
* Get the canonical identifier of the network group for address. * Get the canonical identifier of the network group for address.
@ -52,7 +62,10 @@ public:
bool UsingASMap() const; bool UsingASMap() const;
private: private:
/** Compressed IP->ASN mapping, loaded from a file when a node starts. /** Compressed IP->ASN mapping.
*
* Data may be loaded from a file when a node starts or embedded in the
* binary.
* *
* This mapping is then used for bucketing nodes in Addrman and for * This mapping is then used for bucketing nodes in Addrman and for
* ensuring we connect to a diverse set of peers in Connman. The map is * ensuring we connect to a diverse set of peers in Connman. The map is
@ -69,8 +82,17 @@ private:
* re-bucketed. * re-bucketed.
* *
* This is initialized in the constructor, const, and therefore is * This is initialized in the constructor, const, and therefore is
* thread-safe. */ * thread-safe. m_asmap can either point to m_loaded_asmap which holds
const std::vector<bool> m_asmap; * data loaded from an external file at runtime or it can point to embedded
* asmap data.
*/
const std::span<const std::byte> m_asmap;
std::vector<std::byte> m_loaded_asmap;
explicit NetGroupManager(std::span<const std::byte> asmap, std::vector<std::byte> loaded_asmap)
: m_asmap(asmap.empty() ? std::span<const std::byte>() : asmap),
m_loaded_asmap(std::move(loaded_asmap))
{}
}; };
#endif // BITCOIN_NETGROUP_H #endif // BITCOIN_NETGROUP_H

BIN
src/node/data/ip_asn.dat Normal file

Binary file not shown.

View file

@ -124,3 +124,17 @@ size_t DataStream::GetMemoryUsage() const noexcept
{ {
return sizeof(*this) + memusage::DynamicUsage(vch); return sizeof(*this) + memusage::DynamicUsage(vch);
} }
int64_t AutoFile::size()
{
if (IsNull()) {
throw std::ios_base::failure("AutoFile::size: file handle is nullptr");
}
// Temporarily save the current position
int64_t current_pos = tell();
seek(current_pos, SEEK_END);
int64_t file_size = tell();
// Restore the original position
seek(current_pos, SEEK_SET);
return file_size;
}

View file

@ -439,6 +439,9 @@ public:
/** Find position within the file. Will throw if unknown. */ /** Find position within the file. Will throw if unknown. */
int64_t tell(); int64_t tell();
/** Return the size of the file. Will throw if unknown. */
int64_t size();
/** Wrapper around FileCommit(). */ /** Wrapper around FileCommit(). */
bool Commit(); bool Commit();

View file

@ -24,7 +24,7 @@ using namespace std::literals;
using node::NodeContext; using node::NodeContext;
using util::ToString; using util::ToString;
static NetGroupManager EMPTY_NETGROUPMAN{std::vector<bool>()}; static NetGroupManager EMPTY_NETGROUPMAN{NetGroupManager::NoAsmap()};
static const bool DETERMINISTIC{true}; static const bool DETERMINISTIC{true};
static int32_t GetCheckRatio(const NodeContext& node_ctx) static int32_t GetCheckRatio(const NodeContext& node_ctx)
@ -46,20 +46,6 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0)
return serv.value_or(CService{}); return serv.value_or(CService{});
} }
static std::vector<bool> FromBytes(std::span<const std::byte> source)
{
int vector_size(source.size() * 8);
std::vector<bool> result(vector_size);
for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
uint8_t cur_byte{std::to_integer<uint8_t>(source[byte_i])};
for (int bit_i = 0; bit_i < 8; ++bit_i) {
result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
}
}
return result;
}
BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup) BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(addrman_simple) BOOST_AUTO_TEST_CASE(addrman_simple)
@ -592,8 +578,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy)
// 101.8.0.0/16 AS8 // 101.8.0.0/16 AS8
BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
{ {
std::vector<bool> asmap = FromBytes(test::data::asmap); NetGroupManager ngm_asmap{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)};
NetGroupManager ngm_asmap{asmap};
CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE); CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);
@ -646,8 +631,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
{ {
std::vector<bool> asmap = FromBytes(test::data::asmap); NetGroupManager ngm_asmap{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)};
NetGroupManager ngm_asmap{asmap};
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
@ -724,8 +708,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
BOOST_AUTO_TEST_CASE(addrman_serialization) BOOST_AUTO_TEST_CASE(addrman_serialization)
{ {
std::vector<bool> asmap1 = FromBytes(test::data::asmap); NetGroupManager netgroupman{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)};
NetGroupManager netgroupman{asmap1};
const auto ratio = GetCheckRatio(m_node); const auto ratio = GetCheckRatio(m_node);
auto addrman_asmap1 = std::make_unique<AddrMan>(netgroupman, DETERMINISTIC, ratio); auto addrman_asmap1 = std::make_unique<AddrMan>(netgroupman, DETERMINISTIC, ratio);

View file

@ -6,28 +6,18 @@
#include <netgroup.h> #include <netgroup.h>
#include <test/fuzz/fuzz.h> #include <test/fuzz/fuzz.h>
#include <util/asmap.h> #include <util/asmap.h>
#include <util/strencodings.h>
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
using namespace util::hex_literals;
//! asmap code that consumes nothing //! asmap code that consumes nothing
static const std::vector<bool> IPV6_PREFIX_ASMAP = {}; static const std::vector<std::byte> IPV6_PREFIX_ASMAP = {};
//! asmap code that consumes the 96 prefix bits of ::ffff:0/96 (IPv4-in-IPv6 map) //! asmap code that consumes the 96 prefix bits of ::ffff:0/96 (IPv4-in-IPv6 map)
static const std::vector<bool> IPV4_PREFIX_ASMAP = { static const auto IPV4_PREFIX_ASMAP = "fb03ec0fb03fc0fe00fb03ec0fb03fc0fe00fb03ec0fb0fffffeff"_hex_v;
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, // Match 0xFF
true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true // Match 0xFF
};
FUZZ_TARGET(asmap) FUZZ_TARGET(asmap)
{ {
@ -37,13 +27,11 @@ FUZZ_TARGET(asmap)
bool ipv6 = buffer[0] & 128; bool ipv6 = buffer[0] & 128;
const size_t addr_size = ipv6 ? ADDR_IPV6_SIZE : ADDR_IPV4_SIZE; const size_t addr_size = ipv6 ? ADDR_IPV6_SIZE : ADDR_IPV4_SIZE;
if (buffer.size() < size_t(1 + asmap_size + addr_size)) return; if (buffer.size() < size_t(1 + asmap_size + addr_size)) return;
std::vector<bool> asmap = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP; std::vector<std::byte> asmap_vec = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP;
asmap.reserve(asmap.size() + 8 * asmap_size);
for (int i = 0; i < asmap_size; ++i) { for (int i = 0; i < asmap_size; ++i) {
for (int j = 0; j < 8; ++j) { asmap_vec.push_back(static_cast<std::byte>(buffer[1 + i]));
asmap.push_back((buffer[1 + i] >> j) & 1);
}
} }
std::span<const std::byte> asmap(asmap_vec);
if (!SanityCheckASMap(asmap, 128)) return; if (!SanityCheckASMap(asmap, 128)) return;
const uint8_t* addr_data = buffer.data() + 1 + asmap_size; const uint8_t* addr_data = buffer.data() + 1 + asmap_size;
@ -57,6 +45,6 @@ FUZZ_TARGET(asmap)
memcpy(&ipv4, addr_data, addr_size); memcpy(&ipv4, addr_data, addr_size);
net_addr.SetIP(CNetAddr{ipv4}); net_addr.SetIP(CNetAddr{ipv4});
} }
NetGroupManager netgroupman{asmap}; NetGroupManager netgroupman{NetGroupManager::WithEmbeddedAsmap(asmap)};
(void)netgroupman.GetMappedAS(net_addr); (void)netgroupman.GetMappedAS(net_addr);
} }

View file

@ -31,19 +31,21 @@ FUZZ_TARGET(asmap_direct)
if (buffer.size() - sep_pos - 1 > 128) return; // At most 128 bits in IP address if (buffer.size() - sep_pos - 1 > 128) return; // At most 128 bits in IP address
// Checks on asmap // Checks on asmap
std::vector<bool> asmap(buffer.begin(), buffer.begin() + sep_pos); std::vector<std::byte> asmap(reinterpret_cast<const std::byte*>(buffer.data()),
if (SanityCheckASMap(asmap, buffer.size() - 1 - sep_pos)) { reinterpret_cast<const std::byte*>(buffer.data() + sep_pos));
if (SanityCheckASMap(std::span<const std::byte>(asmap), buffer.size() - 1 - sep_pos)) {
// Verify that for valid asmaps, no prefix (except up to 7 zero padding bits) is valid. // Verify that for valid asmaps, no prefix (except up to 7 zero padding bits) is valid.
std::vector<bool> asmap_prefix = asmap; std::vector<std::byte> asmap_prefix = asmap;
while (!asmap_prefix.empty() && asmap_prefix.size() + 7 > asmap.size() && asmap_prefix.back() == false) { while (!asmap_prefix.empty() && asmap_prefix.size() + 7 > asmap.size() && asmap_prefix.back() == std::byte{0}) {
asmap_prefix.pop_back(); asmap_prefix.pop_back();
} }
while (!asmap_prefix.empty()) { while (!asmap_prefix.empty()) {
asmap_prefix.pop_back(); asmap_prefix.pop_back();
assert(!SanityCheckASMap(asmap_prefix, buffer.size() - 1 - sep_pos)); assert(!SanityCheckASMap(std::span<const std::byte>(asmap_prefix), buffer.size() - 1 - sep_pos));
} }
// No address input should trigger assertions in interpreter // No address input should trigger assertions in interpreter
std::vector<bool> addr(buffer.begin() + sep_pos + 1, buffer.end()); std::vector<std::byte> addr(reinterpret_cast<const std::byte*>(buffer.data() + sep_pos + 1),
(void)Interpret(asmap, addr); reinterpret_cast<const std::byte*>(buffer.data() + buffer.size()));
(void)Interpret(std::span<const std::byte>(asmap), std::span<const std::byte>(addr));
} }
} }

View file

@ -48,7 +48,7 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize)
chainman.ResetIbd(); chainman.ResetIbd();
node::Warnings warnings{}; node::Warnings warnings{};
NetGroupManager netgroupman{{}}; NetGroupManager netgroupman{NetGroupManager::NoAsmap()};
AddrMan addrman{netgroupman, /*deterministic=*/true, 0}; AddrMan addrman{netgroupman, /*deterministic=*/true, 0};
auto peerman = PeerManager::make(connman, addrman, auto peerman = PeerManager::make(connman, addrman,
/*banman=*/nullptr, chainman, /*banman=*/nullptr, chainman,

View file

@ -65,11 +65,6 @@ template<typename B = uint8_t>
return ret; return ret;
} }
[[nodiscard]] inline std::vector<bool> ConsumeRandomLengthBitVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
{
return BytesToBits(ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length));
}
[[nodiscard]] inline DataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept [[nodiscard]] inline DataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
{ {
return DataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)}; return DataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};

View file

@ -21,6 +21,7 @@
#include <util/sock.h> #include <util/sock.h>
#include <chrono> #include <chrono>
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
#include <memory> #include <memory>
@ -210,9 +211,11 @@ public:
[[nodiscard]] inline NetGroupManager ConsumeNetGroupManager(FuzzedDataProvider& fuzzed_data_provider) noexcept [[nodiscard]] inline NetGroupManager ConsumeNetGroupManager(FuzzedDataProvider& fuzzed_data_provider) noexcept
{ {
std::vector<bool> asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider); std::vector<std::byte> asmap{ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider)};
if (!SanityCheckASMap(asmap, 128)) asmap.clear(); if (!SanityCheckASMap(std::span<std::byte>(asmap), 128)) {
return NetGroupManager(asmap); return NetGroupManager::NoAsmap();
}
return NetGroupManager::WithLoadedAsmap(asmap);
} }
inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept

View file

@ -323,7 +323,7 @@ BOOST_AUTO_TEST_CASE(subnet_test)
BOOST_AUTO_TEST_CASE(netbase_getgroup) BOOST_AUTO_TEST_CASE(netbase_getgroup)
{ {
NetGroupManager netgroupman{std::vector<bool>()}; // use /16 NetGroupManager netgroupman{NetGroupManager::NoAsmap()}; // use /16
BOOST_CHECK(netgroupman.GetGroup(ResolveIP("127.0.0.1")) == std::vector<unsigned char>({0})); // Local -> !Routable() BOOST_CHECK(netgroupman.GetGroup(ResolveIP("127.0.0.1")) == std::vector<unsigned char>({0})); // Local -> !Routable()
BOOST_CHECK(netgroupman.GetGroup(ResolveIP("257.0.0.1")) == std::vector<unsigned char>({0})); // !Valid -> !Routable() BOOST_CHECK(netgroupman.GetGroup(ResolveIP("257.0.0.1")) == std::vector<unsigned char>({0})); // !Valid -> !Routable()
BOOST_CHECK(netgroupman.GetGroup(ResolveIP("10.0.0.1")) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable() BOOST_CHECK(netgroupman.GetGroup(ResolveIP("10.0.0.1")) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()

View file

@ -29,6 +29,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullpt"}); BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullpt"});
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullpt"}); BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullpt"});
BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullpt"}); BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullpt"});
BOOST_CHECK_EXCEPTION(xor_file.size(), std::ios_base::failure, HasReason{"AutoFile::size: file handle is nullptr"});
} }
{ {
#ifdef __MINGW64__ #ifdef __MINGW64__
@ -39,6 +40,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
#endif #endif
AutoFile xor_file{raw_file(mode), xor_pat}; AutoFile xor_file{raw_file(mode), xor_pat};
xor_file << test1 << test2; xor_file << test1 << test2;
BOOST_CHECK_EQUAL(xor_file.size(), 14);
} }
{ {
// Read raw from disk // Read raw from disk
@ -48,6 +50,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa"); BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa");
// Check that no padding exists // Check that no padding exists
BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"}); BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
BOOST_CHECK_EQUAL(non_xor_file.size(), 14);
} }
{ {
AutoFile xor_file{raw_file("rb"), xor_pat}; AutoFile xor_file{raw_file("rb"), xor_pat};
@ -57,6 +60,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2)); BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
// Check that eof was reached // Check that eof was reached
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"}); BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
BOOST_CHECK_EQUAL(xor_file.size(), 14);
} }
{ {
AutoFile xor_file{raw_file("rb"), xor_pat}; AutoFile xor_file{raw_file("rb"), xor_pat};
@ -68,6 +72,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
// Check that ignore and read fail now // Check that ignore and read fail now
BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"}); BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"}); BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
BOOST_CHECK_EQUAL(xor_file.size(), 14);
} }
} }

View file

@ -332,7 +332,7 @@ TestingSetup::TestingSetup(
if (!opts.setup_net) return; if (!opts.setup_net) return;
m_node.netgroupman = std::make_unique<NetGroupManager>(/*asmap=*/std::vector<bool>()); m_node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::NoAsmap());
m_node.addrman = std::make_unique<AddrMan>(*m_node.netgroupman, m_node.addrman = std::make_unique<AddrMan>(*m_node.netgroupman,
/*deterministic=*/false, /*deterministic=*/false,
m_node.args->GetIntArg("-checkaddrman", 0)); m_node.args->GetIntArg("-checkaddrman", 0));

View file

@ -5,15 +5,19 @@
#include <util/asmap.h> #include <util/asmap.h>
#include <clientversion.h> #include <clientversion.h>
#include <hash.h>
#include <logging.h> #include <logging.h>
#include <serialize.h> #include <serialize.h>
#include <streams.h> #include <streams.h>
#include <uint256.h>
#include <util/fs.h> #include <util/fs.h>
#include <algorithm> #include <algorithm>
#include <bit> #include <bit>
#include <cassert> #include <cassert>
#include <cstddef>
#include <cstdio> #include <cstdio>
#include <span>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -21,15 +25,14 @@ namespace {
constexpr uint32_t INVALID = 0xFFFFFFFF; constexpr uint32_t INVALID = 0xFFFFFFFF;
uint32_t DecodeBits(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos, uint8_t minval, const std::vector<uint8_t> &bit_sizes) uint32_t DecodeBits(size_t& bitpos, const std::span<const std::byte>& data, uint8_t minval, const std::vector<uint8_t>& bit_sizes)
{ {
uint32_t val = minval; uint32_t val = minval;
bool bit; bool bit;
for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin(); for (auto bit_sizes_it = bit_sizes.begin(); bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
if (bit_sizes_it + 1 != bit_sizes.end()) { if (bit_sizes_it + 1 != bit_sizes.end()) {
if (bitpos == endpos) break; if (bitpos >= data.size() * 8) break;
bit = *bitpos; bit = (std::to_integer<uint8_t>(data[bitpos / 8]) >> (bitpos % 8)) & 1;
bitpos++; bitpos++;
} else { } else {
bit = 0; bit = 0;
@ -38,8 +41,8 @@ uint32_t DecodeBits(std::vector<bool>::const_iterator& bitpos, const std::vector
val += (1 << *bit_sizes_it); val += (1 << *bit_sizes_it);
} else { } else {
for (int b = 0; b < *bit_sizes_it; b++) { for (int b = 0; b < *bit_sizes_it; b++) {
if (bitpos == endpos) return INVALID; // Reached EOF in mantissa if (bitpos >= data.size() * 8) return INVALID; // Reached EOF in mantissa
bit = *bitpos; bit = (std::to_integer<uint8_t>(data[bitpos / 8]) >> (bitpos % 8)) & 1;
bitpos++; bitpos++;
val += bit << (*bit_sizes_it - 1 - b); val += bit << (*bit_sizes_it - 1 - b);
} }
@ -58,69 +61,68 @@ enum class Instruction : uint32_t
}; };
const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1}; const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
Instruction DecodeType(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos) Instruction DecodeType(size_t& bitpos, const std::span<const std::byte>& data)
{ {
return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES)); return Instruction(DecodeBits(bitpos, data, 0, TYPE_BIT_SIZES));
} }
const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24}; const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos) uint32_t DecodeASN(size_t& bitpos, const std::span<const std::byte>& data)
{ {
return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES); return DecodeBits(bitpos, data, 1, ASN_BIT_SIZES);
} }
const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8}; const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos) uint32_t DecodeMatch(size_t& bitpos, const std::span<const std::byte>& data)
{ {
return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES); return DecodeBits(bitpos, data, 2, MATCH_BIT_SIZES);
} }
const std::vector<uint8_t> JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}; const std::vector<uint8_t> JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos) uint32_t DecodeJump(size_t& bitpos, const std::span<const std::byte>& data)
{ {
return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES); return DecodeBits(bitpos, data, 17, JUMP_BIT_SIZES);
} }
} }
uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip) uint32_t Interpret(const std::span<const std::byte>& asmap, const std::span<const std::byte>& ip)
{ {
std::vector<bool>::const_iterator pos = asmap.begin(); size_t pos{0};
const std::vector<bool>::const_iterator endpos = asmap.end(); uint8_t bits = ip.size() * 8;
uint8_t bits = ip.size();
uint32_t default_asn = 0; uint32_t default_asn = 0;
uint32_t jump, match, matchlen; uint32_t jump, match, matchlen;
Instruction opcode; Instruction opcode;
while (pos != endpos) { while (pos < asmap.size() * 8) {
opcode = DecodeType(pos, endpos); opcode = DecodeType(pos, asmap);
if (opcode == Instruction::RETURN) { if (opcode == Instruction::RETURN) {
default_asn = DecodeASN(pos, endpos); default_asn = DecodeASN(pos, asmap);
if (default_asn == INVALID) break; // ASN straddles EOF if (default_asn == INVALID) break; // ASN straddles EOF
return default_asn; return default_asn;
} else if (opcode == Instruction::JUMP) { } else if (opcode == Instruction::JUMP) {
jump = DecodeJump(pos, endpos); jump = DecodeJump(pos, asmap);
if (jump == INVALID) break; // Jump offset straddles EOF if (jump == INVALID) break; // Jump offset straddles EOF
if (bits == 0) break; // No input bits left if (bits == 0) break; // No input bits left
if (int64_t{jump} >= int64_t{endpos - pos}) break; // Jumping past EOF if (int64_t{jump} >= static_cast<int64_t>(asmap.size() * 8 - pos)) break; // Jumping past EOF
if (ip[ip.size() - bits]) { if ((std::to_integer<uint8_t>(ip[(ip.size() * 8 - bits) / 8]) >> (7 - ((ip.size() * 8 - bits) % 8))) & 1) {
pos += jump; pos += jump;
} }
bits--; bits--;
} else if (opcode == Instruction::MATCH) { } else if (opcode == Instruction::MATCH) {
match = DecodeMatch(pos, endpos); match = DecodeMatch(pos, asmap);
if (match == INVALID) break; // Match bits straddle EOF if (match == INVALID) break; // Match bits straddle EOF
matchlen = std::bit_width(match) - 1; matchlen = std::bit_width(match) - 1;
if (bits < matchlen) break; // Not enough input bits if (bits < matchlen) break; // Not enough input bits
for (uint32_t bit = 0; bit < matchlen; bit++) { for (uint32_t bit = 0; bit < matchlen; bit++) {
if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) { if (((std::to_integer<uint8_t>(ip[(ip.size() * 8 - bits) / 8]) >> (7 - ((ip.size() * 8 - bits) % 8))) & 1) != ((match >> (matchlen - 1 - bit)) & 1)) {
return default_asn; return default_asn;
} }
bits--; bits--;
} }
} else if (opcode == Instruction::DEFAULT) { } else if (opcode == Instruction::DEFAULT) {
default_asn = DecodeASN(pos, endpos); default_asn = DecodeASN(pos, asmap);
if (default_asn == INVALID) break; // ASN straddles EOF if (default_asn == INVALID) break; // ASN straddles EOF
} else { } else {
break; // Instruction straddles EOF break; // Instruction straddles EOF
@ -130,50 +132,48 @@ uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
return 0; // 0 is not a valid ASN return 0; // 0 is not a valid ASN
} }
bool SanityCheckASMap(const std::vector<bool>& asmap, int bits) bool SanityCheckASMap(const std::span<const std::byte>& asmap, int bits)
{ {
const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end(); size_t pos{0};
std::vector<bool>::const_iterator pos = begin; size_t endpos{asmap.size() * 8};
std::vector<std::pair<uint32_t, int>> jumps; // All future positions we may jump to (bit offset in asmap -> bits to consume left) std::vector<std::pair<uint32_t, int>> jumps; // All future positions we may jump to (bit offset in asmap -> bits to consume left)
jumps.reserve(bits); jumps.reserve(bits);
Instruction prevopcode = Instruction::JUMP; Instruction prevopcode = Instruction::JUMP;
bool had_incomplete_match = false; bool had_incomplete_match = false;
while (pos != endpos) { while (pos != endpos) {
uint32_t offset = pos - begin; if (!jumps.empty() && pos >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction
if (!jumps.empty() && offset >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction Instruction opcode = DecodeType(pos, asmap);
Instruction opcode = DecodeType(pos, endpos);
if (opcode == Instruction::RETURN) { if (opcode == Instruction::RETURN) {
if (prevopcode == Instruction::DEFAULT) return false; // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN) if (prevopcode == Instruction::DEFAULT) return false; // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN)
uint32_t asn = DecodeASN(pos, endpos); uint32_t asn = DecodeASN(pos, asmap);
if (asn == INVALID) return false; // ASN straddles EOF if (asn == INVALID) return false; // ASN straddles EOF
if (jumps.empty()) { if (jumps.empty()) {
// Nothing to execute anymore // Nothing to execute anymore
if (endpos - pos > 7) return false; // Excessive padding if (endpos - pos > 7) return false; // Excessive padding
while (pos != endpos) { while (pos != endpos) {
if (*pos) return false; // Nonzero padding bit if ((std::to_integer<uint8_t>(asmap[pos / 8]) >> (pos % 8)) & 1) return false; // Nonzero padding bit
++pos; ++pos;
} }
return true; // Sanely reached EOF return true; // Sanely reached EOF
} else { } else {
// Continue by pretending we jumped to the next instruction // Continue by pretending we jumped to the next instruction
offset = pos - begin; if (pos != jumps.back().first) return false; // Unreachable code
if (offset != jumps.back().first) return false; // Unreachable code
bits = jumps.back().second; // Restore the number of bits we would have had left after this jump bits = jumps.back().second; // Restore the number of bits we would have had left after this jump
jumps.pop_back(); jumps.pop_back();
prevopcode = Instruction::JUMP; prevopcode = Instruction::JUMP;
} }
} else if (opcode == Instruction::JUMP) { } else if (opcode == Instruction::JUMP) {
uint32_t jump = DecodeJump(pos, endpos); uint32_t jump = DecodeJump(pos, asmap);
if (jump == INVALID) return false; // Jump offset straddles EOF if (jump == INVALID) return false; // Jump offset straddles EOF
if (int64_t{jump} > int64_t{endpos - pos}) return false; // Jump out of range if (int64_t{jump} > static_cast<int64_t>(endpos - pos)) return false; // Jump out of range
if (bits == 0) return false; // Consuming bits past the end of the input if (bits == 0) return false; // Consuming bits past the end of the input
--bits; --bits;
uint32_t jump_offset = pos - begin + jump; uint32_t jump_offset = pos + jump;
if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps
jumps.emplace_back(jump_offset, bits); jumps.emplace_back(jump_offset, bits);
prevopcode = Instruction::JUMP; prevopcode = Instruction::JUMP;
} else if (opcode == Instruction::MATCH) { } else if (opcode == Instruction::MATCH) {
uint32_t match = DecodeMatch(pos, endpos); uint32_t match = DecodeMatch(pos, asmap);
if (match == INVALID) return false; // Match bits straddle EOF if (match == INVALID) return false; // Match bits straddle EOF
int matchlen = std::bit_width(match) - 1; int matchlen = std::bit_width(match) - 1;
if (prevopcode != Instruction::MATCH) had_incomplete_match = false; if (prevopcode != Instruction::MATCH) had_incomplete_match = false;
@ -184,7 +184,7 @@ bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
prevopcode = Instruction::MATCH; prevopcode = Instruction::MATCH;
} else if (opcode == Instruction::DEFAULT) { } else if (opcode == Instruction::DEFAULT) {
if (prevopcode == Instruction::DEFAULT) return false; // There should not be two successive DEFAULTs (they could be combined into one) if (prevopcode == Instruction::DEFAULT) return false; // There should not be two successive DEFAULTs (they could be combined into one)
uint32_t asn = DecodeASN(pos, endpos); uint32_t asn = DecodeASN(pos, asmap);
if (asn == INVALID) return false; // ASN straddles EOF if (asn == INVALID) return false; // ASN straddles EOF
prevopcode = Instruction::DEFAULT; prevopcode = Instruction::DEFAULT;
} else { } else {
@ -194,30 +194,44 @@ bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
return false; // Reached EOF without RETURN instruction return false; // Reached EOF without RETURN instruction
} }
std::vector<bool> DecodeAsmap(fs::path path) std::span<const std::byte> CheckAsmap(const std::span<const std::byte>& data)
{
if (data.empty()) {
return {};
}
if (!SanityCheckASMap(data, 128)) {
LogInfo("Sanity check of asmap data failed\n");
return {};
}
return data;
}
std::vector<std::byte> DecodeAsmap(fs::path path)
{ {
std::vector<bool> bits;
FILE *filestr = fsbridge::fopen(path, "rb"); FILE *filestr = fsbridge::fopen(path, "rb");
AutoFile file{filestr}; AutoFile file{filestr};
if (file.IsNull()) { if (file.IsNull()) {
LogPrintf("Failed to open asmap file from disk\n"); LogInfo("Failed to open asmap file from disk\n");
return bits;
}
file.seek(0, SEEK_END);
int length = file.tell();
LogPrintf("Opened asmap file %s (%d bytes) from disk\n", fs::quoted(fs::PathToString(path)), length);
file.seek(0, SEEK_SET);
uint8_t cur_byte;
for (int i = 0; i < length; ++i) {
file >> cur_byte;
for (int bit = 0; bit < 8; ++bit) {
bits.push_back((cur_byte >> bit) & 1);
}
}
if (!SanityCheckASMap(bits, 128)) {
LogPrintf("Sanity check of asmap file %s failed\n", fs::quoted(fs::PathToString(path)));
return {}; return {};
} }
return bits;
int64_t length{file.size()};
LogInfo("Opened asmap file %s (%d bytes) from disk\n", fs::quoted(fs::PathToString(path)), length);
std::vector<std::byte> buffer(length);
file.read(buffer);
if (!SanityCheckASMap(buffer, 128)) {
LogInfo("Sanity check of asmap data failed\n");
return {};
}
return buffer;
} }
uint256 AsmapVersion(const std::span<const std::byte>& data)
{
HashWriter asmap_hasher;
asmap_hasher << data;
return asmap_hasher.GetHash();
}

View file

@ -5,16 +5,23 @@
#ifndef BITCOIN_UTIL_ASMAP_H #ifndef BITCOIN_UTIL_ASMAP_H
#define BITCOIN_UTIL_ASMAP_H #define BITCOIN_UTIL_ASMAP_H
#include <uint256.h>
#include <util/fs.h> #include <util/fs.h>
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <span>
#include <vector> #include <vector>
uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip); uint32_t Interpret(const std::span<const std::byte>& asmap, const std::span<const std::byte>& ip);
bool SanityCheckASMap(const std::vector<bool>& asmap, int bits); bool SanityCheckASMap(const std::span<const std::byte>& asmap, int bits);
/** Read asmap from provided binary file */ /** Read and check asmap from provided binary file */
std::vector<bool> DecodeAsmap(fs::path path); std::vector<std::byte> DecodeAsmap(fs::path path);
/** Check asmap from embedded data */
std::span<const std::byte> CheckAsmap(const std::span<const std::byte>& data);
/** Calculate the asmap version, a checksum identifying the asmap being used. */
uint256 AsmapVersion(const std::span<const std::byte>& data);
#endif // BITCOIN_UTIL_ASMAP_H #endif // BITCOIN_UTIL_ASMAP_H

View file

@ -539,8 +539,7 @@ void BerkeleyRODatabase::Open()
page_size = outer_meta.pagesize; page_size = outer_meta.pagesize;
// Verify the size of the file is a multiple of the page size // Verify the size of the file is a multiple of the page size
db_file.seek(0, SEEK_END); int64_t size{db_file.size()};
int64_t size = db_file.tell();
// Since BDB stores everything in a page, the file size should be a multiple of the page size; // Since BDB stores everything in a page, the file size should be a multiple of the page size;
// However, BDB doesn't actually check that this is the case, and enforcing this check results // However, BDB doesn't actually check that this is the case, and enforcing this check results

View file

@ -24,6 +24,7 @@ function(create_test_config)
set_configure_variable(BUILD_DAEMON BUILD_BITCOIND) set_configure_variable(BUILD_DAEMON BUILD_BITCOIND)
set_configure_variable(BUILD_FUZZ_BINARY ENABLE_FUZZ_BINARY) set_configure_variable(BUILD_FUZZ_BINARY ENABLE_FUZZ_BINARY)
set_configure_variable(WITH_ZMQ ENABLE_ZMQ) set_configure_variable(WITH_ZMQ ENABLE_ZMQ)
set_configure_variable(WITH_EMBEDDED_ASMAP ENABLE_EMBEDDED_ASMAP)
set_configure_variable(ENABLE_EXTERNAL_SIGNER ENABLE_EXTERNAL_SIGNER) set_configure_variable(ENABLE_EXTERNAL_SIGNER ENABLE_EXTERNAL_SIGNER)
set_configure_variable(WITH_USDT ENABLE_USDT_TRACEPOINTS) set_configure_variable(WITH_USDT ENABLE_USDT_TRACEPOINTS)

View file

@ -24,5 +24,6 @@ RPCAUTH=@abs_top_srcdir@/share/rpcauth/rpcauth.py
@BUILD_BITCOIND_TRUE@ENABLE_BITCOIND=true @BUILD_BITCOIND_TRUE@ENABLE_BITCOIND=true
@ENABLE_FUZZ_BINARY_TRUE@ENABLE_FUZZ_BINARY=true @ENABLE_FUZZ_BINARY_TRUE@ENABLE_FUZZ_BINARY=true
@ENABLE_ZMQ_TRUE@ENABLE_ZMQ=true @ENABLE_ZMQ_TRUE@ENABLE_ZMQ=true
@ENABLE_EMBEDDED_ASMAP_TRUE@ENABLE_EMBEDDED_ASMAP=true
@ENABLE_EXTERNAL_SIGNER_TRUE@ENABLE_EXTERNAL_SIGNER=true @ENABLE_EXTERNAL_SIGNER_TRUE@ENABLE_EXTERNAL_SIGNER=true
@ENABLE_USDT_TRACEPOINTS_TRUE@ENABLE_USDT_TRACEPOINTS=true @ENABLE_USDT_TRACEPOINTS_TRUE@ENABLE_USDT_TRACEPOINTS=true

View file

@ -31,7 +31,7 @@ from test_framework.util import assert_equal
DEFAULT_ASMAP_FILENAME = 'ip_asn.map' # defined in src/init.cpp DEFAULT_ASMAP_FILENAME = 'ip_asn.map' # defined in src/init.cpp
ASMAP = 'src/test/data/asmap.raw' # path to unit test skeleton asmap ASMAP = 'src/test/data/asmap.raw' # path to unit test skeleton asmap
VERSION = 'fec61fa21a9f46f3b17bdcd660d7f4cd90b966aad3aec593c99b35f0aca15853' VERSION = 'bafc9da308f45179443bd1d22325400ac9104f741522d003e3fac86700f68895'
def expected_messages(filename): def expected_messages(filename):
return [f'Opened asmap file "{filename}" (59 bytes) from disk', return [f'Opened asmap file "{filename}" (59 bytes) from disk',
@ -79,7 +79,7 @@ class AsmapTest(BitcoinTestFramework):
self.start_node(0, [f'-asmap={name}']) self.start_node(0, [f'-asmap={name}'])
os.remove(filename) os.remove(filename)
def test_default_asmap(self): def test_default_asmap_file(self):
shutil.copyfile(self.asmap_raw, self.default_asmap) shutil.copyfile(self.asmap_raw, self.default_asmap)
for arg in ['-asmap', '-asmap=']: for arg in ['-asmap', '-asmap=']:
self.log.info(f'Test bitcoind {arg} (using default map file)') self.log.info(f'Test bitcoind {arg} (using default map file)')
@ -88,6 +88,20 @@ class AsmapTest(BitcoinTestFramework):
self.start_node(0, [arg]) self.start_node(0, [arg])
os.remove(self.default_asmap) os.remove(self.default_asmap)
def test_embedded_asmap(self):
if self.is_embedded_asmap_compiled():
for arg in ['-asmap', '-asmap=1']:
self.log.info(f'Test bitcoind {arg} (using embedded map data)')
self.stop_node(0)
with self.node.assert_debug_log(["Opened asmap data", "from embedded byte array"]):
self.start_node(0, [arg])
else:
self.stop_node(0)
for arg in ['-asmap', '-asmap=1']:
self.log.info(f'Test bitcoind {arg} (compiled without embedded map data)')
msg = f"Error: Could not find asmap file in default location \"{self.default_asmap}\""
self.node.assert_start_raises_init_error(extra_args=[arg], expected_msg=msg)
def test_asmap_interaction_with_addrman_containing_entries(self): def test_asmap_interaction_with_addrman_containing_entries(self):
self.log.info("Test bitcoind -asmap restart with addrman containing new and tried entries") self.log.info("Test bitcoind -asmap restart with addrman containing new and tried entries")
self.stop_node(0) self.stop_node(0)
@ -104,18 +118,19 @@ class AsmapTest(BitcoinTestFramework):
self.node.getnodeaddresses() # getnodeaddresses re-runs the addrman checks self.node.getnodeaddresses() # getnodeaddresses re-runs the addrman checks
os.remove(self.default_asmap) os.remove(self.default_asmap)
def test_default_asmap_with_missing_file(self): def test_asmap_with_missing_file(self):
self.log.info('Test bitcoind -asmap with missing default map file') self.log.info('Test bitcoind -asmap= with missing map file')
self.stop_node(0) self.stop_node(0)
msg = f"Error: Could not find asmap file \"{self.default_asmap}\"" bogus_map = os.path.join(self.datadir, "bogus.map")
self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg) msg = f"Error: Could not find asmap file \"{bogus_map}\""
self.node.assert_start_raises_init_error(extra_args=[f"-asmap={bogus_map}"], expected_msg=msg)
def test_empty_asmap(self): def test_empty_asmap(self):
self.log.info('Test bitcoind -asmap with empty map file') self.log.info('Test bitcoind -asmap with empty map file')
self.stop_node(0) self.stop_node(0)
with open(self.default_asmap, "w", encoding="utf-8") as f: with open(self.default_asmap, "w", encoding="utf-8") as f:
f.write("") f.write("")
msg = f"Error: Could not parse asmap file \"{self.default_asmap}\"" msg = f"Error: Could not parse asmap file in default location \"{self.default_asmap}\""
self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg) self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg)
os.remove(self.default_asmap) os.remove(self.default_asmap)
@ -146,9 +161,10 @@ class AsmapTest(BitcoinTestFramework):
self.test_noasmap_arg() self.test_noasmap_arg()
self.test_asmap_with_absolute_path() self.test_asmap_with_absolute_path()
self.test_asmap_with_relative_path() self.test_asmap_with_relative_path()
self.test_default_asmap() self.test_default_asmap_file()
self.test_embedded_asmap()
self.test_asmap_interaction_with_addrman_containing_entries() self.test_asmap_interaction_with_addrman_containing_entries()
self.test_default_asmap_with_missing_file() self.test_asmap_with_missing_file()
self.test_empty_asmap() self.test_empty_asmap()
self.test_asmap_health_check() self.test_asmap_health_check()

View file

@ -1053,6 +1053,10 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
"""Checks whether the zmq module was compiled.""" """Checks whether the zmq module was compiled."""
return self.config["components"].getboolean("ENABLE_ZMQ") return self.config["components"].getboolean("ENABLE_ZMQ")
def is_embedded_asmap_compiled(self):
"""Checks whether ASMap data was embedded during compilation."""
return self.config["components"].getboolean("ENABLE_EMBEDDED_ASMAP")
def is_usdt_compiled(self): def is_usdt_compiled(self):
"""Checks whether the USDT tracepoints were compiled.""" """Checks whether the USDT tracepoints were compiled."""
return self.config["components"].getboolean("ENABLE_USDT_TRACEPOINTS") return self.config["components"].getboolean("ENABLE_USDT_TRACEPOINTS")